EasyDebug.NET

Linux Command Cheat Sheet

Ten groups from files to networking, plus process signals

126 commands

ls -lahFiles

List files with hidden entries and human-readable sizes.

ls -lah /var/log
cd /pathFiles

Change directory; cd - returns to the previous one.

cd /etc/nginx && cd -
pwdFiles

Print the current working directory.

pwd
cp -r src dstFiles

Copy directories recursively; -a keeps permissions and timestamps.

cp -a /etc/nginx /backup/nginx
mv src dstFiles

Move or rename files and directories.

mv app.log app.log.bak
rm -rf dirFiles

Recursive forced delete. Destructive: list the path first.

rm -rf /tmp/build
mkdir -p a/b/cFiles

Create directories recursively, including missing parents.

mkdir -p /data/app/logs
touch fileFiles

Create an empty file or update timestamps.

touch /tmp/health.ok
stat fileFiles

Show permissions, size, and timestamps of a file.

stat /etc/nginx/nginx.conf
file targetFiles

Identify the real type of a file from its content.

file /data/export.bin
tree -L 2Files

Print a directory tree limited to two levels.

tree -L 2 -I node_modules
find /path -name "*.log"Files

Find files by name with wildcard support.

find /var/log -name "*.log" -size +100M
find /path -mtime +7 -deleteFiles

Delete files last modified more than seven days ago.

find /tmp -name "*.tmp" -mtime +7 -delete
locate keywordFiles

Look up paths from an index, much faster than find.

sudo updatedb && locate nginx.conf
cat fileText

Print a whole file; fine for small ones.

cat /etc/os-release
less fileText

Page through large files; slash searches, q quits.

less /var/log/nginx/access.log
head -n 20 fileText

Show the first lines of a file.

head -n 20 access.log
tail -n 50 -f fileText

Follow the end of a file in real time.

tail -f /var/log/nginx/error.log
grep -rn "keyword" dirText

Search recursively and show line numbers.

grep -rn --include="*.ts" "TODO" src/
grep -v "pattern" fileText

Invert the match and print non-matching lines.

grep -v "^#" nginx.conf
sed -i "s/old/new/g" fileText

Replace in place; -i edits the file directly.

sed -i "s/8080/8081/g" config.yml
sed -n "10,20p" fileText

Print only a range of lines.

sed -n "10,20p" app.log
awk "{print $1, $3}" fileText

Extract fields by column; $0 is the whole line.

awk "{print $1}" access.log | sort | uniq -c
cut -d: -f1 fileText

Cut columns by delimiter, good for fixed formats.

cut -d: -f1 /etc/passwd
sort -u fileText

Sort and dedupe; -n numeric, -r reverse.

sort -nr access.log | head
uniq -cText

Count adjacent duplicates; sort first.

sort access.log | uniq -c | sort -nr
wc -l fileText

Count lines; -c counts bytes.

wc -l access.log
dos2unix fileText

Convert Windows CRLF line endings to Unix LF.

dos2unix win.txt
tr -s " "Text

Squeeze repeated characters, handy for extra spaces.

tr -s " " < in.txt > out.txt
xargs -I{} cmd {}Text

Build command arguments from standard input.

find . -name "*.tmp" | xargs rm -f
tee fileText

Write to both stdout and a file.

echo "1.2.3" | sudo tee /etc/app/version
diff -u a bText

Compare two files in unified format.

diff -u old.conf new.conf
chmod 755 filePermissions

Set permissions: owner rwx, others read and execute.

chmod -R 755 /var/www/html
chmod -R 644 dirPermissions

Make files readable by all and writable by owner only.

