🧠 AI Computer Institute
Content is AI-generated for educational purposes. Verify critical information independently. A bharath.ai initiative.

Linux Terminal Cheat Sheet

programmingGrades 8-126 sections

Navigation & File Operations

# Navigation
pwd                         # Print working directory
cd /path/to/dir             # Change directory
cd ~                        # Home directory
cd ..                       # Parent directory
cd -                        # Previous directory

# List files
ls                          # Simple list
ls -l                       # Detailed list
ls -la                      # Include hidden files
ls -S                       # Sort by size
ls -t                       # Sort by time
ls -R                       # Recursive

# File operations
touch file.txt              # Create empty file
mkdir folder                # Create directory
mkdir -p a/b/c              # Create nested directories

cp file.txt copy.txt        # Copy file
cp -r folder/ folder_copy/  # Copy directory (recursive)

mv file.txt renamed.txt     # Move/rename
mv file.txt /path/          # Move to directory

rm file.txt                 # Delete file
rm -r folder/               # Delete directory (recursive)
rm -f file.txt              # Force delete

# View files
cat file.txt                # Display entire file
less file.txt               # Paginated view (q to quit)
head -n 10 file.txt         # First 10 lines
tail -n 10 file.txt         # Last 10 lines
wc -l file.txt              # Count lines

Permissions & Ownership

# View permissions
ls -l file.txt
# Output: -rw-r--r-- 1 user group 1024 date file.txt

# Change permissions (chmod)
chmod 644 file.txt          # rw- r-- r--
chmod 755 script.sh         # rwx r-x r-x
chmod +x script.sh          # Add execute permission
chmod -x script.sh          # Remove execute permission
chmod u+rw file.txt         # User +read/write
chmod g-w file.txt          # Group -write
chmod o-rwx file.txt        # Others -all

# Permission numbers
# 4 = read (r)
# 2 = write (w)
# 1 = execute (x)
# 7 = 4+2+1 = rwx

# Change ownership (chown)
sudo chown user:group file.txt
sudo chown user file.txt    # Just user

# Common patterns
chmod 644 *.txt             # Files: rw- r-- r--
chmod 755 script.sh         # Executable: rwx r-x r-x
chmod -R 755 folder/        # Recursive

Text Processing: grep, sed, awk

# grep (search text)
grep "pattern" file.txt
grep -n "pattern" file.txt  # Show line numbers
grep -i "pattern" file.txt  # Case-insensitive
grep -v "pattern" file.txt  # Invert match (exclude)
grep -r "pattern" /path/    # Recursive directory search
grep -E "[0-9]{3}" file.txt # Extended regex (ERE)

# sed (stream editor - replace)
sed 's/old/new/' file.txt        # Replace first on each line
sed 's/old/new/g' file.txt       # Replace all (global)
sed -i 's/old/new/g' file.txt    # In-place edit
sed '2,5s/old/new/g' file.txt    # Lines 2-5 only
sed '/pattern/s/old/new/g' file.txt  # Matching lines

# awk (text processing)
awk '{print $1}' file.txt        # Print 1st column
awk -F',' '{print $2}' data.csv  # CSV: 2nd column
awk '{sum += $1} END {print sum}' numbers.txt  # Sum column
awk 'NR > 1' file.txt            # Skip header
awk '$3 > 100 {print $1, $3}' data.txt  # Conditional

# Combining tools
grep "ERROR" log.txt | wc -l     # Count errors
cat file.txt | sed 's/a/A/g' | grep "A"

Piping & Redirection

# Redirection
command > output.txt        # Redirect stdout to file
command >> output.txt       # Append to file
command 2> error.txt        # Redirect stderr
command 2>&1 output.txt     # Both stdout & stderr
command < input.txt         # Redirect stdin

# Piping (|)
cat file.txt | grep "pattern"
ls -l | grep ".txt$"
command1 | command2 | command3

# Examples
ps aux | grep "python"      # Find Python processes
find . -name "*.log" | xargs rm  # Find & delete logs
cat access.log | cut -d' ' -f1 | sort | uniq -c  # IP stats

# tee (pipe to file AND stdout)
command | tee output.txt
command | tee -a output.txt # Append

System & Process Management

# Disk usage
df -h                       # Disk space (human-readable)
du -sh folder/              # Folder size
du -h --max-depth=1 .       # Size by subdirectory

# Memory & processes
free -h                     # Memory usage
ps aux                      # List all processes
ps aux | grep "python"      # Find process
top                         # Real-time monitoring
kill 1234                   # Kill process by ID
killall python              # Kill all Python processes

# Search for files
find . -name "*.txt"        # By name
find . -type f -size +10M   # By size
find . -mtime -1            # Modified in last day
locate file.txt             # Faster search (uses database)

# System info
uname -a                    # System info
hostname                    # Computer name
whoami                      # Current user
id                          # User ID & groups
uptime                      # System uptime

Cron Jobs (Scheduled Tasks)

# Edit cron schedule
crontab -e                  # Edit user crontab
sudo crontab -e             # Edit root crontab

# View cron jobs
crontab -l                  # List user cron jobs
sudo crontab -l             # List root cron jobs

# Cron syntax
# ┌───────────── minute (0-59)
# │ ┌───────────── hour (0-23)
# │ │ ┌───────────── day of month (1-31)
# │ │ │ ┌───────────── month (1-12)
# │ │ │ │ ┌───────────── day of week (0-7, 0 and 7 are Sunday)
# │ │ │ │ │
# * * * * * command

# Examples
0 9 * * * /path/to/script.sh          # Daily at 9 AM
0 */4 * * * python backup.py          # Every 4 hours
30 2 * * 0 /path/to/weekly_task.sh    # Sundays at 2:30 AM
0 0 1 * * /path/to/monthly_task.sh    # 1st of month at midnight
*/15 * * * * python check_health.py   # Every 15 minutes

# Special shortcuts
@hourly /path/to/task.sh      # 0 * * * *
@daily /path/to/task.sh       # 0 0 * * *
@weekly /path/to/task.sh      # 0 0 * * 0
@monthly /path/to/task.sh     # 0 0 1 * *
@reboot /path/to/startup.sh   # On system boot

# Logging cron output
0 9 * * * /path/to/script.sh >> /var/log/cron.log 2>&1

More Cheat Sheets