Linux Cheat Sheet
Essential Linux commands reference
Hardware Information
- lscpu See CPU information
- lsblk See block devices
- lspci -tv Show PCI devices
- lsusb -tv Show USB devices
- lshw Show hardware information
- cat /proc/cpuinfo View CPU details
- cat /proc/meminfo View memory details
- cat /proc/mounts View mounted file systems
- free -h Check free and used memory
- sudo dmidecode Show hardware info from BIOS
- hdparm -i /dev/[device] Show info about disk device
- hdparm -tT /dev/[device] Conduct a read speed test on device
- badblocks -s /dev/[device] Test for unreadable blocks on device
- fsck /dev/[device] Run a disk check on unmounted partition
Searching
- find [path] -name [pattern] Find files and directories
- find [path] -size [+100M] Find files larger than 100MB
- grep [pattern] [file] Search for a specific pattern in file
- grep -r [pattern] [dir] Search recursively for pattern in directory
- locate [name] Locate all files and directories by name
- which [command] Show the full path of a command
- whereis [command] Locate the binary, source, and manual page files
- awk '[pattern] {print $0}' [file] Print all lines matching a pattern
- sed 's/[old]/[new]/' [file] Find and replace text in a file
File Commands
- mkdir [dir] Create a new directory
- rm [file] Remove a file
- rm -r [dir] Remove a directory recursively
- rm -rf [dir] Force remove a directory recursively
- cp [src] [dest] Copy a file
- cp -r [src_dir] [dest_dir] Copy a directory recursively
- mv [src] [dest] Move or rename files/directories
- ln -s [path]/[file] [link] Create a symbolic link
- touch [file] Create a new empty file
- cat [file] Show the contents of a file
- head [file] Show the first 10 lines of a file
- tail [file] Show the last 10 lines of a file
- more [file] Display file content page by page
- less [file] Similar to more but allows backward movement
- nano [file] Open file in Nano text editor
- vim [file] Open file in Vim text editor
- gpg -c [file] Encrypt a file
- gpg [file].gpg Decrypt a file
- wc -w [file] Count words in a file
- cut -d [delim] [file] Cut a section of a file
- shred -u [file] Overwrite and delete a file securely
- diff [file1] [file2] Compare two files
- source [file] Read and execute file content in current shell
- [cmd] | tee [file] Output command result to screen and file
Directory Navigation
- ls List files and directories
- ls -a List all files (including hidden)
- ls -l List files with detailed info
- pwd Show current working directory
- cd Change to home directory
- cd .. Move up one directory level
- cd - Move to previous directory
- cd [path] Change to specified directory
File Compression
- tar cf [archive.tar] [dir] Create a tar archive
- tar xf [archive.tar] Extract a tar archive
- tar czf [archive.tar.gz] Create a compressed tar archive
- gzip [file] Compress a file (.gz)
- gunzip [file.gz] Decompress a .gz file
- bzip2 [file] Compress a file (.bz2)
- bunzip2 [file.bz2] Decompress a .bz2 file
File Transfer
- scp [file] [user]@[host]:[path] Securely copy file to remote host
- rsync -a [src] [user]@[host]:[dest] Synchronize files with remote host
- wget [link] Download file from URL
- curl -O [link] Download file from URL
- sftp [user]@[host] Connect via SFTP
User and Group
- id Show user and group IDs
- last Show last logged in users
- who Show who is logged in
- w Show who is logged in and what they are doing
- sudo useradd [user] Create a new user
- sudo userdel [user] Delete a user
- sudo usermod -aG [group] [user] Add user to a group
- passwd [user] Change user password
- sudo groupadd [group] Create a new group
- sudo groupdel [group] Delete a group
- su - [user] Switch to another user account
Package Installation
- sudo apt install [pkg] Install package (Debian/Ubuntu)
- apt search [keyword] Search for package
- apt show [pkg] Show package info
- sudo dpkg -i [pkg.deb] Install .deb file
- sudo yum install [pkg] Install package (RHEL/CentOS)
- sudo dnf install [pkg] Install package (Fedora)
- sudo rpm -i [pkg.rpm] Install .rpm file
- sudo snap install [pkg] Install Snap package
- flatpak install [pkg] Install Flatpak package
Process Related
- ps List active processes
- top View all running processes
- htop Interactive process viewer
- kill [pid] Kill process by ID
- pkill [name] Kill process by name
- killall [name] Kill all processes by name
- bg Resume suspended job in background
- fg Bring background job to foreground
- lsof List open files
System Management
- uname -a Show kernel and system info
- uptime Show system uptime
- hostname -i Show IP address
- date Show current date and time
- timedatectl Query and change system clock
- shutdown now Shutdown system immediately
- reboot Reboot system
- dmesg Show bootup messages
Disk Usage
- df -h Show free disk space
- du -sh [dir] Show size of directory
- fdisk -l List partition tables
- mount [dev] [point] Mount a device
- findmnt List all mounted filesystems
SSH Login
- ssh [user]@[host] Connect to remote host
- ssh -p [port] [user]@[host] Connect on specific port
- ssh-keygen Generate SSH key pairs
File Permission
- chmod 777 [file] Read, write, execute for all
- chmod 755 [file] Read/execute for group/others
- chown [user]:[group] [file] Change owner and group
Network
- ip addr show List IP addresses
- ip address add [IP] Assign IP address
- ifconfig Display IP addresses
- ping [host] Ping remote host
- netstat -pnltu Show active ports
- netstat -tuln Show TCP/UDP ports
- whois [domain] Domain info
- dig [domain] DNS info
- dig -x [domain] Reverse DNS (domain)
- dig -x [IP] Reverse DNS (IP)
- host [domain] IP lookup
- hostname -I Local IP addresses
- nslookup [domain] Query DNS
Variable Commands
- let "[var]=[val]" Assign integer value
- export [var] Export Bash variable
- declare [var]="[val]" Declare Bash variable
- set List variables/functions
- unset [var] Remove variable
- echo $[var] Display variable value
Shell Command Management
- alias [name]='[cmd]' Create alias
- watch -n [sec] [cmd] Run periodically
- sleep [sec] && [cmd] Run after delay
- at [hh:mm] Schedule command
- man [command] Show manual
- history Show history
Linux Shell Shortcuts
- Ctrl + C Kill process
- Ctrl + Z Suspend process
- Ctrl + D Log out
- Ctrl + W Erase word
- Ctrl + U Erase line
- Ctrl + R Search history
- fg Resume foreground
- bg Resume background
- clear Clear screen
- !! Repeat last command
- exit Log out
Reference: phoenixnap.com