Bash Cheat Sheet

January 16, 2025

  • cd - Change directory
  • cd ~ - Go to home directory
  • cd .. - Go up one directory
  • cd - - Go to previous directory
  • pwd - Print working directory
  • ls - List files
  • ls -la - List all files with details
  • ls -lh - List files with human-readable sizes

File Operations

  • cp file1 file2 - Copy file1 to file2
  • cp -r dir1 dir2 - Copy directory recursively
  • mv file1 file2 - Move/rename file
  • rm file - Remove file
  • rm -r dir - Remove directory recursively
  • rm -rf dir - Force remove directory
  • mkdir dir - Create directory
  • touch file - Create empty file
  • cat file - Display file contents
  • less file - View file page by page
  • head file - Show first 10 lines
  • tail file - Show last 10 lines
  • tail -f file - Follow file updates

File Permissions

  • chmod 755 file - Change file permissions
  • chmod +x file - Make file executable
  • chown user:group file - Change file owner
  • sudo command - Run command as superuser

Search & Find

  • grep pattern file - Search for pattern in file
  • grep -r pattern dir - Recursive search
  • find . -name "*.txt" - Find files by name
  • find . -type f - Find all files
  • locate file - Find file by name

Text Processing

  • echo "text" - Print text
  • cat file1 file2 - Concatenate files
  • wc -l file - Count lines in file
  • sort file - Sort file lines
  • uniq file - Remove duplicate lines
  • cut -d: -f1 file - Cut fields from file
  • sed 's/old/new/g' file - Replace text
  • awk '{print $1}' file - Print first field

Process Management

  • ps - List running processes
  • ps aux - List all processes
  • kill PID - Kill process by ID
  • killall process - Kill all processes by name
  • jobs - List background jobs
  • bg - Resume job in background
  • fg - Bring job to foreground
  • nohup command & - Run command in background

System Info

  • df -h - Disk space usage
  • du -sh dir - Directory size
  • free -h - Memory usage
  • top - System monitor
  • htop - Interactive system monitor
  • uname -a - System information
  • whoami - Current user
  • date - Current date/time

Redirection & Pipes

  • command > file - Redirect output to file
  • command >> file - Append output to file
  • command < file - Redirect input from file
  • command1 | command2 - Pipe output
  • command 2>&1 - Redirect stderr to stdout
  • command > /dev/null - Discard output

Environment Variables

  • export VAR=value - Set environment variable
  • echo $VAR - Print variable
  • env - List all environment variables
  • $PATH - Command search path
  • $HOME - Home directory
  • $USER - Current username