A comprehensive guide to commonly used Linux commands
Ctrl+Alt+T - Open terminal
Ctrl+Shift+T (in terminal) - Create new tab
Ctrl+Shift+C / Ctrl+Insert - Copy
Ctrl+Shift+V / Shift+Insert - Paste
Ctrl+Z - Undo
Ctrl+Shift+X - Cut
su
Switch user
su username # Change to another user su - username # Change to user with their environment exit # Exit from switched user
sudo
Execute command as superuser
sudo command # Run command with superuser privileges sudo -s # Start a shell as superuser sudo -i # Login as root user sudo -l # List allowed commands
passwd
Change user password
passwd # Change your password sudo passwd username # Change another user's password
adduser / useradd
Create a new user
sudo adduser username # Create a new user with interactive prompts
usermod
Modify user account
usermod -L username # Lock a user's password usermod -U username # Unlock a user's password
id
Display user identity
id # Show current user ID and groups id -un # Display only username (same as whoami)
who
Show who is logged on
who # Display logged-in users who -b # Show time of last system boot
lastlog
Show recent login information
lastlog # Show all users' last login times lastlog -b 90 # Show logins in last 90 days
chmod
Change file access permissions
chmod u+x file.txt # Add execute permission for owner chmod g+w file.txt # Add write permission for group chmod a-w file.txt # Remove write permission for all chmod 755 file.txt # Set to rwxr-xr-x (using octal)
| Octal | Binary | Permissions |
|---|---|---|
| 0 | 000 | --- |
| 1 | 001 | --x |
| 2 | 010 | -w- |
| 3 | 011 | -wx |
| 4 | 100 | r-- |
| 5 | 101 | r-x |
| 6 | 110 | rw- |
| 7 | 111 | rwx |
chown
Change file owner and group
sudo chown user file.txt # Change owner sudo chown user:group file.txt # Change owner and group sudo chown :group file.txt # Change only group
chgrp
Change group ownership
chgrp group file.txt # Change file's group
grep
Search for patterns in files
grep "pattern" file.txt # Search for pattern grep -i "pattern" file.txt # Case-insensitive search grep -r "pattern" directory/ # Recursive search grep -n "pattern" file.txt # Show line numbers grep -A2 "pattern" file.txt # Show 2 lines after match grep -B1 "pattern" file.txt # Show 1 line before match grep -C1 "pattern" file.txt # Show 1 line before and after match grep -w "word" file.txt # Match whole words only grep -c "pattern" file.txt # Count matching lines
| Pattern | Description |
|---|---|
| . | Any single character |
| ^ | Start of line |
| $ | End of line |
| [abc] | Any character from set |
| [^abc] | Any character NOT in set |
| [A-Z] | Any character in range |
| * | Zero or more occurrences |
| {n} | Exactly n occurrences |
| {n,m} | Between n and m occurrences |
sort
Sort lines of text files
sort file.txt # Sort lines alphabetically sort -r file.txt # Sort in reverse order sort -n file.txt # Sort numerically sort -u file.txt # Sort and remove duplicates sort -k2 file.txt # Sort by second column
tr
Translate or delete characters
cat file.txt | tr 'a' 'A' # Replace 'a' with 'A' cat file.txt | tr a-z A-Z # Convert to uppercase cat file.txt | tr -d ',' # Delete all commas cat file.txt | tr -d [:blank:] # Delete all spaces
sed
Stream editor for filtering and transforming text
sed 's/old/new/g' file.txt # Replace all occurrences sed -i 's/old/new/g' file.txt # Replace and update file sed '/pattern/d' file.txt # Delete lines with pattern sed '/^$/d' file.txt # Delete empty lines sed '1d' file.txt # Delete first line sed 's/\t/ /g' file.txt # Replace tabs with spaces
awk
Pattern scanning and processing language
awk '{print}' file.txt # Print all lines
awk '{print $1}' file.txt # Print first column
awk '/pattern/ {print}' file.txt # Print lines matching pattern
awk '{print NR, $0}' file.txt # Print with line numbers
awk 'NR==3, NR==6 {print}' file.txt # Print lines 3-6
find
Search for files in a directory hierarchy
find . -name "*.txt" # Find text files in current directory
find /home -user username # Find files owned by user
find . -type f -name "*.png" # Find PNG files
find . -type d # Find directories
find . -size +10M # Find files larger than 10MB
find . -mtime -7 # Find files modified in last 7 days
find . -empty # Find empty files
find . -name "*.txt" -exec cp {} {}_backup \; # Find and copy
locate
Find files by name, using database
locate filename # Find all files named "filename" locate -i pattern # Case-insensitive search locate -e pattern # Only show existing files locate -l 10 pattern # Limit to 10 results
which
Locate a command
which ls # Show full path of ls command
uname
Print system information
uname -a # Print all system information
date
Display or set the system date and time
date # Show current date and time date -u # Show UTC time date +%Y-%m-%d # Format date as YYYY-MM-DD
ncal / cal
Display a calendar
ncal # Display calendar for current month ncal -3 # Display calendar for 3 months ncal -A1 -B1 # Show previous, current, and next month ncal -j # Display Julian dates ncal july 1969 # Display calendar for July 1969
ps
Report process status
ps # Show your processes ps -ef # Show all processes in full format ps aux # Show all processes with user information ps -ef | grep firefox # Find Firefox processes
du
Estimate file space usage
du -h file.txt # Show human-readable file size du -sh directory # Show total directory size du -h --max-depth=1 / # Show size of root directories
ifconfig / ip
Configure network interfaces
ifconfig # Display network interfaces ip addr show # Modern replacement for ifconfig ip a # Short version
ping
Send ICMP ECHO_REQUEST to network hosts
ping google.com # Check connection to Google ping -c 4 google.com # Send only 4 packets
traceroute / tracepath
Print the route packets trace to network host
traceroute google.com # Trace route to Google tracepath google.com # Similar to traceroute but without root
curl
Transfer data from or to a server
curl url # Get contents of URL curl -o file.html url # Save output to file curl ifconfig.me # Show your public IP address
The following commands are also useful for network operations:
ss - Investigate sockets (replacement for netstat)dig - DNS lookup utilitynslookup - Query DNS serversroute - Show or manipulate routing tablehost - DNS lookup utilitymtr - Network diagnostic tool (combines ping and traceroute)command > file.txt # Redirect output to file (overwrite) command >> file.txt # Append output to file command 2> error.txt # Redirect errors to file command &> file.txt # Redirect both output and errors command > output.txt 2> error.txt # Separate redirection
command < file.txt # Use file as input for command
command1 | command2 # Use output of command1 as input to command2 ls | grep ".txt" # List files and filter for .txt cat file.txt | wc -l # Count lines in file find . -name "*.txt" | xargs grep "pattern" # Find and search
tee
Read from stdin and write to stdout and files
command | tee file.txt # Display output and save to file command | tee -a file.txt # Append to file
# Basic script structure #!/bin/bash # Comment echo "Hello World" # Make script executable chmod +x script.sh # Run script ./script.sh
# Variable assignment (no spaces around =) name="John" echo $name # Command substitution current_date=$(date) echo "Today is $current_date"
# Brace expansion
echo {1..5} # Outputs: 1 2 3 4 5
mkdir -p {2023..2025}/{Jan,Feb,Mar} # Creates directory structure
# Pathname expansion (wildcards)
ls *.txt # List all .txt files
ls [A-C]*.txt # Files starting with A, B, or C
# Tilde expansion
echo ~ # Home directory
# Arithmetic expansion echo $((5 + 3)) # Outputs: 8 echo $((10 / 2)) # Outputs: 5 echo $((2**3)) # Power: 8
# If statement
if [ "$name" == "John" ]; then
echo "Hello John"
elif [ "$name" == "Jane" ]; then
echo "Hello Jane"
else
echo "Hello stranger"
fi
# Case statement
case $fruit in
"apple")
echo "It's an apple"
;;
"banana"|"plantain")
echo "It's a banana or plantain"
;;
*)
echo "Unknown fruit"
;;
esac
# For loop
for i in {1..5}; do
echo "Number: $i"
done
# While loop
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
((count++))
done
# For loop with command output
for file in $(ls *.txt); do
echo "Processing $file"
done
crontab
Schedule periodic jobs
crontab -e # Edit crontab crontab -l # List current jobs
# Format: minute hour day-of-month month day-of-week command # * represents any value # Examples: 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # 5 AM every Monday 30 6 * * * command # 6:30 AM every day */5 * * * * command # Every 5 minutes 0 0 1 * * command # Midnight on the first of every month
| Field | Range | Special Characters |
|---|---|---|
| Minute | 0-59 |
* (any value) , (list: 1,5,10) - (range: 1-5) / (steps: */5 = every 5) |
| Hour | 0-23 | |
| Day of Month | 1-31 | |
| Month | 1-12 | |
| Day of Week | 0-6 (Sunday=0) |
Ctrl+A # Move to beginning of line Ctrl+E # Move to end of line Ctrl+F # Move forward one character Ctrl+B # Move backward one character Alt+F # Move forward one word Alt+B # Move backward one word
Ctrl+K # Cut from cursor to end of line Ctrl+U # Cut from beginning of line to cursor Ctrl+W # Cut previous word Alt+D # Cut next word Ctrl+Y # Paste previously cut text Ctrl+T # Swap current and previous character Alt+T # Swap current and previous word
history # Show command history !73 # Run command #73 from history Ctrl+R # Search history (type to search, press again for next match) !! # Repeat last command !$ # Last argument of previous command
Ctrl+C # Interrupt (kill) current process Ctrl+Z # Suspend current process Ctrl+D # Exit shell (or end of input)
Ctrl+L # Clear screen