Linux basics reference

Dilip Kumar
18 min readJan 21, 2025

--

Chapter 1: Introduction to Linux

1. What is Unix

Unix is a proprietary, multi-user, multi-tasking operating system developed in the late 1960s at AT&T Bell Labs. It was designed to be portable, multi-tasking, and multi-user in a time-sharing configuration.

Key Features:

  • Proprietary (owned by AT&T and later various vendors like IBM, HP, and Sun).
  • Written in C and assembly language.
  • Known for its stability, security, and scalability.
  • Used primarily in enterprise environments, servers, and workstations.

2. What is Linux?

Linux is an open-source, Unix-like operating system kernel that serves as the core of many operating systems (distributions). It was created by Linus Torvalds in 1991 and is widely used in servers, desktops, embedded systems, and more.

3. Frequently Asked Questions (FAQs)

Q1: Is Linux a Unix system?

  • No, Linux is not a Unix system. It is a Unix-like operating system inspired by Unix but developed independently.

Q2: Can I run Unix software on Linux?

  • Many Unix applications can be recompiled to run on Linux. Some Unix software may also run on Linux using compatibility layers.

Q3: Why is Linux more popular than Unix?

  • Linux is free, open-source, and has a large community of developers. It supports a wide range of hardware and is highly customizable, making it more accessible than Unix.

4. History of Linux

  • 1969: Unix was developed at AT&T Bell Labs.
  • 1983: Richard Stallman started the GNU Project to create a free Unix-like OS.
  • 1991: Linus Torvalds developed the Linux kernel as a hobby project.
  • 1992: Linux combined with GNU tools to form a complete OS.
  • Today: Linux powers everything from smartphones (Android) to supercomputers.

5. Linux Distributions (Distros)

A Linux distribution is a complete operating system built around the Linux kernel. It includes the kernel, GNU tools, libraries, and additional software.

Popular Linux Distributions:

  1. Ubuntu: User-friendly, great for beginners.
  2. Fedora: Cutting-edge features, backed by Red Hat.
  3. Debian: Stable and reliable, base for many distros.
  4. CentOS: Enterprise-grade, derived from Red Hat Enterprise Linux (RHEL).
  5. Arch Linux: Lightweight and highly customizable.

Choosing a Distribution:

  • For Beginners: Ubuntu, Linux Mint.
  • For Developers: Fedora, Debian.
  • For Servers: CentOS, Ubuntu Server.
  • For Advanced Users: Arch Linux, Gentoo.

6. Examples of Unix and Linux Systems

Unix Systems:

  • Solaris: Developed by Sun Microsystems (now Oracle).
  • AIX: Developed by IBM.
  • HP-UX: Developed by Hewlett-Packard.
  • macOS: Apple’s desktop OS (based on BSD, a Unix variant).

Linux Systems:

  • Ubuntu: Popular desktop and server distribution.
  • Fedora: Cutting-edge features, backed by Red Hat.
  • Debian: Stable and reliable, base for many distros.
  • CentOS: Enterprise-grade, derived from RHEL.

7. Getting Started with Linux

To start using Linux:

  1. Choose a Distribution: For beginners, Ubuntu is recommended.
  2. Download the ISO: Get the installation file from the official website.
  3. Create a Bootable USB: Use tools like Rufus (Windows) or Etcher (cross-platform).
  4. Install Linux: Follow the installation wizard.

8. Linux Kernel vs. Unix Kernel

1. What is a Kernel?

The kernel is the core component of an operating system. It manages system resources, hardware communication, memory, processes, and security. Both Linux and Unix have kernels, but they are implemented differently.

2. Unix Kernel

  • Origin: Developed in the late 1960s at AT&T Bell Labs.
  • Type: Monolithic kernel.
  • Design: The Unix kernel is designed to be simple, modular, and portable.
  • Licensing: Proprietary (owned by AT&T and later by various vendors like IBM, HP, and Sun).
  • Variants: Different vendors have their own versions of the Unix kernel (e.g., Solaris kernel, AIX kernel, HP-UX kernel).

