Introduction to Bash
Bash (Bourne Again SHell) is a Unix shell and command-line interpreter that serves as the default shell on most Linux distributions and macOS. It provides a powerful interface for interacting with the operating system, automating tasks, and managing files and processes.
Key Concepts
- Commands: Programs or built-in functions that perform specific tasks
- Arguments: Inputs passed to commands to modify their behavior
- Options: Flags that change how commands work (usually start with
-or--) - Pipes: Connect commands together using
| - Redirection: Control input/output streams using
>,>>,< - Variables: Store data for later use
- Scripts: Files containing sequences of commands
1. Basic Command Structure
# Basic syntax: command [options] [arguments] ls -la /home/user # Command with long options ls --all --long /home/user # Multiple commands on one line (separated by ;) cd /tmp; ls -la; pwd # Conditional execution mkdir newdir && cd newdir # Only cd if mkdir succeeds rm file || echo "Failed" # Echo only if rm fails # Command substitution current_date=$(date) echo "Today is $current_date" # Process substitution diff <(ls dir1) <(ls dir2)
2. File System Navigation
Directory Navigation Commands
# pwd - Print Working Directory pwd # Output: /home/username # ls - List Directory Contents ls # List current directory ls -l # Long format with details ls -a # Show all files (including hidden) ls -la # Long format with all files ls -lh # Human-readable sizes ls -lt # Sort by modification time ls -ltr # Reverse time sort ls -R # Recursive listing ls *.txt # List only .txt files # cd - Change Directory cd # Go to home directory cd /var/log # Go to absolute path cd .. # Go up one level cd ../.. # Go up two levels cd - # Go to previous directory cd ~ # Go to home directory cd ~/Documents # Go to Documents in home # tree - Display directory tree (may need installation) tree # Show directory tree tree -L 2 # Limit to 2 levels deep tree -d # Show only directories # pushd/popd - Directory stack pushd /tmp # Save current dir and go to /tmp pushd /var/log # Save and go to /var/log dirs # Show directory stack popd # Return to previous directory popd # Return to original directory
Path Navigation Shortcuts
# Special path characters
. # Current directory
.. # Parent directory
~ # Home directory
- # Previous directory
# Path completion examples
cd /u/lo/b<TAB> # Tab completion expands to /usr/local/bin
# Using wildcards
ls *.txt # All .txt files
ls file?.txt # file1.txt, file2.txt, etc.
ls file[0-9].txt # file0.txt through file9.txt
ls file[a-z].txt # filea.txt through filez.txt
ls {file1,file2}.txt # file1.txt and file2.txt
ls *.{txt,pdf} # All .txt and .pdf files
3. File Operations
Creating and Deleting Files
# touch - Create empty file or update timestamp
touch file.txt # Create empty file
touch -t 202403101200 file # Set specific timestamp
# mkdir - Create directories
mkdir newdir # Create single directory
mkdir -p path/to/nested/dir # Create parent directories as needed
mkdir -m 755 newdir # Create with specific permissions
mkdir dir{1..5} # Create dir1, dir2, dir3, dir4, dir5
# rm - Remove files/directories
rm file.txt # Remove file
rm -i file.txt # Interactive (ask before remove)
rm -f file.txt # Force remove (no prompt)
rm -r directory # Remove directory recursively
rm -rf directory # Force remove directory recursively (careful!)
rm *.tmp # Remove all .tmp files
# rmdir - Remove empty directories
rmdir emptydir # Remove empty directory
rmdir -p path/to/emptydir # Remove nested empty directories
# ln - Create links
ln -s target linkname # Create symbolic link
ln target linkname # Create hard link
Copying and Moving Files
# cp - Copy files/directories cp file.txt copy.txt # Copy file cp -i file.txt copy.txt # Interactive (prompt before overwrite) cp -v file.txt copy.txt # Verbose output cp -r sourcedir destdir # Copy directory recursively cp -a sourcedir destdir # Archive mode (preserve attributes) cp file1.txt file2.txt dir/ # Copy multiple files to directory cp -u source dest # Update (copy only when source is newer) # mv - Move/rename files mv oldname.txt newname.txt # Rename file mv file.txt dir/ # Move file to directory mv -i file.txt dir/ # Interactive move mv -v *.txt backup/ # Move multiple files mv sourcedir/* destdir/ # Move contents of directory mv dir1 dir2 # Rename directory # install - Copy and set attributes install -m 755 script.sh /usr/local/bin/ # Copy with permissions install -d /path/to/new/dir # Create directory
Viewing File Contents
# cat - Concatenate and display files cat file.txt # Display file contents cat file1.txt file2.txt # Display multiple files cat -n file.txt # Show line numbers cat > file.txt # Create file from keyboard input (Ctrl+D to end) # less/more - Page through files less largefile.txt # View file page by page more largefile.txt # Simpler pager # In less: /search, n/N next/prev match, q quit, g/G top/bottom # head - Display first lines head file.txt # First 10 lines head -n 20 file.txt # First 20 lines head -c 100 file.txt # First 100 bytes # tail - Display last lines tail file.txt # Last 10 lines tail -n 20 file.txt # Last 20 lines tail -f logfile.txt # Follow file as it grows tail -f -n 50 logfile.txt # Last 50 lines and follow # nl - Number lines nl file.txt # Display with line numbers nl -b a file.txt # Number all lines nl -w 4 file.txt # Set width of line numbers # od - Octal dump (view binary files) od -x file.bin # Hexadecimal dump od -c file.bin # Character dump # xxd - Hex dump xxd file.bin # Hexadecimal view xxd -r hex.txt > binary.bin # Convert hex back to binary
4. Text Processing
Searching Text
# grep - Search text patterns grep "pattern" file.txt # Search for pattern grep -i "pattern" file.txt # Case-insensitive grep -r "pattern" ./ # Recursive search grep -l "pattern" *.txt # List files with match grep -c "pattern" file.txt # Count matches grep -v "pattern" file.txt # Invert match (lines without pattern) grep -w "word" file.txt # Match whole word only grep -n "pattern" file.txt # Show line numbers grep -A 2 "pattern" file.txt # Show 2 lines after match grep -B 2 "pattern" file.txt # Show 2 lines before match grep -C 2 "pattern" file.txt # Show 2 lines before and after grep -E "pattern1|pattern2" file.txt # Extended regex (OR) grep -e "pattern1" -e "pattern2" file.txt # Multiple patterns # egrep/fgrep - Variants (egrep = grep -E, fgrep = grep -F) egrep "pattern1|pattern2" file.txt # Extended regex fgrep "literal*string" file.txt # Fixed strings (no regex) # ag/silver searcher - Faster alternative ag "pattern" ./ # Fast recursive search
Cutting and Sorting
# cut - Remove sections from lines cut -d':' -f1 /etc/passwd # First field, delimited by : cut -c1-10 file.txt # First 10 characters of each line cut -f1,3 -d',' data.csv # Fields 1 and 3 from CSV # sort - Sort lines sort file.txt # Sort alphabetically sort -n file.txt # Sort numerically sort -r file.txt # Reverse sort sort -u file.txt # Unique sort (remove duplicates) sort -k2 -t',' data.csv # Sort by second field sort -h file.txt # Human-numeric sort (2K, 1G) sort -M file.txt # Sort by month name # uniq - Report or omit repeated lines uniq file.txt # Remove consecutive duplicates uniq -c file.txt # Count occurrences uniq -d file.txt # Only show duplicates uniq -u file.txt # Only show unique lines sort file.txt | uniq # Remove all duplicates # wc - Word count wc file.txt # Lines, words, characters wc -l file.txt # Lines only wc -w file.txt # Words only wc -c file.txt # Bytes only wc -m file.txt # Characters only
Text Transformation
# tr - Translate characters
tr 'a-z' 'A-Z' < file.txt # Convert to uppercase
tr -d ' ' < file.txt # Delete spaces
tr -s ' ' < file.txt # Squeeze repeated spaces
tr '\n' ' ' < file.txt # Replace newlines with spaces
tr -cd 'a-zA-Z0-9' < file.txt # Keep only alphanumeric
# sed - Stream editor
sed 's/old/new/' file.txt # Replace first occurrence per line
sed 's/old/new/g' file.txt # Replace all occurrences
sed -i 's/old/new/g' file.txt # Edit file in-place
sed '/pattern/d' file.txt # Delete lines matching pattern
sed '1,10d' file.txt # Delete lines 1-10
sed -n '5,10p' file.txt # Print lines 5-10
sed 's/^/ /' file.txt # Indent each line
sed 's/$/;/' file.txt # Add semicolon at end of lines
sed 's/ */ /g' file.txt # Replace multiple spaces with one
# awk - Pattern scanning and processing
awk '{print $1}' file.txt # Print first field
awk '/pattern/' file.txt # Print lines matching pattern
awk -F':' '{print $1}' /etc/passwd # Use custom delimiter
awk '{sum+=$1} END {print sum}' file.txt # Sum first column
awk 'NR>1 && NR<10' file.txt # Print lines 2-9
awk '{printf "%s\t%s\n", $1, $2}' file.txt # Formatted output
# paste - Merge lines of files
paste file1.txt file2.txt # Merge side by side
paste -d',' file1.txt file2.txt # Use comma delimiter
paste -s file.txt # Merge all lines into one
# join - Join lines of two files on common field
join -t',' -1 1 -2 1 file1.csv file2.csv # Join on first field
Comparing Files
# diff - Compare files line by line diff file1.txt file2.txt # Show differences diff -u file1.txt file2.txt # Unified format diff -c file1.txt file2.txt # Context format diff -r dir1 dir2 # Compare directories recursively diff -q file1.txt file2.txt # Only indicate if different diff -y file1.txt file2.txt # Side-by-side comparison # cmp - Compare byte by byte cmp file1.bin file2.bin # First difference cmp -l file1.bin file2.bin # All differences with byte numbers # comm - Compare sorted files line by line comm file1.txt file2.txt # 3-column output (file1 only, file2 only, common) comm -12 file1.txt file2.txt # Show only common lines # patch - Apply diff to file patch file.txt < changes.patch # Apply patch patch -R file.txt < changes.patch # Reverse patch
5. File Permissions and Ownership
Permission Commands
# chmod - Change file mode (permissions) chmod 755 script.sh # rwxr-xr-x chmod u+x script.sh # Add execute for user chmod g-w file.txt # Remove write for group chmod o+r file.txt # Add read for others chmod a+x script.sh # Add execute for all chmod -R 755 directory/ # Recursive change # chown - Change file owner chown user file.txt # Change owner chown user:group file.txt # Change owner and group chown :group file.txt # Change group only chown -R user:group directory/ # Recursive change # chgrp - Change group ownership chgrp group file.txt # Change group chgrp -R group directory/ # Recursive change # umask - Set default permissions umask # Show current mask umask 022 # Set mask (files: 644, dirs: 755) umask 077 # Set mask (files: 600, dirs: 700) # ls with permissions ls -l file.txt # Show permissions ls -ld directory/ # Show directory permissions getfacl file.txt # Get ACL (Access Control List) setfacl -m u:user:rw file.txt # Set ACL
Understanding Permission Notation
# Permission types r (4) - Read w (2) - Write x (1) - Execute - (0) - No permission # Permission groups u - User (owner) g - Group o - Others a - All # Examples -rwxr-xr-x 1 user group 1024 Mar 10 10:00 script.sh # -: regular file # rwx: user can read, write, execute # r-x: group can read, execute # r-x: others can read, execute # Numeric notation chmod 644 file.txt # rw-r--r-- (6=4+2 for user, 4 for group, 4 for others) chmod 755 file.txt # rwxr-xr-x (7=4+2+1 for user, 5=4+1 for group/others) chmod 700 file.txt # rwx------ (7 for user, 0 for group, 0 for others)
6. Process Management
Viewing Processes
# ps - Process status ps # Current shell processes ps aux # All processes with details ps -ef # Full format listing ps -u username # Processes for user ps -C command # Processes by command name ps -p PID # Process by PID ps -eo pid,user,command --sort=-pcpu # Custom format sorted by CPU # top - Dynamic process viewer top # Interactive process viewer htop # Enhanced top (may need install) # In top: P (CPU sort), M (memory sort), k (kill), q (quit) # pgrep - Find processes by name pgrep bash # Find bash process IDs pgrep -u user bash # Find user's bash processes pgrep -l bash # Show PID and name # pstree - Show process tree pstree # Tree view of processes pstree -p # Show PIDs pstree username # Processes for user
Managing Processes
# kill - Terminate processes kill PID # Terminate process kill -9 PID # Force kill (SIGKILL) kill -15 PID # Graceful termination (SIGTERM) kill -l # List all signals killall process_name # Kill all processes by name pkill process_name # Kill processes by pattern # bg/fg/jobs - Job control command & # Run in background Ctrl+Z # Suspend foreground job jobs # List jobs bg %1 # Resume job 1 in background fg %1 # Bring job 1 to foreground disown %1 # Remove job from shell control # nice/renice - Process priority nice -n 10 command # Run with lower priority renice -n 5 -p PID # Change priority of running process
System Monitoring
# uptime - System load uptime # How long system has been running # w - Who's logged in w # Show logged-in users # who - Show who is logged in who # List logged-in users who -b # Last boot time who -r # Current runlevel # free - Memory usage free -h # Human-readable memory info free -m # Memory in MB free -s 5 # Update every 5 seconds # vmstat - Virtual memory statistics vmstat 2 10 # Stats every 2 seconds, 10 times # iostat - I/O statistics iostat # CPU and I/O stats iostat -x 2 # Extended stats every 2 seconds # netstat - Network statistics netstat -tulpn # Listening ports netstat -an # All connections ss -tulpn # Modern alternative to netstat # lsof - List open files lsof # All open files lsof -u username # Files opened by user lsof -i :8080 # Processes using port 8080 lsof -p PID # Files opened by process
7. Input/Output Redirection
Basic Redirection
# Standard streams 0 - stdin (standard input) 1 - stdout (standard output) 2 - stderr (standard error) # Output redirection command > file.txt # Redirect stdout to file (overwrite) command >> file.txt # Redirect stdout to file (append) command 2> error.txt # Redirect stderr to file command 2>> error.txt # Append stderr to file command > file.txt 2>&1 # Redirect both stdout and stderr command &> file.txt # Same as above (bash) command > file.txt 2> /dev/null # Stdout to file, stderr to null # Input redirection command < input.txt # Read stdin from file command << EOF # Here document > line1 > line2 > EOF # Here string command <<< "string" # Pass string as stdin
Pipes and Process Substitution
# Pipes - Connect commands command1 | command2 # Pass output of command1 to command2 command1 | command2 | command3 # Chain multiple commands # Common pipe examples ls -la | grep "^d" # List only directories ps aux | grep firefox # Find firefox processes cat file.txt | sort | uniq # Sort and remove duplicates tail -f log.txt | grep error # Monitor log for errors # Named pipes (FIFO) mkfifo mypipe # Create named pipe command1 > mypipe & # Write to pipe in background command2 < mypipe # Read from pipe # Process substitution diff <(ls dir1) <(ls dir2) # Compare directory listings while read line; do echo $line; done < <(command)
Advanced Redirection
# Redirect to multiple files command | tee file.txt # Output to file and stdout command | tee -a file.txt # Append to file command | tee file1.txt file2.txt # Write to multiple files command 2>&1 | tee file.txt # Capture stdout and stderr # Redirect to both stdout and stderr command | tee /dev/stderr # Output to stderr as well # Using /dev/null command > /dev/null # Discard stdout command 2> /dev/null # Discard stderr command &> /dev/null # Discard all output # File descriptors exec 3> file.txt # Open file descriptor 3 for writing echo "Hello" >&3 # Write to fd 3 exec 3>&- # Close fd 3 exec 4< file.txt # Open file descriptor 4 for reading cat <&4 # Read from fd 4 exec 4<&- # Close fd 4
8. Variables and Environment
Shell Variables
# Variable assignment
name="John" # No spaces around =
age=30
echo $name # Reference variable
echo ${name} # Same, but allows concatenation
# Variable types
readonly var="constant" # Read-only variable
declare -i number=42 # Integer variable
declare -r constant="fixed" # Read-only (same as readonly)
declare -l lowercase="HELLO" # Auto-lowercase
declare -u uppercase="hello" # Auto-uppercase
# Array variables
array=(one two three) # Indexed array
echo ${array[0]} # First element
echo ${array[@]} # All elements
echo ${#array[@]} # Array length
array+=(four) # Append element
# Associative arrays (declare -A)
declare -A user
user[name]="John"
user[age]=30
echo ${user[name]}
# Special variables
$0 - Script name
$1, $2, ... - Positional parameters
$# - Number of arguments
$@ - All arguments as separate words
$* - All arguments as single word
$? - Exit status of last command
$$ - Current shell PID
$! - PID of last background command
$- - Current shell options
$_ - Last argument of previous command
Environment Variables
# Environment variables export PATH=$PATH:/usr/local/bin # Add to PATH export EDITOR=vim # Set default editor export LANG=en_US.UTF-8 # Set locale # Common environment variables HOME - User home directory USER - Current username SHELL - Current shell PWD - Current working directory OLDPWD - Previous working directory PATH - Command search path TERM - Terminal type LANG - Language/locale # View environment env # Show all environment variables printenv # Same as env printenv PATH # Show specific variable set # Show all variables and functions # Set for a single command LANG=C command # Run command with specific locale PATH=/custom/path command # Run with custom PATH # Remove variable unset VAR # Remove variable
9. Shell Scripting Basics
Script Structure
#!/bin/bash
# Shebang line - tells system which interpreter to use
# Comments start with #
# Good practice to include description
# Script: example.sh
# Purpose: Demonstrate bash scripting
# Author: User
# Date: 2024-03-10
# Exit on error
set -e # Exit on any error
set -u # Exit on undefined variable
set -o pipefail # Pipe failures cause exit
# Debugging
set -x # Print commands as they execute
set -v # Print input lines as read
# Functions
greet() {
local name=$1 # Local variable
echo "Hello, $name!"
}
# Main script
main() {
greet "World"
}
# Call main if script is executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi
Conditional Statements
# if-then-else if [ condition ]; then # commands elif [ another condition ]; then # commands else # commands fi # Test conditions [ -f file ] # True if file exists and is regular file [ -d dir ] # True if directory exists [ -e file ] # True if file exists [ -r file ] # True if file is readable [ -w file ] # True if file is writable [ -x file ] # True if file is executable [ -s file ] # True if file size > 0 [ -L file ] # True if file is symbolic link [ -z string ] # True if string is empty [ -n string ] # True if string is not empty [ string1 = string2 ] # String equality [ string1 != string2 ] # String inequality [ num1 -eq num2 ] # Numeric equality [ num1 -ne num2 ] # Numeric not equal [ num1 -gt num2 ] # Numeric greater than [ num1 -ge num2 ] # Numeric greater than or equal [ num1 -lt num2 ] # Numeric less than [ num1 -le num2 ] # Numeric less than or equal # Combining conditions [ condition1 -a condition2 ] # AND [ condition1 -o condition2 ] # OR [ ! condition ] # NOT # Double brackets (bash-specific, more features) [[ -f file && -r file ]] # AND [[ -f file || -d file ]] # OR [[ string == pattern ]] # Pattern matching [[ string =~ regex ]] # Regex matching
Loops
# for loop
for i in 1 2 3 4 5; do
echo "Number: $i"
done
# for loop with range
for i in {1..5}; do
echo "Number: $i"
done
# for loop with step
for i in {1..10..2}; do
echo "Odd: $i"
done
# C-style for loop
for ((i=0; i<5; i++)); do
echo "i = $i"
done
# for loop with files
for file in *.txt; do
echo "Processing: $file"
done
# while loop
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
((count++))
done
# until loop
count=1
until [ $count -gt 5 ]; do
echo "Count: $count"
((count++))
done
# Reading file line by line
while IFS= read -r line; do
echo "Line: $line"
done < file.txt
# Loop control
break # Exit loop
continue # Skip to next iteration
Functions
# Function definition
function myfunc {
echo "Function called"
}
# Alternative syntax
myfunc() {
echo "Function called"
}
# Function with parameters
greet() {
local name=$1
local greeting=${2:-Hello} # Default value
echo "$greeting, $name!"
}
# Function returning value (via echo)
get_name() {
echo "John"
}
name=$(get_name) # Capture output
# Function returning exit status
check_file() {
if [ -f "$1" ]; then
return 0 # Success
else
return 1 # Failure
fi
}
if check_file "test.txt"; then
echo "File exists"
fi
# Variable scope
var="global"
test_scope() {
local var="local" # Local variable
echo "Inside: $var"
}
test_scope
echo "Outside: $var" # Still "global"
10. Advanced Commands
Text Processing Tools
# find - Search for files
find . -name "*.txt" # Find by name
find . -type f -size +1M # Files > 1MB
find . -mtime -7 # Modified in last 7 days
find . -user username # Owned by user
find . -perm 755 # With specific permissions
find . -exec rm {} \; # Execute command on each
find . -print0 | xargs -0 command # Handle spaces in filenames
# xargs - Build and execute command lines
find . -name "*.txt" | xargs rm # Remove all .txt files
find . -name "*.log" | xargs grep "error" # Search in log files
cat files.txt | xargs -n 1 echo # One argument per line
seq 1 10 | xargs -P 4 -I {} ./script {} # Parallel execution
# locate - Fast file search (requires updatedb)
locate file.txt # Find file by name
locate -i "*.pdf" # Case-insensitive pattern
# which - Locate a command
which bash # Show path to bash
which -a ls # Show all matches
# type - Show command type
type ls # ls is aliased to 'ls --color=auto'
type -a echo # All locations of echo
Archive and Compression
# tar - Tape archive tar -cf archive.tar file1 file2 # Create archive tar -xf archive.tar # Extract archive tar -czf archive.tar.gz directory/ # Create gzipped archive tar -xzf archive.tar.gz # Extract gzipped archive tar -cjf archive.tar.bz2 directory/ # Create bzipped archive tar -tvf archive.tar # List contents tar -uf archive.tar newfile # Update archive # gzip/gunzip gzip file.txt # Compress file gunzip file.txt.gz # Decompress gzip -l file.txt.gz # List compression info zcat file.txt.gz # View compressed file # bzip2/bunzip2 bzip2 file.txt # Better compression bunzip2 file.txt.bz2 # Decompress bzcat file.txt.bz2 # View compressed # zip/unzip zip archive.zip file1 file2 # Create zip unzip archive.zip # Extract zip unzip -l archive.zip # List contents zip -r archive.zip directory/ # Zip directory # 7z (p7zip) 7z a archive.7z file.txt # Add to 7z archive 7z x archive.7z # Extract 7z
Networking Commands
# ping - Test network connectivity ping google.com # Ping host continuously ping -c 4 google.com # Ping 4 times ping -i 2 google.com # 2 second interval # curl - Transfer data curl http://example.com # Get webpage curl -o file.html http://example.com # Save to file curl -I http://example.com # Get headers only curl -X POST -d "data" http://example.com # POST request curl -H "Header: value" http://example.com # Custom headers curl -u user:pass http://example.com # Basic auth # wget - Download files wget http://example.com/file.zip # Download file wget -c http://example.com/file.zip # Continue partial download wget -r http://example.com/ # Recursive download wget -O output.html http://example.com # Specify output name wget --limit-rate=200k http://example.com # Limit download speed # ssh - Secure Shell ssh user@host # Connect to host ssh -p 2222 user@host # Custom port ssh -i key.pem user@host # Use identity file ssh -L 8080:localhost:80 user@host # Port forwarding ssh -D 1080 user@host # SOCKS proxy # scp - Secure copy scp file.txt user@host:/path/ # Copy file to remote scp user@host:/path/file.txt . # Copy from remote scp -r directory/ user@host:/path/ # Copy directory # rsync - Remote sync rsync -av source/ user@host:/dest/ # Sync to remote rsync -avz source/ user@host:/dest/ # With compression rsync -av --delete source/ dest/ # Mirror (delete extra files) rsync -av --progress source/ dest/ # Show progress # wget - Download files wget http://example.com/file.zip # Download file wget -c http://example.com/file.zip # Continue partial download wget -r http://example.com/ # Recursive download wget -O output.html http://example.com # Specify output name wget --limit-rate=200k http://example.com # Limit download speed # ssh - Secure Shell ssh user@host # Connect to host ssh -p 2222 user@host # Custom port ssh -i key.pem user@host # Use identity file ssh -L 8080:localhost:80 user@host # Port forwarding ssh -D 1080 user@host # SOCKS proxy # scp - Secure copy scp file.txt user@host:/path/ # Copy file to remote scp user@host:/path/file.txt . # Copy from remote scp -r directory/ user@host:/path/ # Copy directory # rsync - Remote sync rsync -av source/ user@host:/dest/ # Sync to remote rsync -avz source/ user@host:/dest/ # With compression rsync -av --delete source/ dest/ # Mirror (delete extra files) rsync -av --progress source/ dest/ # Show progress
System Information
# uname - System information uname -a # All system info uname -s # Kernel name uname -r # Kernel release uname -m # Machine hardware # lsb_release - Distribution info (Linux) lsb_release -a # All distribution info lsb_release -d # Description lsb_release -c # Codename # hostname hostname # Show hostname hostname newname # Set hostname (as root) # date date # Current date/time date +"%Y-%m-%d %H:%M:%S" # Custom format date -d "yesterday" # Yesterday's date date -s "2024-03-10 12:00:00" # Set date/time (as root) # cal - Calendar cal # Current month cal 2024 # Year calendar cal 12 2024 # December 2024 # bc - Calculator echo "5+3" | bc # Basic calculation echo "scale=2; 10/3" | bc # With decimal places bc -l # Start interactive calculator
User Management
# whoami - Current username whoami # Show current user # id - User identity id # Show user and group IDs id -u # Show UID only id -g # Show GID only id username # Info for specific user # users/who - Who's logged in users # List logged-in users who # Detailed user info who -b # Last boot time # last - Login history last # Show login history last -n 10 # Last 10 logins last username # Logins for specific user # passwd - Change password passwd # Change own password passwd username # Change user's password (as root) # useradd/usermod/userdel - User management (as root) useradd newuser # Add user useradd -m -s /bin/bash newuser # Add with home and shell usermod -aG sudo username # Add user to group userdel username # Delete user userdel -r username # Delete user and home directory # groupadd/groupdel - Group management (as root) groupadd newgroup # Add group groupdel newgroup # Delete group
11. Job Control and Scheduling
Job Control
# Background and foreground command & # Run in background Ctrl+Z # Suspend foreground job jobs # List jobs bg %1 # Resume job in background fg %1 # Bring job to foreground disown %1 # Remove job from job table nohup command & # Run immune to hangups # Process priorities nice -n 10 command # Run with lower priority renice +5 -p PID # Increase niceness renice -5 -p PID # Decrease niceness (higher priority) # Screen/Tmux - Terminal multiplexers screen # Start screen session screen -S sessionname # Named session Ctrl+A d # Detach from screen screen -r # Reattach screen -ls # List sessions tmux # Start tmux session tmux new -s sessionname # New named session Ctrl+B d # Detach tmux attach -t sessionname # Attach tmux ls # List sessions
Task Scheduling
# at - One-time tasks at 14:30 # Schedule command at 14:30 > echo "Task executed" > ~/log.txt > Ctrl+D # End input atq # List pending jobs atrm job_id # Remove job # batch - Run when load is low batch # Schedule when system idle > long_running_command > Ctrl+D # cron - Recurring tasks crontab -l # List cron jobs crontab -e # Edit cron jobs crontab -r # Remove all cron jobs # Crontab format # * * * * * command # │ │ │ │ │ # │ │ │ │ └── Day of week (0-7, 0/7 = Sunday) # │ │ │ └──── Month (1-12) # │ │ └────── Day of month (1-31) # │ └──────── Hour (0-23) # └────────── Minute (0-59) # Examples 0 2 * * * /backup/script.sh # Daily at 2 AM */15 * * * * /monitor/check.sh # Every 15 minutes 0 9-17 * * 1-5 /work/script.sh # Weekdays 9 AM-5 PM hourly # systemd timers (modern alternative) systemctl list-timers # List timers systemctl enable --now timer-name # Enable timer
12. Bash Shortcuts and Productivity
Keyboard Shortcuts
# Navigation Ctrl+A # Go to beginning of line Ctrl+E # Go to end of line Alt+B # Move back one word Alt+F # Move forward one word Ctrl+XX # Toggle between current and previous position # Editing Ctrl+U # Cut from cursor to beginning Ctrl+K # Cut from cursor to end Ctrl+W # Cut word before cursor Alt+D # Cut word after cursor Ctrl+Y # Paste last cut text Ctrl+T # Swap last two characters Alt+T # Swap last two words Ctrl+_ # Undo # History Ctrl+R # Reverse search history Ctrl+G # Exit search mode Ctrl+P # Previous command Ctrl+N # Next command Alt+< # First command in history Alt+> # Last command in history !! # Repeat last command !$ # Last argument of previous command !^ # First argument of previous command !* # All arguments of previous command !string # Repeat last command starting with "string" !?string # Repeat last command containing "string" # Process control Ctrl+C # Terminate foreground process Ctrl+Z # Suspend foreground process Ctrl+D # Exit shell (EOF) Ctrl+L # Clear screen
Aliases and Functions
# Creating aliases
alias ll='ls -la' # List with details
alias ..='cd ..' # Go up one level
alias gs='git status' # Git shortcuts
alias rm='rm -i' # Interactive remove
# Permanent aliases (in ~/.bashrc)
echo "alias ll='ls -la'" >> ~/.bashrc
source ~/.bashrc
# Functions in .bashrc
mkcd() {
mkdir -p "$1" && cd "$1"
}
extract() {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar e $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
Conclusion
Bash is an incredibly powerful tool for system administration, development workflows, and automation. This guide covers the essential commands and concepts, but there's always more to learn:
Key Takeaways
- File Operations:
ls,cp,mv,rm,mkdirfor basic file management - Text Processing:
grep,sed,awk,cutfor manipulating text - Permissions:
chmod,chownfor access control - Process Management:
ps,kill,jobsfor controlling processes - Redirection:
>,>>,|for managing input/output - Scripting: Variables, conditionals, loops for automation
- System Info:
uname,df,freefor monitoring - Networking:
ssh,scp,curlfor network operations
Best Practices
- Use tab completion to save time and avoid typos
- Test commands with
-ior--interactivebefore destructive operations - Quote variables to handle spaces and special characters
- Use
set -ein scripts to exit on errors - Keep backups before using
rm -rf - Learn one command at a time and practice regularly
- Read man pages (
man command) for detailed information - Customize your environment with aliases and functions
- Use version control for important scripts
- Be careful with sudo - only use when necessary
Further Learning
man bash- Bash manualhelp- Built-in help for shell commands- Online resources: tldr.sh, explainshell.com
- Practice with real tasks and automation
- Study other shells (zsh, fish) for inspiration
Bash mastery comes with practice. Start with simple commands and gradually build up to complex scripts and automation tasks.