Linux Commands Cheat Sheet
File Navigation Commands
- pwd
- Purpose: Print working directory
- Example: pwd
- Output: /home/user/documents
- ls
- Purpose: List directory contents
- Common options:
- ls -l: Long format listing
- ls -a: Show hidden files
- ls -h: Human-readable sizes
- Example: ls -lah
- cd
- Purpose: Change directory
- Usage:
- cd /path/to/directory: Go to specific directory
- cd ..: Go up one directory
- cd ~: Go to home directory
- cd -: Go to previous directory
- find
- Purpose: Search for files
- Example: find /home -name “*.txt”
- Common options:
- -type f: Files only
- -type d: Directories only
- -size: Search by size
File Management Commands
- cp
- Purpose: Copy files/directories
- Usage: cp source destination
- Common options:
- -r: Copy directories recursively
- -i: Interactive (prompt before overwrite)
- mv
- Purpose: Move/rename files
- Example: mv oldname.txt newname.txt
- Example: mv file.txt /new/location/
- rm
- Purpose: Remove files/directories
- Common options:
- -r: Remove directories recursively
- -f: Force remove
- Example: rm -rf directory/
- mkdir
- Purpose: Create directory
- Example: mkdir new_directory
- Option: -p: Create parent directories
- touch
- Purpose: Create empty file/update timestamp
- Example: touch newfile.txt
- chmod
- Purpose: Change file permissions
- Example: chmod 755 file.txt
- Common options:
- u+x: Add execute permission for user
- g-w: Remove write permission for group
File Content Commands
- cat
- Purpose: Display file contents
- Example: cat file.txt
- less
- Purpose: View file contents with pagination
- Example: less largefile.txt
- Navigation:
- Space: Next page
- b: Previous page
- q: Quit
- head
- Purpose: Show first lines of file
- Example: head -n 10 file.txt
- tail
- Purpose: Show last lines of file
- Example: tail -f logfile.txt
- Option -f: Follow file updates
- grep
- Purpose: Search text patterns
- Example: grep “pattern” file.txt
- Common options:
- -i: Case insensitive
- -r: Recursive search
- -v: Invert match
System Information Commands
- uname
- Purpose: System information
- Example: uname -a
- top
- Purpose: Show running processes
- Interactive commands:
- k: Kill process
- q: Quit
- r: Renice
- df
- Purpose: Disk space usage
- Example: df -h
- du
- Purpose: Directory space usage
- Example: du -sh *
- free
- Purpose: Memory usage
- Example: free -h
Process Management Commands
- ps
- Purpose: Show processes
- Example: ps aux
- kill
- Purpose: Terminate processes
- Example: kill -9 PID
- killall
- Purpose: Kill processes by name
- Example: killall firefox
- htop
- Purpose: Interactive process viewer
- Navigation:
- F6: Sort by column
- F9: Kill process
- F10: Quit
Network Commands
- ping
- Purpose: Test network connectivity
- Example: ping google.com
- ifconfig/ip
- Purpose: Network interface configuration
- Example: ip addr show
- netstat
- Purpose: Network statistics
- Example: netstat -tuln
- ssh
- Purpose: Secure shell connection
- Example: ssh user@hostname
- scp
- Purpose: Secure copy files
- Example: scp file.txt user@host:/path
File Compression Commands
- tar
- Purpose: Archive files
- Create: tar -czf archive.tar.gz files/
- Extract: tar -xzf archive.tar.gz
- gzip/gunzip
- Purpose: Compress/decompress files
- Example: gzip file.txt
- zip/unzip
- Purpose: Create/extract ZIP archives
- Example: zip archive.zip files/
User Management Commands
- sudo
- Purpose: Execute as superuser
- Example: sudo command
- useradd
- Purpose: Create new user
- Example: sudo useradd username
- passwd
- Purpose: Change password
- Example: passwd username
Text Processing Commands
- sed
- Purpose: Stream editor
- Example: sed ‘s/old/new/g’ file.txt
- awk
- Purpose: Text processing
- Example: awk ‘{print $1}’ file.txt
- sort
- Purpose: Sort text lines
- Example: sort file.txt
- wc
- Purpose: Word, line, character count
- Example: wc -l file.txt
System Control Commands
- systemctl
- Purpose: Service management
- Example: systemctl start service
- journalctl
- Purpose: View system logs
- Example: journalctl -u service
- crontab
- Purpose: Schedule tasks
- Example: crontab -e
Package Management Commands
- apt/apt-get (Debian/Ubuntu)
- Update: apt update
- Install: apt install package
- Remove: apt remove package
- yum/dnf (RedHat/Fedora)
- Update: dnf update
- Install: dnf install package
- Remove: dnf remove package
File Transfer Commands
- wget
- Purpose: Download files
- Example: wget URL
- curl
- Purpose: Transfer data
- Example: curl -O URL
Disk Management Commands
- fdisk
- Purpose: Partition management
- Example: fdisk -l
- mount
- Purpose: Mount filesystems
- Example: mount /dev/sdb1 /mnt
System Monitoring Commands
- vmstat
- Purpose: Virtual memory statistics
- Example: vmstat 1
- iostat
- Purpose: I/O statistics
- Example: iostat -x 1
Advanced Commands
- lsof
- Purpose: List open files
- Example: lsof -i :80
- strace
- Purpose: Trace system calls
- Example: strace command
- tcpdump
- Purpose: Network packet analyzer
- Example: tcpdump -i eth0
- nmap
- Purpose: Network exploration
- Example: nmap hostname
Shell Scripting Commands
- echo
- Purpose: Print text
- Example: echo “Hello World”
- read
- Purpose: Read user input
- Example: read -p “Enter name: ” name
- test/[ ]
- Purpose: Condition evaluation
- Example: [ -f file.txt ]
Performance Commands
- nice
- Purpose: Run with priority
- Example: nice -n 19 command
- time
- Purpose: Time command execution
- Example: time command
- watch
- Purpose: Execute periodically
- Example: watch -n 1 command
Quick Tips
- Use Tab for command completion
- Use Ctrl+R for command history search
- Use man or –help for command documentation
- Use | (pipe) to chain commands
- Use > for output redirection
- Use && to run commands sequentially
Common Command Combinations
bash
# Find and remove files find . -name “*.tmp” -exec rm {} \; # Search in files and count matches grep -r “pattern” . | wc -l # Monitor log file in real-time tail -f logfile | grep “error” # Disk usage sorted by size du -sh * | sort -hr # Find largest files find . -type f -exec du -h {} + | sort -hr | head -n 10
Safety Tips
- Always use -i flag with rm for interactive deletion
- Make backups before major changes
- Be careful with sudo commands
- Test commands on small scale first
- Use man pages for detailed documentation
Note: Some commands may require root privileges or additional packages to be installed.