Key Features of Unix Kernel:

  • Monolithic Design: All operating system services (file system, device drivers, etc.) run in kernel space.
  • Portability: Written in C, making it easier to port to different hardware architectures.
  • Stability: Known for its reliability and robustness in enterprise environments.

3. Linux Kernel

  • Origin: Created by Linus Torvalds in 1991 as a free and open-source alternative to Unix.
  • Type: Monolithic kernel with modular design.
  • Design: Inspired by Unix but developed independently. It is highly customizable and supports a wide range of hardware.
  • Licensing: Open-source (released under the GNU General Public License — GPL).
  • Variants: The Linux kernel is used in various distributions (e.g., Ubuntu, Fedora, Debian).

Key Features of Linux Kernel:

  • Modular Design: Supports loadable kernel modules (LKMs), allowing drivers and other features to be added or removed without rebooting.
  • Hardware Support: Supports a wide range of hardware architectures (x86, ARM, RISC-V, etc.).
  • Open-Source: Freely available for modification and distribution.
  • Scalability: Used in everything from embedded systems to supercomputers.

4. Key Differences Between Linux and Unix Kernels

5. Similarities Between Linux and Unix Kernels

  • Monolithic Design: Both kernels are monolithic, meaning most operating system services run in kernel space.
  • Multi-User and Multi-Tasking: Both support multiple users and processes running simultaneously.
  • Command-Line Interface: Both rely heavily on command-line interfaces (CLI) for system administration.
  • File System Hierarchy: Both follow a similar file system hierarchy (e.g., /bin, /etc, /usr).

6. Example: Kernel Version and Information

Unix (Solaris):

# Check kernel version on Solaris
$ uname -a
SunOS hostname 5.11 11.4.42.111.0 sun4v sparc SUNW,SPARC-Enterprise-T5220

Linux (Ubuntu):

# Check kernel version on Linux
$ uname -a
Linux hostname 5.15.0-83-generic #92-Ubuntu SMP Tue Oct 3 09:00:00 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Chapter 2: Linux Installation and Setup

1. Choosing a Linux Distribution

Before installing Linux, you need to choose a distribution (distro) that suits your needs. Here are some popular options:

2. System Requirements

Before installing Linux, ensure your system meets the minimum requirements for your chosen distribution. Here are general guidelines:

3. Creating a Bootable USB

To install Linux, you’ll need a bootable USB drive. Here’s how to create one:

On Windows:

  1. Download a tool like Rufus or Balena Etcher.
  2. Insert a USB drive (at least 4 GB).
  3. Open Rufus, select the Linux ISO file, and click “Start.”

On Linux:

  1. Use the dd command to create a bootable USB:
sudo dd if=/path/to/linux.iso of=/dev/sdX bs=4M status=progress

Replace /dev/sdX with your USB drive (e.g., /dev/sdb).

4. Installing Linux

Once you have a bootable USB, follow these steps to install Linux:

Step 1: Boot from USB

  1. Insert the USB drive into your computer.
  2. Restart your computer and enter the BIOS/UEFI settings (usually by pressing F2, F12, or Del).
  3. Set the USB drive as the primary boot device.
  4. Save changes and exit. Your computer will boot into the Linux installer.

Step 2: Follow the Installation Wizard

  1. Choose your language and keyboard layout.
  2. Select the installation type:
  • Erase disk and install Linux: Wipes the entire disk.
  • Something else: Allows manual partitioning.

3. Set up partitions (see next section for details).

4. Create a user account and set a password.

5. Wait for the installation to complete.

5. Disk Partitioning

Partitioning is the process of dividing your disk into sections. Here’s a basic guide:

Recommended Partition Scheme:

Chapter 3: Linux File System Hierarchy

1. What is the Linux File System?

The Linux file system is a hierarchical structure that organizes files and directories. Everything in Linux is treated as a file, including hardware devices, processes, and directories. The file system starts from the root directory (/) and branches out into subdirectories.

2. Key Directories in Linux

Here’s an overview of the most important directories in the Linux file system:

