linux· 6 min read

Linux command line survival kit

A compact reference for moving around, inspecting files and keeping an eye on processes in a Bash shell.

Files and directories

CommandWhat it does
ls -lahLong list, hidden files, human-readable sizes
cd -Go back to the previous directory
mkdir -p a/b/cCreate nested directories
cp -r src/ dst/Copy a directory recursively
du -sh *Size of each item in the current directory
df -hFree disk space per filesystem
less file.logPage through a file (/ to search, q to quit)
tail -f file.logFollow a growing file

Permissions

Permissions are read (4), write (2) and execute (1) for user, group and others. Try the chmod calculator to see how they add up.

chmod 644 notes.txt        # rw-r--r--
chmod 755 deploy.sh        # rwxr-xr-x
chmod +x script.sh         # add execute for everyone
chown alice:dev report.md  # change owner and group
grep -rn "TODO" src/                 # recursive, with line numbers
grep -i "error" app.log | wc -l      # count matches, case-insensitive
find . -name "*.md" -mtime -7        # markdown files changed in 7 days
find . -type f -size +100M           # large files

Processes

ps aux | grep python       # find a process
top                         # live view (htop if installed)
kill 1234                   # polite stop (SIGTERM)
kill -9 1234                # force stop, last resort
command &                   # run in background
jobs; fg %1                 # list jobs, bring one back

Archives

TaskCommand
Create .tar.gztar -czf out.tar.gz dir/
Extract .tar.gztar -xzf out.tar.gz
List contentstar -tzf out.tar.gz
Zip / unzipzip -r out.zip dir/ · unzip out.zip

Shell shortcuts

  • Ctrl+R — search command history
  • Ctrl+A / Ctrl+E — jump to line start / end
  • Ctrl+W — delete the previous word
  • !! — repeat the last command; !$ — last argument of it