Linux Commands Cheat Sheet with Simple
Explanations
File Management
• cd : Change directory
• cd .. : Move one level up in the directory
• cd ~ : Go to the home directory
• cd - : Switch to the previous working directory
• cd <path> : Go to a specific directory path
• mkdir dir_name : Create a new directory
• mkdir dir1 dir2 : Create multiple directories
• mkdir /path/to/directory : Create directory at a specified path
• mkdir -p /a/b/c/ : Create nested directories
• mkdir -m <mode> dir_name : Create directory with specific permissions
• mkdir -i dir_name : Interactive directory creation (asks before overwrite)
• mkdir -v dir_name : Show messages while creating directory
• rmdir dir_name : Remove an empty directory
• rmdir dir1 dir2 : Remove multiple empty directories
• rmdir /path/to/directory : Remove directory at a specified path
• rmdir -r dir_name : Remove directory and contents recursively
• rmdir -i dir_name : Prompt before removing directory
• rmdir -v dir_name : Show messages while removing directory
• touch file_name : Create an empty file or update timestamp
• touch file1 file2 : Create multiple empty files
• touch -t yyyymmddhhmm file_name : Create file with specific timestamp
Viewing Files
• cat file_name : View contents of a file
• cat file1 file2 : Concatenate and display multiple files
• cat > file_name : Create/overwrite file with input
• cat file1 file2 > new_file : Save merged files into a new file
• cat -n file_name : Display file with line numbers
• cat -v file_name : Show hidden characters like tabs
Displaying File Content (Head/Tail)
• head file_name : Show first 10 lines
• head -n 5 file_name : Show first 5 lines
• head -c 100 file_name : Show first 100 bytes
• tail file_name : Show last 10 lines
• tail -n 5 file_name : Show last 5 lines
1
• tail -c 100 file_name : Show last 100 bytes
File and Directory Removal
• rm file_name : Remove a file
• rm file1 file2 : Remove multiple files
• rm -r dir_name : Remove directory and contents
• rm -rf dir_name : Force remove directory recursively
• rm -i file_name : Confirm before deleting
• rm -v file_name : Show message after deletion
• rm -d dir_name : Remove empty directory
File Listing
• ls : List files and folders
• ls -l : Long format listing
• ls -a : Include hidden files
• ls -t : Sort by modification time
• ls -h : Human-readable sizes
• ls -r : Reverse order
• ls -R : List recursively
• ls -S : Sort by file size
Copy and Move Files
• cp source dest : Copy file/folder
• cp -i : Prompt before overwrite
• cp -r : Copy folder recursively
• cp -p : Preserve file attributes
• cp -a : Archive copy (recursive + preserve)
• cp -v : Verbose output
• mv source dest : Move or rename file/folder
• mv -f : Force move
• mv -i : Prompt before overwrite
• mv -p : Preserve attributes
• mv -R : Move directories recursively
Misc Commands
• alias ll='ls -alF' : Create shortcut command
• unalias ll : Remove shortcut
• history : Show command history
• history | grep 'term' : Search history
• export VAR=value : Set environment variable
• unset VAR : Remove environment variable
2
Process Management
• ps : List processes
• ps aux : Show all processes
• ps -ef : Full process list
• top : Live process monitor
• htop : Interactive process viewer
• kill PID : Terminate process
• killall name : Kill all matching processes
• pkill name : Kill processes by name
• pgrep name : Get PID(s) of process
• fg %1 : Bring job to foreground
• bg %1 : Send job to background
• jobs : List background jobs
• nohup command & : Run process in background with no hangup
• cat nohup.out : View output of background process
File Ownership and Permission
• chmod 755 file : Set permission using octal
• chmod -R 755 dir : Recursive permission change
• chmod u=rw,go=r file : Set symbolic permission
• chown user file : Change file owner
• chown user:group file : Change owner and group
• chown :group file : Change only group
User and Group Management
• whoami : Show current user
• groupadd name : Create new group
• useradd -G group user : Add user to group
• passwd user : Set password for user
• chown :group folder : Change group ownership
• chmod g+w folder : Add write for group
• chmod o-rx folder : Remove read/execute for others
• su - user : Switch user
Remote Access and File Transfer
• ssh -i key.pem user@host : Connect to remote server
• scp -i key.pem file user@host:path : Copy to remote
• rsync -av -e "ssh -i key.pem" source user@host:path : Sync to remote
• sftp -i key.pem user@host : SFTP access
3
Log Monitoring
• journalctl : View system logs
• journalctl -u service : Logs for specific service
• journalctl -p err : Show error logs
• journalctl --since "start" --until "end" : Logs in time range
Firewall Configuration
• ufw allow 80 : Allow HTTP
• ufw default deny incoming : Block all others
• ufw reset : Reset firewall
• ufw delete allow 80/tcp : Delete rule
• ufw deny 80/tcp : Deny specific rule
• iptables -L : List rules
• iptables -A INPUT -p tcp --dport 22 -j ACCEPT : Allow SSH
• iptables -P INPUT DROP : Drop all others
• iptables -F : Flush rules
File Compression
• zip archive.zip file1 file2 : Create ZIP
• zip -u archive.zip file3 : Update ZIP
• unzip archive.zip : Extract ZIP
• tar -cvf archive.tar files : Create TAR
• tar -cvzf archive.tar.gz files : Create TAR.GZ
• tar -xvf archive.tar : Extract TAR
Text Processing
• grep "pattern" file : Search in file
• grep -r "pattern" dir : Recursive search
• grep -i : Case insensitive
• grep -n : Show line numbers
• grep -v : Show non-matching lines
• grep -c : Count matches
• sed 's/a/b/' file : Replace first occurrence
• sed 's/a/b/g' : Replace all
• awk '{print $1}' : Print first column
• sort file : Sort lines
• wc -l : Line count
• find . -name "file" : Find files
System Info
• uptime : System uptime
4
• free -h : RAM usage
• df -h : Disk usage
• du -h : Folder size
• uname -a : System info
• who : Who is logged in
Package Management
• apt update : Update package list
• apt install pkg : Install package
• apt upgrade : Upgrade packages
• apt remove pkg : Remove package
• apt purge pkg : Remove with config
• apt autoremove : Remove unused packages
• apt search keyword : Search packages
Service Management
• systemctl start service : Start service
• systemctl stop service : Stop service
• systemctl restart service : Restart
• systemctl enable service : Auto-start on boot
• systemctl status service : Check status
Networking
• ifconfig : Show network interfaces
• ip addr show : Show IPs
• ping host : Test connectivity
• traceroute host : Trace route
• netstat -tuln : Listening ports
• nslookup domain : DNS lookup
• dig domain : DNS lookup
• wget url : Download file
• curl url : Transfer data
• ssh user@host : Remote login