Essential Unix/Linux terminal commands
Navigate, handle files, search, manage processes: the terminal foundation on Linux and macOS.
Navigate
pwd # show the current directory cd folder/ # move into "folder" cd .. # go up one level cd - # back to the previous directory ls -lah # list (detail, hidden files, human-readable sizes)
Files and folders
touch file # create an empty file (or bump its timestamp) mkdir -p a/b/c # create nested folders in a single command cp file dest # copy a file cp -r src/ dst/ # copy a folder (recursive) mv a b # move or rename rm file # delete a file rm -i file # delete, asking for confirmation first rm -r folder/ # delete a folder and everything in it
rm (especially with -r or -rf) deletes permanently — there's no trash bin to recover from. Always double-check the path before hitting enter, and use -i when unsure.
Search
find . -name "*.log" -mtime -1 # .log files modified less than a day ago grep -rn "TODO" src/ # search "TODO" recursively, with line numbers rg "pattern" # ripgrep: same idea, much faster
Read and filter
cat file # print the whole file at once less file # page through it (q to quit) head -n 20 file # first 20 lines tail -f app.log # last lines, following new ones live wc -l file # count lines sort file | uniq -c # sort then count duplicates cut -d',' -f1 data.csv # extract the 1st column of a CSV file
Processes
ps aux # list every running process ps aux | grep node # keep only processes containing "node" top # real-time process monitor htop # friendlier equivalent (needs installing separately) kill -9 <pid> # force-stop a process by its PID command & # run a command in the background jobs # list the session's background jobs fg # bring the last job to the foreground bg # resume a paused job in the background
Permissions and quick redirections
chmod +x script.sh # make it executable sudo command # run as administrator command > file # redirect output (overwrite) command >> file # redirect, appending to the end command1 | command2 # chain: output of 1 = input of 2
man command shows the full manual; command --help a quicker summary to check.
Links and archives
ln -s /path/to/target link # create a symbolic link tar -czvf archive.tar.gz folder/ # compress (gzip) tar -xzvf archive.tar.gz # extract zip -r archive.zip folder/ # compress into a zip unzip archive.zip # extract a zip
Environment variables
echo $HOME # show a variable export MY_VAR="value" # set it for the current session env # list every variable which command # path to the executable being used
An export doesn't survive closing the terminal — for a permanent variable, add the line to ~/.bashrc, ~/.zshrc or the equivalent for your shell.
Disk space and network
df -h # disk space per partition du -sh folder/ # total size of a folder ping example.com # test connectivity curl -I https://example.com # fetch just the HTTP headers
Thanks for the feedback!