chmod -R 644 /etc/app/*.conf
chown -R user:group dirPermissions

Change owner and group recursively.

chown -R www-data:www-data /var/www
chgrp group filePermissions

Change only the group.

chgrp developers /srv/project
umask 022Permissions

Set the default permission mask for new files.

umask 022
useradd -m -s /bin/bash adaPermissions

Create a user with a home directory and login shell.

useradd -m -s /bin/bash ada
usermod -aG docker adaPermissions

Append a user to a supplementary group; without -a the others are replaced.

usermod -aG docker ada
passwd userPermissions

Change the password of a user.

sudo passwd ada
su - userPermissions

Switch user and load that environment.

su - postgres
sudo -iPermissions

Start a login shell as root.

sudo -i
idPermissions

Show the current uid, gid, and groups.

id
groups userPermissions

List all groups a user belongs to.

groups www-data
ps auxProcesses

Snapshot all processes; filter with grep.

ps aux | grep -v grep | grep nginx
topProcesses

Live CPU and memory view; M sorts by memory.

top -b -n 1 | head -20
htopProcesses

Friendlier interactive process viewer; install separately.

htop
kill <pid>Processes

Send SIGTERM to ask a process to exit gracefully.

kill 12345
kill -9 <pid>Processes

Force kill; may lose data, use only when SIGTERM fails.

kill -9 12345
pkill -f "java -jar"Processes

Kill processes matching the full command line.

pkill -f "java -jar app.jar"
killall nginxProcesses

Kill all processes with the given name.

killall -HUP nginx
jobs / bg / fgProcesses

List background jobs and move them between foreground and background.

jobs -l
nohup cmd &Processes

Keep a process running after logout, logging to nohup.out.

nohup ./start.sh > app.log 2>&1 &
nice -n 10 cmdProcesses

Start with lower priority to avoid starving key services.

nice -n 10 tar -czf backup.tgz /data
renice -n -5 -p <pid>Processes

Adjust the priority of a running process.

sudo renice -n -5 -p 12345
watch -n 2 cmdProcesses

Re-run a command every two seconds.

watch -n 2 "df -h /"
lsof -p <pid>Processes

List files and sockets opened by a process.

lsof -p 12345 | head
uname -aSystem

Show kernel version and architecture.

uname -a
uptimeSystem

Show uptime and load averages.

uptime
free -hSystem

Show memory and swap usage.

free -h
vmstat 1System

Report CPU, memory, IO, and context switches every second.

vmstat 1 5
iostat -x 1System

Show per-disk IO utilization and wait.

iostat -x 1
envSystem

Print all environment variables.

env | grep PATH
export KEY=valueSystem

Set an environment variable for the current session only.

export NODE_ENV=production
historySystem

Show command history; !! repeats the last command.

history | grep systemctl
alias ll="ls -lah"System

Define a command alias.

alias ll="ls -lah"
date -d "@1700000000"System

Format a timestamp as a date.

date -d "@1700000000" "+%F %T"
timedatectlSystem

Show and set timezone and NTP sync state.

timedatectl set-timezone Asia/Shanghai
ulimit -nSystem

Show or set the file descriptor limit.

ulimit -n 65535
sysctl -w net.core.somaxconn=1024System

Temporarily change a kernel parameter.

sysctl -w net.ipv4.ip_forward=1
crontab -eSystem

Edit the crontab of the current user.

crontab -l
ip addrNetworking

Show interfaces and addresses; replaces ifconfig.

ip -br addr
ip routeNetworking

Show the routing table and default gateway.

ip route get 8.8.8.8
ss -tulpnNetworking

List listening ports with owning processes.

ss -tulpn | grep 8080
netstat -anpNetworking

Show all connections; still common on older systems.

netstat -anp | grep ESTABLISHED
ping -c 4 hostNetworking

Connectivity test with four packets.

ping -c 4 8.8.8.8
traceroute hostNetworking

Show the route hop by hop.

traceroute example.com
dig +short example.comNetworking

Query DNS records, useful for resolution issues.

dig +short example.com A
nslookup example.com 8.8.8.8Networking

Query a specific DNS server.

nslookup example.com 8.8.8.8
curl -I https://example.comNetworking

Fetch response headers only.

curl -I -L https://example.com
curl -X POST -d "{}" urlNetworking

Send a JSON request body.

curl -X POST -H "Content-Type: application/json" -d "{}" http://localhost:8080/api
wget -c urlNetworking

Download with resume support.

wget -c https://example.com/big.iso
ssh -i key.pem user@hostNetworking

Log in to a remote host with a key.

ssh -i ~/.ssh/id_ed25519 ada@10.0.0.5
scp -r dir user@host:/pathNetworking

Copy directories recursively between hosts.

scp -r ./dist root@10.0.0.5:/var/www
rsync -avz --delete src/ dst/Networking

Incremental sync; --delete mirrors the source.

rsync -avz --delete ./dist/ deploy@10.0.0.5:/var/www/
nc -zv host 8080Networking

Check whether a remote port is reachable.

nc -zv 10.0.0.5 8080
tcpdump -i any port 8080Networking

Capture packets on a port; -w writes a file.

tcpdump -i any -nn port 8080 -w dump.pcap
df -hStorage

Show disk usage per filesystem.

df -h
du -sh *Storage

Summarize sizes of entries in the current directory.

du -sh /* 2>/dev/null | sort -h
lsblkStorage

Show block devices and mount points as a tree.

lsblk -f
fdisk -lStorage

List disks and partition tables.

sudo fdisk -l
mount /dev/sdb1 /dataStorage

Mount a device at a directory.

mount -o ro /dev/sdb1 /mnt
umount /dataStorage

Unmount; if busy, find the user with lsof first.

umount /data
mkfs.ext4 /dev/sdb1Storage

Format a partition. Destroys existing data.

mkfs.ext4 -L data /dev/sdb1
fsck /dev/sdb1Storage

Check and repair a filesystem; unmount first.

fsck -y /dev/sdb1
tar -czf out.tgz dirStorage

Create a gzipped tar archive.

tar -czf backup-$(date +%F).tgz /data/app
tar -xzf in.tgz -C dirStorage

Extract an archive into a directory.

tar -xzf app.tgz -C /opt
zip -r out.zip dirStorage

Create a zip archive recursively.

zip -r site.zip ./dist
xz -9 fileStorage

Compress with a high ratio at the cost of speed.

xz -9 -T0 big.sql
systemctl start <unit>Services

Start a service; enable makes it start on boot.

systemctl enable --now nginx
systemctl status <unit>Services

Show service status and recent logs.

systemctl status nginx
systemctl list-units --failedServices

List units that failed to start.

systemctl list-units --failed
journalctl -u <unit>Services

Show logs of a unit.

journalctl -u nginx --since "1 hour ago"
journalctl -fServices

Follow the system journal.

journalctl -f -p err
dmesg -TServices

Read the kernel ring buffer.

dmesg -T | tail -50
apt update && apt upgradeServices

Debian family index and package upgrade.

sudo apt update && sudo apt upgrade -y
apt install <pkg>Services

Install a package.

sudo apt install -y curl
dpkg -l | grep <pkg>Services

List installed packages and versions.

dpkg -l | grep nginx
dnf install <pkg>Services

Install on RHEL family; older releases use yum.

sudo dnf install -y nginx
strace -p <pid>Troubleshooting

Trace system calls of a process to find where it hangs.

strace -p 12345 -f -e trace=network
lsof -i :8080Troubleshooting

Find the process using a port.

sudo lsof -i :8080
ss -sTroubleshooting

Summarize socket counts.

ss -s
iptables -L -n --line-numbersTroubleshooting

List firewall rules with line numbers.

iptables -L INPUT -n --line-numbers
ufw allow 8080/tcpTroubleshooting

Allow a port with the simplified firewall.

ufw status verbose
nc -l 9000Troubleshooting

Listen on a local port for connectivity tests.

nc -l 9000
tail -f /var/log/syslogTroubleshooting

Follow the system log; the path varies by distribution.

tail -f /var/log/messages
grep -i "error" /var/log/*.logTroubleshooting

Grep error lines across logs.

grep -iE "error|fail" /var/log/app/*.log | tail -30
kill -15 <pid>Signals

SIGTERM: request a graceful shutdown, the default.

kill -15 12345
kill -9 <pid>Signals

SIGKILL: the kernel kills it immediately, no cleanup.

kill -9 12345
kill -1 <pid>Signals

SIGHUP: services often reload their configuration.

kill -1 $(cat /run/nginx.pid)
kill -2 <pid>Signals

SIGINT: same as pressing Ctrl+C.

kill -2 12345
kill -3 <pid>Signals

SIGQUIT: the JVM dumps thread stacks and exits.

kill -3 12345
kill -18 / -19 <pid>Signals

SIGCONT and SIGSTOP: resume and pause a process.

kill -19 12345 && kill -18 12345
kill -10 <pid>Signals

SIGUSR1: user-defined, for example reopening logs in Nginx.

kill -10 <nginx master pid>
kill -lSignals

List all signal numbers and names.

kill -l

Something broken or missing?

Send feedback
Author's Blog
Share an idea