3. Navigating the File System

Here are some basic commands to navigate and explore the file system:

Print Working Directory (pwd):

$ pwd
/home/username

List Files and Directories (ls):

$ls

Options:

  • -l: Long listing format.
  • -a: Show hidden files (files starting with .).
  • -h: Human-readable file sizes.

Example:

$ ls -l /home

Change Directory (cd):

$ cd /path/to/directory

4. File and Directory Operations

Here are some common commands for managing files and directories:

Create a Directory (mkdir):

mkdir dirname

Create a File (touch):

touch filename

Copy Files or Directories (cp):

cp source destination

Move or Rename Files or Directories (mv):

mv source destination

Delete Files or Directories (rm):

rm filename

Options:

  • -r: Recursively delete directories.
  • -f: Force deletion (no confirmation).

5. File Permissions and Ownership

Linux uses a permission system to control access to files and directories. Each file has three types of permissions:

  • Read (r): View the file’s contents.
  • Write (w): Modify the file.
  • Execute (x): Run the file as a program.

View Permissions (ls -l):

$ ls -l
-rw-r--r-- 1 username groupname 4096 Oct 10 12:34 file.txt
  • The first character (-) indicates the file type (- for regular files, d for directories).
  • The next three characters (rw-) are the owner’s permissions.
  • The next three (r--) are the group’s permissions.
  • The last three (r--) are others’ permissions.

Change Permissions (chmod):

chmod permissions filename

Example

$ chmod 755 script.sh
  • 7 (owner): Read, write, execute (rwx).
  • 5 (group): Read, execute (r-x).
  • 5 (others): Read, execute (r-x).

Executable permission for everyone

$ chmod +x script.sh
  • By default file created with (-rw-r — r — ) doesn’t have execution permission.
  • Above command add executable permission to everyone and changed to -rwxr-xr-x.

If you want to specify who gets the execute permission, you can use:

  • u for the user (owner).
  • g for the group.
  • o for others.
  • a for all (default).
$ chmod u+x script.sh
$ chmod g+x script.sh
$ chmod o+x script.sh
$ chmod +x script.sh

Change Ownership (chown):

The chown command in Linux is used to change the owner and/or group of a file or directory. The syntax is:

chown owner:group filename

To change only the owner of a file, use the following syntax:

chown newowner filename

Example: Change the owner of file.txt to alice:

$ chown alice file.txt

Change the Group of a File

To change only the group of a file, use the following syntax:

chown :newgroup filename

Example: Change the group of file.txt to developers:

chown :developers file.txt

Change Both Owner and Group

chown alice:developers file.txt

To change the owner and/or group of a directory and all its contents recursively, use the -R option:

chown -R newowner:newgroup directory

Change Owner Using UID and GID: You can use numeric UID (User ID) and GID (Group ID) instead of names:

chown UID:GID filename

Note: chgrp changes only the group.

chgrp developers report.txt

Chapter 4: Basic Linux Commands

1. Navigating the File System

These commands help you move around and explore the file system.

Print Working Directory (pwd):

$ pwd
/home/username

List Files and Directories (ls):

$ ls

Options:

  • -l: Long listing format.
  • -a: Show hidden files (files starting with .).
  • -h: Human-readable file sizes.

Change Directory (cd):

cd /path/to/directory

2. File and Directory Operations

These commands help you create, copy, move, and delete files and directories.

Create a Directory (mkdir):

mkdir dirname

Create a File (touch):

touch filename

Copy Files or Directories (cp):

cp source destination

Move or Rename Files or Directories (mv):

mv source destination

Delete Files or Directories (rm):

rm filename

Options:

  • -r: Recursively delete directories.
  • -f: Force deletion (no confirmation).

3. Viewing and Editing Files

These commands help you view and edit file contents.

View File Contents (cat):

cat filename

View File Contents Page by Page (less):

less filename

Edit Files (nano):

nano filename

4. File Permissions and Ownership

Refer previous chapter.

5. Searching and Finding Files

These commands help you search for files and text within files.

