Linux#

Files and Directories#

Command

Action

ls -lah

detailed list with sizes

ls -lAtr

sorted by mtime, oldest first

cd -

previous directory

pwd

print working directory

cp -a SRC DST

archive copy (preserves metadata, links)

mv OLD NEW

move / rename

rm -rf path

recursive delete

mkdir -p a/b/c

create with parents

ln -s TARGET LINK

symbolic link

stat path

metadata

readlink -f path

resolve symlinks

find . -name '*.log'

search by name

find . -mtime -7

modified in last 7 days

find . -size +100M

larger than 100 MB

find . -name '*.tmp' -delete delete matches

find . -type f -exec cmd {} \; run cmd per file

tree -L 2

show tree (depth 2)

Disk Usage#

Command

Action

df -h

free space per filesystem

du -sh dir

total size of dir

du -h --max-depth=1

sizes of immediate children

ncdu /

interactive du

lsblk

block devices

mount

show mounts

findmnt

tree view of mounts

Processes#

Command

Action

ps aux

all processes

ps -ef

System V style

ps -o pid,user,cmd --sort=-%mem | head

top memory

top / htop

interactive

btop

prettier interactive

pgrep -af nginx

find by name

pidof sshd

pkill -f pattern

kill by pattern

kill -9 PID

SIGKILL

nohup CMD &

background, survives logout

disown %1

detach job from shell

jobs

list shell jobs

Permissions and Users#

Command

Action

id

current user / groups

whoami

groups USER

chmod 644 path

set numeric mode

chmod u+x path

add execute for owner

chown user:group path

change ownership

sudo -i

interactive root shell

sudo -u user CMD

run as user

passwd

change password

getent passwd USER

user record

getent group GROUP

group record

usermod -aG group USER

add to group

Text Processing#

Command

Action

cat / less / head -20 / tail -20 / tail -f

grep -RIn 'TODO' src/

recursive grep

rg PATTERN

ripgrep (faster)

rg --files-with-matches PATTERN

sed 's/old/new/g' file

substitute

sed -i 's/old/new/g' file

in place (GNU)

awk '{ print $2 }'

column 2

awk '$3 > 100 { sum += $3 } END { print sum }'

cut -d: -f1 /etc/passwd

field 1 of colon-separated

sort | uniq -c | sort -rn

frequency counts

tr -d '\r'

remove CR

wc -l file

line count

column -t

tabularize

jq '.field'

JSON

yq '.field'

YAML

Compression and Archives#

Command

Action

tar -czf out.tgz dir/

gzip archive

tar -cJf out.txz dir/

xz archive

tar -xzf in.tgz

extract gzip

tar -tf in.tgz

list contents

zip -r out.zip dir/

unzip out.zip

gzip / gunzip

zstd / unzstd

Packages#

Debian / Ubuntu (apt):

Command

Action

sudo apt update && sudo apt upgrade

update and upgrade

sudo apt install PKG

install

sudo apt remove PKG

remove

apt search QUERY

search

apt show PKG

info

dpkg -l | grep PKG

installed packages

dpkg -L PKG

files in package

dpkg -S /path

owner of file

Fedora / RHEL (dnf):

Command

Action

sudo dnf install PKG

sudo dnf upgrade

rpm -qa | grep PKG

rpm -ql PKG

rpm -qf /path

Arch (pacman):

Command

Action

sudo pacman -Syu

sync + upgrade

sudo pacman -S PKG

install

sudo pacman -Rs PKG

remove with deps

pacman -Ss QUERY

pacman -Qi PKG

pacman -Ql PKG

Alpine (apk):

Command

Action

sudo apk update && sudo apk upgrade

sudo apk add PKG

sudo apk del PKG

apk info -L PKG

systemd#

Command

Action

systemctl status SVC

service status

sudo systemctl start SVC

sudo systemctl stop SVC

sudo systemctl restart SVC

sudo systemctl reload SVC

sudo systemctl enable --now SVC enable + start

sudo systemctl disable SVC

systemctl list-units --type=service

systemctl list-timers

journalctl -u SVC -f

follow logs

journalctl --since '1 hour ago'

journalctl -p err

priority error

Inspecting#

Command

Action

uname -a

kernel info

hostnamectl

hostname / OS / kernel

cat /etc/os-release

distro

lscpu

CPU details

free -h

memory

vmstat 1

CPU / memory / IO

iostat -xz 1

disk IO (sysstat)

lsof -i :PORT

who has port open

ss -tulpn

listening sockets

ip addr

interfaces

ip route

routes

dmesg | tail

kernel messages