Basic Linux commands cheat sheet
Cheat sheet of common Linux commands: processes, memory, filesystems, permissions, networking, redirections and grep.
A quick reference of the Linux commands used most often in day-to-day administration: processes, memory, filesystems, permissions, networking, archives, redirections and search.
Help
man ls
man is the built-in manual. Use it on any command.
Process management
| Command | Purpose |
|---|---|
top |
Live view of running processes |
htop |
Like top, with a clearer interface |
ps |
List processes |
pstree |
Tree of processes and their relationships |
who |
Users currently logged in |
kill |
Send a signal to a process (terminate it by default) |
ps -ef | grep pmon
ps -aux
CPU fields in top
| Field | Meaning |
|---|---|
| us | Time in user space |
| sy | Time in kernel space |
| ni | Time running niced user processes (user-defined priority) |
| id | Idle time |
| wa | Time waiting for I/O (for example disk) |
| hi | Time handling hardware interrupts |
| si | Time handling software interrupts |
| st | Involuntary wait of a virtual CPU while the hypervisor serves another processor (time stolen from the VM) |
Interactive keys in top
| Key | Action |
|---|---|
H |
Help |
P |
Sort by CPU usage |
M |
Sort by memory usage |
R |
Reverse the sort order |
z |
Colors |
x |
Highlight the sort column |
1 |
Show each CPU separately |
Signals
| Signal | Number | Description |
|---|---|---|
| SIGHUP | 1 | Hang up: controlling terminal closed or controlling process ended |
| SIGINT | 2 | Interrupt (Ctrl+C) |
| SIGQUIT | 3 | Quit (Ctrl+\) |
| SIGFPE | 8 | Illegal arithmetic operation |
| SIGKILL | 9 | Immediate kill, cannot be caught |
| SIGALRM | 14 | Alarm clock |
| SIGTERM | 15 | Request termination |
| SIGSTOP | 17, 19 or 23 depending on the platform | Stop the process |
Memory
free -m
| Column | Meaning |
|---|---|
| total | Total installed memory (MemTotal and SwapTotal in /proc/meminfo) |
| used | Used memory |
| free | Unused memory |
| shared | Memory used by tmpfs (Shmem in /proc/meminfo) |
| buffers | Memory used by kernel buffers |
| cached | Memory used by the page cache and slabs |
Effective free memory and utilisation (values in KB, from free):
Free memory = Total - Used + Buffers + Cache
= 16313900 - 7857128 + 203152 + 4381116 = 13041040
Utilisation = (Total - Free) / Total * 100 = 20.06 %
Filesystems
Common types
| Type | Description |
|---|---|
| ext2, ext3, ext4 | Successive versions of the Extended Filesystem; ext3 improved performance, ext4 added performance and features |
| JFS | Journaled file system developed by IBM; logs changes to files and folders |
| ReiserFS | Alternative to ext3 with advanced features and better performance |
| XFS | High-speed journaled file system aimed at parallel I/O |
| Btrfs | B-tree file system focused on large storage, repair, administration and fault tolerance |
Swap is disk space used as extra memory when physical RAM is full. It is useful on machines with little memory or under heavy load.
Commands
lsblk -m
df -Th
fdisk
Main directories
| Path | Content |
|---|---|
/boot |
Files needed at boot, including the kernel |
/dev |
Device files |
/etc |
Application configuration and start/stop scripts |
/home |
User home directories |
/lib |
Kernel modules and shared libraries needed to boot and run commands |
/lost+found |
Recovery of files damaged by an unexpected shutdown |
/media |
Mount points for removable devices |
/mnt |
Temporary mount point for filesystems |
/opt |
Optional third-party software |
/proc |
Virtual filesystem with information about running processes (by PID) |
/root |
Home directory of the root user (not to be confused with /) |
/run |
Early runtime data |
/sbin |
Administration binaries (iptables, fdisk, ifconfig, swapon, reboot...) |
/srv |
Service-specific data |
/sys |
Virtual filesystem exposing and allowing changes to connected devices |
/tmp |
Temporary files, kept until the next boot |
/usr |
Binaries, documentation, sources and libraries of second-level programs |
/var |
Variable data: logs, locks, spool, mail, temporary files |
File manipulation
| Command | Purpose |
|---|---|
mkdir / rmdir |
Create / remove a directory |
touch |
Create an empty file |
cp / mv / rm |
Copy / move / delete |
find |
Search for files |
tar |
Archive |
# Create a gzip-compressed archive
tar -czvf backup_config_ssh.tar .ssh/
# Extract it
tar -xzvf backup_config_ssh.tar
tar options: c create, x extract, z gzip, j bzip2, t list, u update.
Zip archives
zip archive.zip file1 file2 # compress files
zip -r archive.zip folder1 folder2 # compress whole folders
unzip archive.zip # extract
unzip -l archive.zip # list the content
Permissions
Permissions apply to three classes: the owner, the group and all other users. Each class can have read, write and execute rights. Execute on a directory allows entering it.
ls -l # show owner, group and permissions
chown user:group file # change owner and group
chmod 750 file # change permissions
| Number | Symbol | Permission |
|---|---|---|
| 0 | --- |
None |
| 1 | --x |
Execute |
| 2 | -w- |
Write |
| 3 | -wx |
Write + execute |
| 4 | r-- |
Read |
| 5 | r-x |
Read + execute |
| 6 | rw- |
Read + write |
| 7 | rwx |
Read + write + execute |
Network
Several classic tools are legacy and replaced by modern ones: ifconfig, netstat, route and arp by ip and ss; dig by drill on some systems.
ip addr # network addresses
ip route # routing table
ip link # adapter state
ip neighbour # ARP/neighbour table
ip -s -s neigh flush all # flush the ARP table
traceroute example.com
tracepath example.com
ping -c 4 example.com
ss -s # summary
ss -t4 # established IPv4 TCP sockets
ss -t4 state closed # closed TCP sockets
ss dst 192.0.2.10 # connections to a given address
ss -tupln # TCP/UDP listeners, numeric
netstat -s # network statistics (legacy)
netstat -p # programs and ports
netstat -r # routing table
dig example.com ANY @ns1.example.com
dig -t txt example.com
dig -x 192.0.2.10
dig example.com MX
drill -t txt example.com
drill -x 192.0.2.10
host sqlserver01
host -r sqlserver01
arp -n
arp -a
arp -d 192.0.2.12
Other useful tools: nethogs (traffic per application), nslookup (older than dig), iwconfig (Wi-Fi), hostname, curl, wget, mtr, whois, ifplugstatus, iftop, tcpdump.
nmcli device status
lspci | less
lshw -C network | grep -B 1 -A 12 'Wireless interface'
Other common commands
pwd, cd, ls, cat, grep, sudo, df, du, head, tail, diff, jobs, uname, history, echo, stat, useradd, userdel.
Shell
echo $0 # current shell
cat /etc/shells # available shells
Bash startup files are read in this order: /etc/profile, ~/.bash_profile, ~/.bashrc, ~/.bash_login, ~/.profile; ~/.bash_logout runs at logout. ~/.bashrc is the usual place for application setup.
Standard streams and redirection
| Stream | ID |
|---|---|
| stdin | 0 |
| stdout | 1 |
| stderr | 2 |
ls > output.txt # overwrite
ls >> output.txt # append
tr a-z A-Z < filename.txt # read from a file
cat filename.txt | tr a-z A-Z # pipe
ls *.txt | wc -l > count.txt # combine pipe and redirection
ls fffff 2> error.txt # redirect errors
ls -l new.txt ffff > output.txt 2> error.txt
ls -l new.txt fff 2>> combined.txt >> combined.txt
ls -l new.txt fff > output.txt 2>&1 # stdout and stderr to the same file
Searching text with grep
grep --after-context=5 --before-context=10 'Nov 12 2012' yourfile.log
grep -rlw "text" /var/log
grep -rlw -e "text" -e "https" /var/log
grep -rlw --include="*.log" -e "text" /var/log
grep -rlw --exclude="*.txt" -e "text" /var/log
grep -rlw --exclude-dir="*http*" -e "text" /var/log
| Option | Meaning |
|---|---|
-r |
Recursive search |
-R |
Recursive search, following symlinks |
--include=PATTERN |
Search only files matching the pattern |
--exclude=PATTERN |
Skip files matching the pattern |
--exclude-from=FILE |
Skip patterns listed in a file |
--exclude-dir=PATTERN |
Skip matching directories |
-l |
Print only names of files with a match |
-L |
Print only names of files without a match |
-i |
Ignore case |
-e |
Pattern (can be repeated) |
-w |
Match whole words |