Find Files (find):

Searches for files in a directory hierarchy.

find /path/to/search -name "filename"

Search Text in Files (grep):

grep "pattern" filename

6. System Information

These commands provide information about the system.

Display System Information (uname):

$ uname -a
Linux hostname 5.15.0-83-generic #92-Ubuntu SMP Tue Oct 3 09:00:00 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Display Disk Usage (df):

$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 20G 30G 40% /

Display Memory Usage (free):

$ free -h
total used free shared buff/cache available
Mem: 7.7G 2.1G 4.9G 200M 700M 5.2G
Swap: 2.0G 0B 2.0G

Chapter 5: Shell Scripting

1. What is a Shell Script?

A shell script is a text file containing a series of commands that are executed by the shell (e.g., Bash). It allows you to automate repetitive tasks, combine multiple commands, and create custom workflows.

2. Writing Your First Shell Script

Let’s create a simple shell script that prints “Hello, World!” to the terminal.

Step 1: Create the Script File

Open a terminal and create a new file:

nano hello.sh

Step 2: Add the Script Content

Add the following lines to the file:

#!/bin/bash
# This is a comment
echo "Hello, World!"
  • #!/bin/bash: This is called a shebang. It tells the system to use the Bash shell to execute the script.
  • # This is a comment: Comments start with # and are ignored by the shell.
  • echo "Hello, World!": The echo command prints text to the terminal.

Step 3: Save and Exit

  • Save the file and exit the editor (in nano, press CTRL + X, then Y, then Enter).

Step 4: Make the Script Executable

Use chmod to add execute permission:

chmod +x hello.sh

Step 5: Run the Script

$ ./hello.sh
Hello, World!

3. Variables in Shell Scripts

Variables are used to store data. In shell scripts, you don’t need to declare variables before using them.

#!/bin/bash
name="Alice"
echo "Hello, $name!"

4. User Input

You can prompt the user for input using the read command.

#!/bin/bash
echo "What is your name?"
read name
echo "Hello, $name!"

5. Conditional Statements

Conditional statements allow you to make decisions in your script. Follownig is syntax.

if [ condition1 ]; then
# Commands for condition1
elif [ condition2 ]; then
# Commands for condition2
else
# Commands if none of the above conditions are true
fi

Following is one simple example.

#!/bin/bash
echo "Enter a number:"
read number

if [ $number -gt 10 ]; then
echo "The number is greater than 10."
else
echo "The number is 10 or less."
fi

Following are possible conditions

  1. File Tests:

These conditions check various properties of files and directories. They use the test command or the equivalent [ ] (with spaces around the brackets).

2. String Comparisons:

These conditions compare strings. Use = or == for equality, != for inequality. It's crucial to quote your variables to prevent word splitting and globbing issues.

3. Numeric Comparisons:

These conditions compare numbers. Use the following operators:

4. Combined Conditions:

You can combine multiple conditions using logical AND (&&) and logical OR (||).

5. Command Exit Status:

Any command’s exit status can be used as a condition. A successful command returns 0 (true), while a failed command returns a non-zero value (false).

if grep "pattern" file.txt; then  # If grep finds the pattern (exit status 0)
echo "Pattern found!"
fi

if ! grep "pattern" file.txt; then # If grep *doesn't* find the pattern (non-zero exit status)
echo "Pattern not found!"
fi

6. Using [[ ]] (Bash Specific):

Bash provides the [[ ]] construct, which offers some additional features compared to [ ]:

  • No word splitting or globbing: You don’t need to quote variables inside [[ ]].
  • More string comparison operators: [[ "$str" == "pattern" ]] allows for pattern matching.
  • Regular expression matching: [[ "$str" =~ "regex" ]] allows for regular expression matching.
if [[ "$name" == "J*" ]]; then  # Matches names starting with J
echo "Name starts with J"
fi

if [[ "$text" =~ ^[0-9]+$ ]]; then # Checks if text contains only digits
echo "Text contains only digits"
fi

6. Loops

Loops allow you to repeat commands.

