The Linux CLI Commands Handbook#
Table of Contents#
- Preface
- The Linux Handbook (Overview)
- Conclusion
Commands covered (quick list)#
- Introduction to Linux
- man
- ls
- cd
- pwd
- mkdir
- rmdir
- mv
- cp
- open
- touch
- find
- ln
- gzip
- gunzip
- tar
- alias
- cat
- less
- tail
- wc
- grep
- sort
- uniq
- diff
- echo
- chown
- chmod
- umask
- du
- df
- basename
- dirname
- ps
- top
- kill
- killall
- jobs
- bg
- fg
- type
- which
- nohup
- xargs
- vim
- emacs
- nano
- whoami
- who
- su
- sudo
- passwd
- ping
- traceroute
- clear
- history
- export
- crontab
- uname
- env
- printenv
Preface#
The Linux Handbook follows the 80/20 rule: learn in 20% of the time the 80% of a topic.
In particular, the goal is to get you up to speed quickly with Linux. This book is written by Flavio. I publish programming tutorials on my blog flaviocopes.com and I organize a yearly bootcamp at bootcamp.dev. You can reach me on Twitter @flaviocopes.
Enjoy!
The Linux Handbook#
1. Introduction to Linux#
Linux is an operating system, like macOS or Windows. It is also the most popular Open Source and free, as in freedom, operating system. It powers the vast majority of the servers that compose the Internet. It’s the base upon which everything is built upon. Android is based on (a modified version of) Linux.
The Linux “core” (called kernel) was born in 1991 in Finland. It went on to be the kernel of the GNU Operating System, creating the duo GNU/Linux. There’s not just “one Linux” — we have distributions (distros) like Debian, Red Hat, and Ubuntu.
macOS is a UNIX-like OS and shares many commands with GNU/Linux. Windows has WSL (Windows Subsystem for Linux). Most cloud servers run Linux (VPS providers like DigitalOcean).
A shell is a command interpreter (Bash, Zsh, Fish, etc.) that lets you run programs and write scripts. In this handbook we’ll cover common commands you’ll use frequently.
2. man#
The man command shows the manual page for other commands.
Example:
man <command>Man pages are comprehensive; for quick examples use the tldr pages project (tldr <command>).
3. ls#
List files in a directory:
ls
ls /bin
ls -al /binThe -l option shows detailed info (permissions, links, owner, group, size, modified date, name). The -a option shows hidden files (those starting with .).
4. cd#
Change directory:
cd <directory>
cd .. # parent directory
cd /etc # absolute pathExample:
mkdir fruits
cd fruits5. pwd#
Print working directory:
pwd6. mkdir#
Create directories:
mkdir fruits
mkdir dogs cars
mkdir -p fruits/apples7. rmdir#
Remove empty directories:
rmdir fruits
rmdir fruits carsTo remove directories with contents, use rm -rf (dangerous):
rm -rf fruits cars8. mv#
Move or rename files:
touch pear
mv pear new_pear
mv pear apple fruits # move multiple files into folder9. cp#
Copy files and directories:
cp apple another_apple
cp -r fruits cars10. open (macOS)#
Open files or directories on macOS (opens Finder or apps):
open <filename>
open <directory>
open .
open <application name>11. touch#
Create an empty file or update its timestamp:
touch apple12. find#
Search for files/dirs recursively. Examples:
find . -name '*.js'
find . -type d -name src
find folder1 folder2 -name filename.txt
find . -type d -name '*.md' -not -path 'node_modules/*'
find . -type f -size +100c
find . -type f -mtime -1
find . -type f -mtime -1 -delete
find . -type f -exec cat {} \;Notes: use quotes around * to avoid shell expansion. Use -iname for case-insensitive name matching.
13. ln#
Create links (hard or symbolic):
Hard link:
ln original link
ln recipes.txt newrecipes.txtSymbolic (soft) link:
ln -s original link
ln -s recipes.txt newrecipes.txt14. gzip#
Compress files with gzip:
gzip filename
gzip -c filename > filename.gz # keep original
gzip -k filename # keep original (if supported)
gzip -1 filename # compression level 1..9
gzip -r a_folder # recursive
gzip -d filename.gz # decompress15. gunzip#
Equivalent to gzip -d:
gunzip filename.gz
gunzip -c filename.gz > anotherfilename16. tar#
Create and extract archives:
tar -cf archive.tar file1 file2
tar -xf archive.tar
tar -xf archive.tar -C directory
# gzipped tar
tar -czf archive.tar.gz file1 file2
tar -xf archive.tar.gz17. alias#
Create shell aliases:
alias ll='ls -al'
alias lsthis="ls $PWD"
alias lscurrent='ls $PWD'To make aliases permanent, add them to ~/.bashrc, ~/.profile, ~/.bash_profile, or your shell config file.
18. cat#
Print or concatenate files:
cat file
cat file1 file2
cat file1 file2 > file3 # overwrite
cat file1 file2 >> file3 # append
cat -n file1 # show line numbers19. less#
View files interactively:
less file
# navigation: q to quit, / to search, ? to search backward, v to open editor, F to follow (like tail -f)20. tail#
Show end of file; follow for live updates:
tail -f /var/log/system.log
tail -n 10 file
tail -n +10 file21. wc#
Count lines, words, bytes:
wc file
wc -l file # lines
wc -w file # words
wc -c file # bytes
wc -m file # characters (multibyte aware)22. grep#
Search text using patterns/regex:
grep 'pattern' file
grep -n 'pattern' file # show line numbers
grep -nC 2 'pattern' file # show 2 lines of context
less file | grep -n 'pattern' # pipe to grep
grep -i 'pattern' file # case insensitive
grep -v 'pattern' file # invert match23. sort#
Sort lines of text:
sort file
sort -r file # reverse
sort -n file # numeric
sort -u file # unique
ls | sort24. uniq#
Filter adjacent duplicate lines; often used with sort:
sort dogs.txt | uniq
sort dogs.txt | uniq -d # show duplicates
sort dogs.txt | uniq -u # show unique lines
sort dogs.txt | uniq -c | sort -nr # frequency sort25. diff#
Show differences between files:
diff dogs.txt moredogs.txt
diff -y file1 file2 # side-by-side
diff -u file1 file2 # unified format (git-friendly)
diff -r dir1 dir2 # recursive directories26. echo#
Print arguments or variables:
echo "hello"
echo "hello" >> output.txt
echo "The path variable is $PATH"
echo $(ls -al)
echo {1..5}Escape special characters as needed (e.g. \, $, .).
27. chown#
Change file owner and group:
chown <owner> <file>
chown flavio test.txt
chown -R <owner> <dir> # recursive
chown owner:group file
chgrp <group> file28. chmod#
Change file permissions (symbolic or numeric):
Symbolic examples:
chmod a+r filename
chmod a+rw filename
chmod o-rwx filename
chmod og-r filename
chmod -R u+rw folderNumeric examples:
chmod 777 filename
chmod 755 filename
chmod 644 filenamePermission bits: read=4, write=2, execute=1 (sum to form digits 0..7).
29. umask#
Set default file creation mask:
umask # show current mask
umask -S # human readable
umask 002
umask g+r30. du#
Estimate directory sizes:
du
du -h <directory> # human readable
du -a # include files
du -h <directory> | sort -nr | head31. df#
Show disk free/usage:
df
df -h # human readable
df <path> # show filesystem for path32. basename#
Extract final component of a path:
basename /Users/flavio/test.txt # -> test.txt33. dirname#
Extract directory part of a path:
dirname /Users/flavio/test.txt # -> /Users/flavio34. ps#
List processes:
ps
ps ax
ps axww | grep "Visual Studio Code"Columns include PID, TT, STAT, TIME, and command. Use ps axww to avoid truncation.
35. top#
Interactive real-time process viewer:
top
top -o mem # sort by memoryQuit with q or Ctrl-C.
36. kill#
Send signals to processes:
kill <PID>
kill -HUP <PID>
kill -INT <PID>
kill -KILL <PID>
kill -TERM <PID>
kill -CONT <PID>
kill -STOP <PID>Signals can also be sent by number (e.g. kill -9 <PID> for KILL).
37. killall#
Send a signal to processes by name:
killall top
killall -HUP top38. jobs#
List background jobs started from the shell:
top &
jobs
fg %1Use jobs -l to show PIDs.
39. bg#
Resume a stopped job in the background:
bg %1
bg40. fg#
Bring a background job to the foreground:
fg %1
fg41. type#
Show how a command will be interpreted by the shell (executable, builtin, function, alias):
type ls42. which#
Show the full path of an executable on PATH (does not work for aliases/builtins):
which ls43. nohup#
Run a command immune to hangups, so it continues after logout:
nohup <command> &44. xargs#
Build and execute command lines from standard input:
command1 | xargs command2
cat todelete.txt | xargs rm
xargs -p # prompt before execution
xargs -n1 -p
xargs -I % /bin/bash -c 'command2 %; command3 %'45. vim#
Modal text editor. Basics:
vi test.txt
# i to enter insert mode, esc to exit insert mode
# :w to save, :q to quit, :wq to save and quit, :q! to quit without saving
# undo: u, redo: Ctrl-rUse vimtutor to learn interactively.
46. emacs#
Powerful editor; start with:
emacs
emacs <filename>
# Exit: Ctrl-x Ctrl-c
# Tutorial: Ctrl-h tmacOS users: consider installing a modern Emacs via Homebrew (brew install emacs).
47. nano#
Simple beginner editor:
nano <filename>
# Quit: Ctrl-X48. whoami#
Print current user:
whoami49. who#
Show users logged in:
who
who -aH
who am i50. su#
Switch user:
su <username>
# su (without args) switches to root and asks for root password
exit # return to previous user51. sudo#
Run commands as another user (commonly root):
sudo <command>
sudo -i # start root shell
sudo -u flavio ls /Users/flavio52. passwd#
Change user’s password:
passwd # change your own password interactively
passwd <username> # as root, set another user's password53. ping#
Send ICMP echo requests to test reachability:
ping google.com
ping -c 2 google.comStop with Ctrl-C.
54. traceroute#
Trace network route to a host:
traceroute flaviocopes.com
traceroute -q 1 flaviocopes.com55. clear#
Clear the terminal screen (shortcut Ctrl-L):
clear
clear -x # clears screen but allows scrollback56. history#
Show command history:
history
history | grep docker
!121 # repeat command number 121
history -c # clear history57. export#
Export environment variables to child processes:
TEST="test"
echo $TEST
export TEST="test"
./script.sh # script sees exported TEST
export PATH=$PATH:/new/path
export -n TEST # unset exported flag for TEST58. crontab#
Edit scheduled cron jobs:
crontab -l
EDITOR=nano crontab -e
# Example cron line: * */12 * * * /Users/flavio/test.sh >/dev/null 2>&159. uname#
Show system information:
uname
uname -a
uname -s
uname -r
uname -mmacOS: sw_vers shows macOS-specific version details.
60. env#
Run a command with modified environment or print environment:
env USER=flavio node app.js
env -i /usr/local/bin/node app.js
env -i NAME=flavio node app.js
env -u HOME node app.js
env # list env61. printenv#
Print environment variables or a specific variable:
printenv
printenv PATH
