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.
Quick reference — reach for these first
ls -lahLong list with perms, owner, human sizes, and dotfiles. Read column 1 for the type+mode bits.grep -rn 'PATTERN' .Recurse (-r) printing file:line (-n). Add -i to ignore case, -F for a literal string.ps auxEvery process with user, %CPU, %MEM, and command. Pipe to grep or --sort=-%mem.journalctl -u NAME -eLogs for one unit, jumped to the end (-e). Add -f to follow, --since today to scope.df -hFree space per mounted filesystem, human units. 100% Use% on / is the classic outage.Permission bits: rwx = 4 / 2 / 1 per group
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
| Task | Command | What it shows / changes |
|---|---|---|
| List a dir in detail | ls -lah /var/log | SHOWS mode bits, owner:group, human size, mtime, and hidden files. |
| Find files by name/age | find /var/log -name '*.log' -mtime -2 | SHOWS matching paths modified in the last 2 days; -type f limits to files. |
| Search file contents | grep -rniI 'timeout' /etc | SHOWS file:line of matches; -I skips binaries, -i ignores case. |
| Print a line range | sed -n '100,120p' app.log | SHOWS lines 100-120 only; awk 'NR>=100 && NR<=120' does the same. |
| Change permission bits | chmod 640 secrets.env | CHANGES mode to rw-r----- (owner rw, group r, other none). Verify with ls -l. |
| Change owner/group | sudo chown deploy:web /srv/app | CHANGES owning user:group; -R recurses a tree. |
| Check who you are / can do | id && sudo -l | SHOWS your uid/gid + groups, then which sudo commands you may run. |
| Top memory consumers | ps aux --sort=-%mem | head | SHOWS the heaviest processes with PID and RSS; swap %mem for %cpu for CPU. |
| Service state | systemctl status nginx | SHOWS active/failed, main PID, and the last log lines for the unit. |
| Tail a unit's logs | journalctl -u nginx -f --since today | SHOWS and follows that service's logs; -p err filters to errors+. |
| Where is disk going | du -xh --max-depth=1 /var 2>/dev/null | sort -h | SHOWS per-subdir size on one filesystem (-x), largest last. |
| Install a package | sudo apt install ripgrep # dnf install on RHEL | CHANGES installed set; dpkg -l PKG / rpm -q PKG confirms the version. |
| Create an archive | tar -czf logs.tgz /var/log/app | CHANGES: writes a gzip tarball. List it without extracting via tar -tzf logs.tgz. |
| Extract an archive | tar -xzf logs.tgz -C /tmp/restore | CHANGES: unpacks into -C target; -v lists each file as it lands. |
Worked example — a server runs out of disk
TRACE IT
df -h
# /dev/root 40G 40G 0 100% / <- the root fs is fullsudo du -xh --max-depth=1 / 2>/dev/null | sort -h | tail
# 37G /varsudo du -xh --max-depth=1 /var 2>/dev/null | sort -h | tail -3
# 36G /var/log <- runaway logssudo lsof +L1 | sort -k7 -h | tail
# nginx ... /var/log/app/access.log (deleted) <- space won't free until nginx reopenssudo 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 pathdf -h /
# /dev/root 40G 6G 34G 16% /Troubleshooting — symptom → first check → next check
| Symptom | First check | Establishes | Next check |
|---|---|---|---|
| Permission denied | ls -l FILE && id | Whether the mode bits / owner allow your uid+groups | Fix with chmod/chown, or re-run under sudo if it's a root-owned path |
| command not found | type -a CMD && echo $PATH | Not installed vs installed but not on PATH | Install the package, or add its dir to PATH / use the full path |
| No space left on device | df -h then df -i | Out 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 die | ps -o pid,stat,cmd -p PID | STAT D (uninterruptible I/O) or Z (zombie) vs a normal process ignoring SIGTERM | kill -9 PID for a stubborn live one; a Z needs its parent (PPID) reaped, not the zombie |
| Service failed to start | systemctl status NAME then journalctl -u NAME -e | Config error vs crash vs missing dependency, from the exit code + log tail | Fix config, systemctl daemon-reload if the unit changed, then systemctl restart NAME |
| Operation not permitted (even as root) | lsattr FILE | Whether the immutable (i) attribute is set | sudo chattr -i FILE, then retry the modify/delete |
| rm freed nothing | lsof +L1 | grep deleted | A deleted file is still held open by a running process | Restart/reload that process, or truncate the path instead of deleting it |
References
- GNU Coreutils manualgnu.org
- GNU grep manualgnu.org
- find — GNU findutils manualgnu.org
- journalctl(1) — systemd man pagesfreedesktop.org
- chmod(1) — numeric permission modesgnu.org
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.