← All cheatsheets

Linux

Linux Commands — files, permissions, processes, logs & storage

The everyday Linux toolkit grouped by what each command SHOWS or CHANGES — files, search, permissions, processes, services, logs, storage, packages, and archives — with a disk-fills-up worked trace.

Docs-checkedfilespermissionsprocesseslogsstoragepackages

Quick reference — reach for these first

listls -lahLong list with perms, owner, human sizes, and dotfiles. Read column 1 for the type+mode bits.
searchgrep -rn 'PATTERN' .Recurse (-r) printing file:line (-n). Add -i to ignore case, -F for a literal string.
processesps auxEvery process with user, %CPU, %MEM, and command. Pipe to grep or --sort=-%mem.
service logsjournalctl -u NAME -eLogs for one unit, jumped to the end (-e). Add -f to follow, --since today to scope.
diskdf -hFree space per mounted filesystem, human units. 100% Use% on / is the classic outage.

Permission bits: rwx = 4 / 2 / 1 per group

Unix permission bits decoded into an octal modeThe nine permission bits split into owner, group and other; read is 4, write is 2, execute is 1, and each group's set bits add up to one octal digit, so -rwxr-x--- equals chmod 750.ls -l-rwxr-x---= chmod 750owner (u)group (g)other (o)rwxr-x---421401000750r = 4 · w = 2 · x = 1 → add the set bits in each group for its octal digit

Text equivalent: A file mode has three groups of three bits — owner (u), group (g), and other (o). In each group read=4, write=2, execute=1, and you add the set bits to get one octal digit. The example -rwxr-x--- is owner rwx = 4+2+1 = 7, group r-x = 4+0+1 = 5, other --- = 0, giving chmod 750: the owner can read, write and run it, the group can read and run it, and everyone else gets nothing.

Common tasks

Task · Command · What it shows / changes

TaskCommandWhat it shows / changes
List a dir in detaills -lah /var/logSHOWS mode bits, owner:group, human size, mtime, and hidden files.
Find files by name/agefind /var/log -name '*.log' -mtime -2SHOWS matching paths modified in the last 2 days; -type f limits to files.
Search file contentsgrep -rniI 'timeout' /etcSHOWS file:line of matches; -I skips binaries, -i ignores case.
Print a line rangesed -n '100,120p' app.logSHOWS lines 100-120 only; awk 'NR>=100 && NR<=120' does the same.
Change permission bitschmod 640 secrets.envCHANGES mode to rw-r----- (owner rw, group r, other none). Verify with ls -l.
Change owner/groupsudo chown deploy:web /srv/appCHANGES owning user:group; -R recurses a tree.
Check who you are / can doid && sudo -lSHOWS your uid/gid + groups, then which sudo commands you may run.
Top memory consumersps aux --sort=-%mem | headSHOWS the heaviest processes with PID and RSS; swap %mem for %cpu for CPU.
Service statesystemctl status nginxSHOWS active/failed, main PID, and the last log lines for the unit.
Tail a unit's logsjournalctl -u nginx -f --since todaySHOWS and follows that service's logs; -p err filters to errors+.
Where is disk goingdu -xh --max-depth=1 /var 2>/dev/null | sort -hSHOWS per-subdir size on one filesystem (-x), largest last.
Install a packagesudo apt install ripgrep # dnf install on RHELCHANGES installed set; dpkg -l PKG / rpm -q PKG confirms the version.
Create an archivetar -czf logs.tgz /var/log/appCHANGES: writes a gzip tarball. List it without extracting via tar -tzf logs.tgz.
Extract an archivetar -xzf logs.tgz -C /tmp/restoreCHANGES: unpacks into -C target; -v lists each file as it lands.

Worked example — a server runs out of disk

TRACE IT

1
Confirm which filesystem is full and by how much. Read the Use% and Mounted on columns.
df -h
# /dev/root  40G  40G  0  100%  /    <- the root fs is full
2
Find the biggest directories ON THAT filesystem. -x stays on one device so you don't chase /proc or mounts.
sudo du -xh --max-depth=1 / 2>/dev/null | sort -h | tail
# 37G  /var
3
Descend into the offender, repeating until a single culprit stands out.
sudo du -xh --max-depth=1 /var 2>/dev/null | sort -h | tail -3
# 36G  /var/log   <- runaway logs
!
A file still held open by a process keeps its space even after rm. Check before deleting.
sudo lsof +L1 | sort -k7 -h | tail
# nginx ... /var/log/app/access.log (deleted)   <- space won't free until nginx reopens
4
Free space the safe way: truncate or rotate rather than deleting a live file. Truncating keeps the open handle valid.
sudo truncate -s 0 /var/log/app/access.log
sudo systemctl reload nginx   # or: logrotate -f /etc/logrotate.conf
# file emptied in place; nginx keeps writing to the same path
✓
Verify space actually came back. 100% -> a healthy number confirms you freed the right thing, not just a deleted-but-held file.
df -h /
# /dev/root  40G  6G  34G  16%  /

Troubleshooting — symptom → first check → next check

SymptomFirst checkEstablishesNext check
Permission deniedls -l FILE && idWhether the mode bits / owner allow your uid+groupsFix with chmod/chown, or re-run under sudo if it's a root-owned path
command not foundtype -a CMD && echo $PATHNot installed vs installed but not on PATHInstall the package, or add its dir to PATH / use the full path
No space left on devicedf -h then df -iOut of blocks vs out of inodes (many tiny files)du -xh to find the hog; truncate/rotate; delete small files if inodes are the limit
Process won't dieps -o pid,stat,cmd -p PIDSTAT D (uninterruptible I/O) or Z (zombie) vs a normal process ignoring SIGTERMkill -9 PID for a stubborn live one; a Z needs its parent (PPID) reaped, not the zombie
Service failed to startsystemctl status NAME then journalctl -u NAME -eConfig error vs crash vs missing dependency, from the exit code + log tailFix config, systemctl daemon-reload if the unit changed, then systemctl restart NAME
Operation not permitted (even as root)lsattr FILEWhether the immutable (i) attribute is setsudo chattr -i FILE, then retry the modify/delete
rm freed nothinglsof +L1 | grep deletedA deleted file is still held open by a running processRestart/reload that process, or truncate the path instead of deleting it

References

Assumes GNU coreutils on a modern Linux (systemd). macOS/BSD differ: du/df lack -x semantics and use -d N not --max-depth, ps has no --sort, stat -c is stat -f, and there is no journalctl (use log show). Placeholders in CAPS; app.shipreadymetrics.com (scm.cc) is a sample host.