Example: For Loop

#!/bin/bash
for i in 1 2 3 4 5; do
echo "Number: $i"
done

Example: While Loop

#!/bin/bash
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
count=$((count + 1))
done

7. Functions

Functions allow you to group commands into reusable blocks.

#!/bin/bash
greet() {
echo "Hello, $1!"
}

greet "Alice"
greet "Bob"

8. Command-Line Arguments

You can pass arguments to a script when you run it.

#!/bin/bash
echo "First argument: $1"
echo "Second argument: $2"
$ ./script.sh Alice Bob

Chapter 6: Advanced Shell Scripting

1. Arrays

Arrays allow you to store multiple values in a single variable.

Example: Using Arrays

#!/bin/bash
fruits=("Apple" "Banana" "Cherry")

# Access elements
echo "First fruit: ${fruits[0]}"

# Loop through the array
for fruit in "${fruits[@]}"; do
echo "Fruit: $fruit"
done

2. String Manipulation

Shell scripting provides powerful tools for manipulating strings.

Example: String Length

#!/bin/bash
text="Hello, World!"
echo "Length: ${#text}"

Example: Substring Extraction

#!/bin/bash
text="Hello, World!"
echo "Substring: ${text:0:5}"

Example: String Replacement

#!/bin/bash
text="Hello, World!"
echo "Replaced: ${text/World/Linux}"

3. Error Handling

You can handle errors in your script using exit codes and traps.

Example: Exit Codes

#!/bin/bash
ls /nonexistent
if [ $? -ne 0 ]; then
echo "Command failed with exit code $?"
fi

Example: Trap for Cleanup

#!/bin/bash
trap "echo 'Script interrupted'; exit 1" SIGINT

echo "Press Ctrl+C to interrupt"
sleep 10
echo "Script completed"

4. File Handling

You can read from and write to files in your script.

Example: Reading a File

#!/bin/bash
while IFS= read -r line; do
echo "Line: $line"
done < "file.txt"

Example: Writing to a File

#!/bin/bash
echo "Hello, File!" > output.txt

5. Advanced Loops

You can use loops to process files, directories, and command output.

Example: Processing Files in a Directory

#!/bin/bash
for file in /path/to/directory/*; do
echo "Processing $file"
done

Example: Using while with Command Output

#!/bin/bash
ls | while read file; do
echo "File: $file"
done

6. Functions with Return Values

Functions can return values using return or by echoing the result.

Example: Function with Return

#!/bin/bash
add() {
return $(($1 + $2))
}

add 5 10
echo "Sum: $?"

Example: Function with Echo

#!/bin/bash
add() {
echo $(($1 + $2))
}

result=$(add 5 10)
echo "Sum: $result"

7. Command Substitution

You can capture the output of a command and use it in your script.

Example: Command Substitution

#!/bin/bash
current_date=$(date)
echo "Today is $current_date"

8. Advanced Conditional Statements

You can use advanced conditions like case statements and logical operators.

#!/bin/bash
echo "Enter a fruit:"
read fruit

case $fruit in
"Apple")
echo "You chose Apple."
;;
"Banana")
echo "You chose Banana."
;;
*)
echo "Unknown fruit."
;;
esac

Example: Logical Operators

#!/bin/bash
if [ $1 -gt 10 ] && [ $1 -lt 20 ]; then
echo "The number is between 10 and 20."
fi

9. How do I check if a variable is empty?

Use the -z flag:

if [ -z "$var" ]; then
echo "Variable is empty."
fi

10. How do I concatenate strings?

Use double quotes:

str1="Hello"
str2="World"
result="$str1, $str2!"
echo "$result"

11. How do I pass an array to a function?

Pass the array elements and reconstruct it inside the function:

my_function() {
local arr=("$@")
echo "First element: ${arr[0]}"
}

my_array=("Apple" "Banana" "Cherry")
my_function "${my_array[@]}"

12. $ symbol

The $ symbol in shell scripting is used to access the value of a variable. However, depending on the context and the additional characters used with $, it can have different meanings and functionalities.

  1. Basic Variable Access
name="Alice"
echo $name

2. Special Variables

Shell scripting provides special variables that are prefixed with $. These variables hold useful information about the script or the system.

#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"
echo "Number of arguments: $#"
echo "All arguments: $@"

3. Accessing Array Elements

Arrays are accessed using the ${array[index]} syntax.

fruits=("Apple" "Banana" "Cherry")
echo "First fruit: ${fruits[0]}"

4. String Manipulation

The $ symbol is also used in string manipulation operations.

text="Hello, World!"
echo "Length: ${#text}"
echo "Substring: ${text:0:5}"
echo "Replaced: ${text/World/Linux}"

5. Command Substitution

You can capture the output of a command and store it in a variable using $(command) or backticks (`command`).

current_date=$(date)
echo "Today is $current_date"

6. Arithmetic Operations

The $((...)) syntax is used for arithmetic operations.

a=5
b=10
sum=$((a + b))
echo "Sum: $sum"

7. Parameter Expansion

Parameter expansion allows you to manipulate variables in various ways.

Example: Default Value

name=""
echo "Hello, ${name:-Guest}"

Example: Remove Suffix

filename="file.txt"
echo "Base name: ${filename%.txt}"

Example: Remove Prefix

filename="/path/to/file.txt"
echo "File name: ${filename##*/}"

