Skip to content

Linux Command Reference

Quick reference for essential command-line tools used in system administration and development.

Terminal window
ls [options] [path]

Common Options:

  • -l - Long format with details
  • -a - Show hidden files
  • -h - Human-readable file sizes
  • -t - Sort by modification time
  • -r - Reverse order

Examples:

Terminal window
ls -la # List all files with details
ls -lh /var/log # Show log files with readable sizes
ls -lt # Sort by newest first
Terminal window
cp [options] source destination

Options:

  • -r - Copy directories recursively
  • -p - Preserve attributes
  • -v - Verbose output
  • -i - Prompt before overwrite

Examples:

Terminal window
cp file.txt backup.txt
cp -r dir1/ dir2/
cp -p *.conf /etc/backup/
Terminal window
mv [options] source destination

Examples:

Terminal window
mv oldname.txt newname.txt
mv *.log /var/logs/
mv -i file.txt /backup/ # Prompt before overwrite
Terminal window
rm [options] file

Options:

  • -r - Remove directories recursively
  • -f - Force removal without prompts
  • -i - Prompt before each removal
  • -v - Verbose output

Examples:

Terminal window
rm file.txt
rm -rf directory/
rm -i *.tmp # Prompt before deleting
Terminal window
grep [options] pattern [files]

Options:

  • -i - Case insensitive
  • -r - Recursive search
  • -n - Show line numbers
  • -v - Invert match (show non-matching)
  • -c - Count matches

Examples:

Terminal window
grep "error" logfile.txt
grep -i "warning" *.log
grep -rn "TODO" /project/
grep -v "^#" config.conf # Exclude comments
Terminal window
sed [options] 'command' file

Common Commands:

  • s/old/new/ - Substitute text
  • /pattern/d - Delete matching lines
  • /pattern/p - Print matching lines

Examples:

Terminal window
sed 's/foo/bar/' file.txt # Replace first occurrence
sed 's/foo/bar/g' file.txt # Replace all occurrences
sed -i 's/foo/bar/g' file.txt # Edit file in-place
sed '/^#/d' file.txt # Delete comment lines
sed -n '/error/p' log.txt # Print only error lines
Terminal window
awk 'pattern {action}' file

Examples:

Terminal window
awk '{print $1}' file.txt # Print first column
awk -F: '{print $1}' /etc/passwd # Use : as delimiter
awk '$3 > 100' data.txt # Print lines where column 3 > 100
awk '{sum+=$1} END {print sum}' # Sum first column
Terminal window
ps [options]

Options:

  • aux - All processes, user-oriented
  • -ef - All processes, full format
  • -u username - Processes for specific user

Examples:

Terminal window
ps aux
ps -ef | grep nginx
ps -u apache
Terminal window
top [options]

Interactive Commands (while running):

  • q - Quit
  • k - Kill process
  • M - Sort by memory
  • P - Sort by CPU
  • u - Filter by user
Terminal window
df [options]

Options:

  • -h - Human-readable sizes
  • -T - Show filesystem type
  • -i - Inode information

Examples:

Terminal window
df -h
df -hT /dev/sda1
Terminal window
du [options] [path]

Options:

  • -h - Human-readable
  • -s - Summary only
  • -a - All files
  • --max-depth=N - Limit directory depth

Examples:

Terminal window
du -h /var/log
du -sh /home/*
du -h --max-depth=1 /
Terminal window
ping [options] hostname

Options:

  • -c N - Send N packets
  • -i N - Wait N seconds between packets
  • -s size - Packet size

Examples:

Terminal window
ping google.com
ping -c 4 192.168.1.1
Terminal window
curl [options] URL

Options:

  • -O - Save with original filename
  • -o filename - Save as filename
  • -X METHOD - HTTP method
  • -H "Header" - Add header
  • -d data - POST data

Examples:

Terminal window
curl https://example.com
curl -O https://example.com/file.zip
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://api.example.com
Terminal window
netstat [options]

Options:

  • -tuln - Show listening TCP/UDP ports with numbers
  • -r - Show routing table
  • -i - Show network interfaces

Examples:

Terminal window
netstat -tuln
netstat -r
netstat -i
Terminal window
chmod [options] mode file

Numeric Mode:

  • 4 = read (r)
  • 2 = write (w)
  • 1 = execute (x)

Examples:

Terminal window
chmod 755 script.sh # rwxr-xr-x
chmod 644 file.txt # rw-r--r--
chmod +x script.sh # Add execute
chmod -w file.txt # Remove write
Terminal window
chown [options] owner[:group] file

Examples:

Terminal window
chown user file.txt
chown user:group file.txt
chown -R user:group directory/
Terminal window
tar [options] archive files

Common Options:

  • -c - Create archive
  • -x - Extract archive
  • -v - Verbose
  • -f - File name
  • -z - Gzip compression
  • -j - Bzip2 compression

Examples:

Terminal window
tar -czf archive.tar.gz directory/ # Create compressed
tar -xzf archive.tar.gz # Extract compressed
tar -tvf archive.tar # List contents
Terminal window
gzip [options] file
gunzip [options] file.gz

Examples:

Terminal window
gzip file.txt # Creates file.txt.gz
gunzip file.txt.gz # Extracts to file.txt
gzip -k file.txt # Keep original