Linux Cheat Sheet

Files, processes, permissions, systemd, disk, and user management on a Linux host — the commands engineers run every day.

Core DevOps13 sections · 134 commands
⌘K

Navigation

10
pwd

Print the current working directory

ls

List files in the current directory

ls -lah

List all files with sizes, human-readable

ls -la DIR | grep '^d'

Show only directories

cd DIR

Change to a directory

cd -

Jump to the previous directory

cd ~

Change to the home directory

tree DIR

Show a directory structure as a tree

realpath FILE

Print the fully resolved absolute path

readlink -f FILE

Resolve file or symlink to its target path

File Manipulation

12
touch FILE

Create an empty file or bump its mtime

cp SRC DST

Copy a file

cp -r SRC DIR/

Copy a directory recursively

mv SRC DST

Move or rename

rm FILE

Remove a file

rm -rf DIR

Force-remove recursively (careful!)

mkdir -p DIR/SUB

Create nested directories

ln -s TARGET LINK

Create a symbolic link

file FILE

Print the file type

du -sh * | sort -hr

Biggest directories in the tree, sorted

stat FILE

Full file metadata (size, perms, timestamps)

md5sum FILE

Compute an MD5 checksum

Viewing Files

11
cat FILE

Print a file to stdout

less FILE

View a file with scroll and search

head -n 20 FILE

Show the first 20 lines

tail -f FILE

Follow a file's new lines

tail -n 50 FILE

Show the last 50 lines

nl FILE

Numbered output

wc -l FILE

Count lines in a file

cut -d: -f1 FILE

Extract the first colon-delimited field

diff FILE1 FILE2

Compare two files line by line

sort -u FILE

Sort and deduplicate lines

uniq -c FILE

Count consecutive duplicate lines

Finding

9
find DIR -name '*.log'

Find files by name pattern

find DIR -mtime +7

Files modified more than 7 days ago

find DIR -size +100M

Files larger than 100 MB

find DIR -type f -exec rm {} \;

Execute a command per match

which CMD

Print the absolute path of a command

command -v CMD

Locate a command (portable)

locate FILE

Fast filename lookup from the database

grep -rn 'pattern' DIR

Recursive case-sensitive search

grep -rn -i 'error' /var/log

Case-insensitive search across logs

Text Processing

9
grep '^ERROR' FILE

Lines starting with ERROR

grep -v 'pattern' FILE

Lines that do NOT match

sed -i 's/OLD/NEW/g' FILE

Replace all matches in place

sed -n '10,20p' FILE

Print lines 10 through 20

awk '{print $1}' FILE

Print the first column

awk 'NR>1{sum+=$3} END{print sum}' FILE

Sum values in column 3

tr -d '\r' < FILE

Strip carriage returns

paste -sd, FILE

Join lines with a comma

xargs -n1 echo < FILE

Run a command per input line

Processes

13
ps aux

List all user processes

ps aux --sort=-%mem | head

Top memory consumers

pgrep -f nginx

Find PIDs by command pattern

kill PID

Send SIGTERM (graceful)

kill -9 PID

Force kill (SIGKILL)

killall nginx

Signal every process by name

pkill -f 'python app.py'

Kill by full command line

nohup CMD &

Run detached from the terminal

setsid CMD

Start a new session, fully detached

nice -n 10 CMD

Run with low priority

renice 5 PID

Lower an existing process priority

top -o %MEM

Interactive view sorted by memory

htop

Interactive process viewer

File Permissions

12
chmod 755 FILE

rwx owner, r-x group/others

chmod 644 FILE

rw owner, r group/others (default files)

chmod 750 DIR

rwx owner, r-x group, none others

chmod +x FILE

Make a file executable

chmod -R g+w DIR

Recursively add group write

chown USER:GROUP FILE

Change owner and group

chown -R USER DIR

Recursively change the owner

chgrp GROUP FILE

Change only the group

umask 022

Default permissions for new files

setfacl -m u:USER:rw FILE

Grant an ACL for one user

getfacl FILE

View a file's ACLs

lsattr FILE

Show Linux file attributes

System Information

10
uname -a

Kernel and architecture details

cat /etc/os-release

Distro name and version

uptime

Uptime and load averages

free -h

Memory usage, human-readable

df -h

Filesystem usage by mount

df -i

Inode usage (full inodes = silent failures)

lscpu

CPU model and core counts

dmesg | tail

Recent kernel messages

hostnamectl

Hostname and OS info (systemd)

timedatectl

Time zone and clock sync status

Disk & Storage

9
lsblk

Block devices and mount points

blkid

Filesystem type and UUID per partition

mount

List mounted filesystems

mount /dev/SD1 /mnt

Mount a device

umount /mnt

Unmount a filesystem

du -sh DIR

Total size of a directory

fdisk -l /dev/SDA

List partitions on a disk

iostat -x 2

Per-device I/O stats, 2s interval

pvs; vgs; lvs

LVM layout at a glance

Users & Groups

10
whoami

Current user name

id

UID, GID, and groups of the user

useradd -m -s /bin/bash USER

Create a user with home and shell

usermod -aG sudo USER

Add the user to a group (append)

passwd USER

Set or change a password

userdel -r USER

Remove a user with their home

groupadd GROUP

Create a group

su - USER

Switch user with their environment

last

Recent login sessions

getent passwd USER

Look up a user in the account database

Logs & Journal

9
journalctl -u nginx.service

Logs for one systemd unit

journalctl -u nginx -f

Follow a service's live logs

journalctl --since '1 hour ago'

Logs from the last hour

journalctl -b / --boot=-1

This boot / previous boot

journalctl -k -b

Kernel messages since boot

journalctl -p err

Errors and above only

tail -f /var/log/syslog

Follow the system log

tail -n 200 /var/log/nginx/error.log

Tail the Nginx error log

logrotate -vf /etc/logrotate.conf

Force-run log rotation

Networking

10
ip addr

IP addresses on all interfaces

ip route

Routing table

ss -tlnp

Listening TCP ports and owner processes

ss -tn state established

Established TCP connections

ping HOST

Reachability and round-trip time

hostname -I

Local IP addresses only

nc -vz HOST PORT

Check a TCP port is open

curl -I https://HOST

Fetch only response headers

resolvectl status

DNS resolver configuration (systemd)

ip link set eth0 up

Bring an interface up

Archiving & Transfer

10
tar -czf DIR.tar.gz DIR

Create a gzipped archive

tar -xzf DIR.tar.gz

Extract a gzipped archive

tar -tzf DIR.tar.gz

List contents without extracting

tar -xzf FILE.tar.gz -C DIR

Extract into a specific directory

tar -xf FILE.tar -C DIR --strip-components=1

Extract without the top-level folder

zip -r ARCHIVE.zip DIR

Create a zip archive

unzip FILE.zip -d DIR

Extract a zip

rsync -avz SRC USER@HOST:DEST

Synchronize with compression

scp FILE USER@HOST:DEST

Copy a file over SSH

dd if=FILE of=DEV bs=4M status=progress

Raw block copy (e.g. a USB stick)