8. Use $ in a loop?

Use $ to access loop variables:

for i in 1 2 3; do
echo "Number: $i"
done

Async programming

Non-blocking parallel function calls in a bash script can be achieved using background processes and process management tools. Following are steps to write async programming in shell script.

  1. Using & for Backgrounding:

The simplest way to run a function in the background is to append & to the function call:

my_function_1 &
my_function_2 &
my_function_3 &

# Optionally wait for all background processes to finish
wait
  • &: This operator puts the command (in this case, the function call) into the background, allowing the script to continue executing other commands without waiting for the function to complete.
  • wait: This command is crucial. It pauses the main script's execution until all background processes have finished. Without wait, the script might exit before the background functions have a chance to complete their work.

Example:

function process_data {
input="$1"
echo "Processing: $input"
sleep $((RANDOM % 5)) # Simulate some work
echo "Finished processing: $input"
}

process_data "file1.txt" &
process_data "file2.txt" &
process_data "file3.txt" &

echo "Main script continues..."

wait

echo "All processing complete."

2. Capturing Output and Exit Codes:

When running functions in the background, you’ll often want to capture their output and check for errors. Here’s how:

function process_data {
input="$1"
if [[ "$input" == "error" ]]; then
return 1 # Indicate an error
fi
echo "Processing: $input"
sleep $((RANDOM % 5))
echo "Finished processing: $input"
return 0 # Success
}

output1=$(process_data "file1.txt" &) # Capture output (won't work as expected with &)
pid1=$! # Get the Process ID of the last backgrounded command

output2=$(process_data "file2.txt" &)
pid2=$!

process_data "error" &
pid3=$!

wait pid1 pid2 pid3 # Wait specifically for the background processes

# Capture output and exit codes (better approach)
process_data "file1.txt" > "output1.txt" 2>&1 & pid1=$!
process_data "file2.txt" > "output2.txt" 2>&1 & pid2=$!
process_data "error" > "error.txt" 2>&1 & pid3=$!

wait

if [[ $? -eq 0 ]]; then
echo "All background tasks finished successfully."
else
echo "Some background tasks failed."
fi

# Check exit codes individually (more robust)
wait pid1; rc1=$?
wait pid2; rc2=$?
wait pid3; rc3=$?

if [[ $rc1 -eq 0 && $rc2 -eq 0 && $rc3 -eq 0 ]]; then
echo "All processes completed successfully"
else
echo "Process 1 exit code: $rc1"
echo "Process 2 exit code: $rc2"
echo "Process 3 exit code: $rc3"
fi
  • pid=$!: This is essential! It captures the Process ID (PID) of the most recently backgrounded process. You'll need these PIDs to wait for specific processes or check their exit codes.
  • wait pid1 pid2 pid3: You can wait for specific background processes by providing their PIDs to the wait command. This is more precise than waiting for all background processes.
  • > and 2>&1: Redirect standard output (stdout) and standard error (stderr) to files. This is crucial for capturing the output of background processes. 2>&1 merges stderr into stdout.
  • wait pid; rc=$?: After waiting for a specific process, $? contains the exit code of that process. A zero exit code indicates success, while any other value indicates an error.

3. Using xargs for Parallel Execution:

If you have a list of items to process in parallel, xargs can be very helpful:

# Create a list of files
files=("file1.txt" "file2.txt" "file3.txt" "file4.txt" "file5.txt")

# Process files in parallel (up to 4 jobs at a time)
printf "%s\n" "${files[@]}" | xargs -P 4 -n 1 -I {} process_data {}

# Or using find
find . -name "*.txt" -print0 | xargs -0 -P 4 -n 1 -I {} process_data {}

Chapter 7: System Administration with Shell Scripting

1. Managing Users and Groups

Shell scripts can automate user and group management tasks.

Example: Create a User

#!/bin/bash
username="alice"
password="password123"

# Create user
sudo useradd -m -s /bin/bash "$username"

# Set password
echo "$username:$password" | sudo chpasswd

echo "User $username created."

Example: Delete a User

#!/bin/bash
username="alice"

# Delete user
sudo userdel -r "$username"

echo "User $username deleted."

2. Monitoring System Resources

You can use shell scripts to monitor CPU, memory, and disk usage.

Example: Monitor CPU Usage

#!/bin/bash
cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')
echo "CPU Usage: $cpu_usage%"

Example: Monitor Memory Usage

#!/bin/bash
memory_usage=$(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2 }')
echo "Memory Usage: $memory_usage"

Example: Monitor Disk Usage

#!/bin/bash
disk_usage=$(df -h / | awk 'NR==2{print $5}')
echo "Disk Usage: $disk_usage"

3. Automating Backups

Shell scripts can automate the backup process for files and directories.

#!/bin/bash
backup_dir="/path/to/backup"
source_dir="/path/to/source"
timestamp=$(date +"%Y%m%d_%H%M%S")

# Create backup directory
mkdir -p "$backup_dir"

# Create backup
tar -czf "$backup_dir/backup_$timestamp.tar.gz" "$source_dir"

echo "Backup created: $backup_dir/backup_$timestamp.tar.gz"

4. Log Rotation

You can use shell scripts to manage log files and prevent them from growing too large.

#!/bin/bash
log_file="/var/log/myapp.log"
max_size=1048576 # 1MB

# Check log file size
if [ $(stat -c%s "$log_file") -gt $max_size ]; then
mv "$log_file" "$log_file.old"
touch "$log_file"
echo "Log file rotated."
fi

5. Scheduling Tasks with Cron

Cron is a time-based job scheduler in Linux. You can use shell scripts with cron to automate tasks.

Open the crontab editor:

crontab -e

Add a cron job to run the backup script daily at 2 AM:

0 2 * * * /path/to/backup_script.sh

6. Network Configuration

Shell scripts can automate network configuration tasks.

#!/bin/bash
interface="eth0"
ip_address="192.168.1.100"
netmask="255.255.255.0"
gateway="192.168.1.1"

# Configure network interface
sudo ifconfig "$interface" "$ip_address" netmask "$netmask"
sudo route add default gw "$gateway" "$interface"

echo "Network configured."

7. System Updates and Maintenance

You can use shell scripts to automate system updates and maintenance tasks.

#!/bin/bash
sudo apt update
sudo apt upgrade -y
sudo apt autoremove -y

echo "System updated."

This post is based on interaction with https://chat.deepseek.com/.

Happy learning :-)

--

--

Dilip Kumar
Dilip Kumar

Written by Dilip Kumar

With 18+ years of experience as a software engineer. Enjoy teaching, writing, leading team. Last 4+ years, working at Google as a backend Software Engineer.

No responses yet