Cheatsheet
“UNIX is simple and coherent, but it takes a genius (or at any rate, a programmer) to understand and appreciate its simplicity.” — Dennis M. Ritchie
This page lists fundamental utilities and gives a short summary of their purpose:
- Parts in black are as found in recent canonical implementations (GNU coreutils, util-linux, etc.).
- Parts in blue are POSIX.2024, with links to the standard where appropriate. To maximize portability of your shell scripts, stick to the blue.
- Heavy typeface indicates fixed parts of commands, e.g. command and option names.
- Italic indicates variable parts of commands, e.g. option values and arguments.
- Brackets denote
[
optional]
parts (heavy brackets are regular text). - Alternatives are separated with a
|
- Parts which may repeat are suffixed with
...
- Commentary is given in italic serif typeface.
Note: The utilities (both as standardized and as commonly implemented) typically support many more options than shown. Only canonical invocations and their most important related options are listed.
Shell scripting
- . fileexecute commands in current environment
- :
[
ignored-arg...]
null utility - bc
[
-l]
[
file...]
arbitrary-precision arithmetic language - builtin
[
shell-builtin[
arg...]]
execute a shell built-in (suppress function lookup) - break
[
n]
exit from for, while or until loop - cd
[
dir|
-]
change the working directory - command
[
-p]
[
cmd[
arg...]]
execute a simple command (suppresses function lookup) - command
[
-p]
[
-v|
-V]
cmdlook up a command - continuecontinue for, while or until loop
- env
[
-i]
[
-S]
[
name=value]...
[
cmd[
arg...]]
execute a command with modified environment - envprint current environment
- eval
[
arg...]
construct command by concatenating arguments - exec
[
cmd[
arg...]]
perform redirections in current shell, or execute a utility - exit
[
n]
cause the shell to exit - export name
[
=word]
...set the export attribute of a variable - export
[
-p]
print exported variables - falseexit with a non-zero exit code
- getopts opt-string var-name
[
param...]
- hash -rforget remembered utility paths
- hash
[
utility]
print remembered utility paths - pause
[
ignored-arg...]
don't exit, efficiently - pwdprint the working directory
- read
[
-r]
1[
-d delim]
var...
read from standard input into shell variables - readonly name
[
=word]...
set the readonly attribute of a variable - readonly
[
-p]
print readonly variables - return
[
n]
return from a function or dot script - set
[
-eux]
[
+eux]
[
-o option]
[
+o option]
[
--]
[
arg...]
set options and arguments - shift
[
n]
shift positional parameters - sleep number
[
s|
m|
h|
d]
sleep for a given time interval - test
[
expr]
- where expr is:
- ! exprnegation
- -e pathpath exists
- -d pathpath exists and is a directory
- -f pathpath exists and is a regular file
- -p pathpath exists and is a pipe
- -s pathpath exists and is a file with size greater than zero
- -n stringstring has non-zero length
- -z stringstring has zero length
- s1 = s2string s1 and s2 are identical
- s1 != s2string s1 and s2 are not identical
- s1 > s2string s1 collates after s2 (locale-aware)
- s1 < s2string s1 collates before s2 (locale-aware)
- n1 -eq n2integers n1 and n2 are equal
- n1 -ne n2integers n1 and n2 are not equal
- n1 -lt n2integer n1 is less than integer n2
- n1 -le n2integer n1 is less than or equal to integer n2
- n1 -gt n2integer n1 is greater than integer n2
- n1 -ge n2integer n1 is greater than or equal to integer n2
- where expr is:
- [2 expr ]same as test expr but more readable
- timeout
[
-fp]
[
-k time]
[
-s signal]
duration cmd[
arg...]
execute with a time limit - trap
[
action EXIT|
signal...]
trap signals - trueexit with a zero exit code
- umask
[
-S]
[
mask]
get or set the file mode creation mask - unset
[
-fv]
name...
unset variables and functions - wait
[
pid...]
await process completion
Man pages
- man
[
-k]
[
section]
[
name]
display manual pages - apropos
[
expression]
search manual page database (same as man -k) - whatis
[
expression]
give a command summary (same as apropos -f, man -kf)
Text processing
Traditional
- awk
[
-F sep]
[
-v var-assignment]...
program[
arg...]
pattern scanning/processing language - awk
[
-F sep]
-f program-file[
-f program-file]...
[
-v var-assignment]...
program[
arg...]
- cat
[
-v]
[
file...]
concatenate files - comm
[
-123]
[
-z]
file1 file2select or reject lines common to two files - cut
[
-d delimiter]
[
-f fields]
[
-z]
[
file...]
select fields of input - date
[
-u]
[
-d date]
[
+format]
output formatted date and time - diff?
- fold?
- grep
[
-E|
-F]
[
-iv]
pattern[
file...]
print matching lines - grep
[
-E|
-F]
[
-iv]
-f pattern-file[
-f pattern-file]...
[
-e pattern]...
[
file...]
- grep
[
-E|
-F]
[
-iv]
-e pattern[
-e pattern]...
[
-f pattern-file]...
[
file...]
- iconv
[
-c]
[-f from-enc[
-t to-enc]
[
file...]
convert between encodings - iconv
[
-c]
[
-f from-enc]
-t to-enc][
file...]
- iconv -llist supported source end destination encodings
- join?
- od
[
-A addr-base]
[
-j skip]
[
-N count]
[
-t format]
[
--endian=big|
little]
[
file...]
- paste
[
-d delimiters]
[
-s]
[
-z]
[
file...
]
merge corresponding lines of input - patch
[
-i patch-file]
[
-o output-file]
[
-p n]
[
-u]
[
file]
apply a diff - printf format
[
arg...]
write formatted output- where the following format modifiers are recognized3:
- %sstring
- %ddecimal integer
- %xhexadecimal integer
- %ffloat
- %%literal %
- where the following format modifiers are recognized3:
- sed
[
-E]
[
-isuffix]
4[
-n]
[
-z]
script[
file...]
transform text on the fly - sed
[
-E]
[
-isuffix]
4[
-n]
[
-z]
-e script[
-e script]...
[
-f script-file]
[
file...]
- sed
[
-E]
[
-isuffix]
4[
-n]
[
-z]
-f script-file[
-f script-file]...
[
-e script]
[
file...]
- where some example scripts are3:
- s/pattern/replacement/replace occurrences of pattern with replacement
- npprint n-th line
- /pattern/,$ddelete lines between first occurrence of pattern and end of input
- where some example scripts are3:
- sort
[
-o output-file]
[
file...]
- split
[
-a suffix-length]
[
-b n[
unit]]
[
-l lines]
[
file[
name]]
split a file in parts- without unit, defaults to bytes; valid unit is3:
- kkiB
- mMiB
- K
|
M|
G|
etc.kibi-, mebi-, gibi-, ... bytes (IEC) - KB
|
MB|
GB|
etc.kilo-, mega-, giga-, ... bytes (SI)
- without unit, defaults to bytes; valid unit is3:
- strings
[
-n min-len]
[
-t offset-format]
[
file...]
find printable strings - tee
[
-a]
[
-p]
[
file...]
duplicate standard input - tr
[
-cds]
string1 string2translate or delete characters- where string1 and string2 may be3:
- [a-z]characters between a and z (inclusive, no multibyte support)
- [[:upper:]]uppercase characters (no multibyte support)
- [[:lower:]]lowercase characters (no multibyte support)
- [[:space:]]whitespace (no multibyte support)
- [[:alnum:]]alphanumeric characters (no multibyte support)
- where string1 and string2 may be3:
- uniq
[
-cdu]
[
-f skip]
[
-s skip]
[
-z]
[
file[
output-file]]
keep or remove duplicates - wc
[
-clmw]
[
-L]
[
file...]
word count - xargs
Modern
- base64
[
-d]
[
file]
encode or decode Base64 data - hexdump
[
-C]
[
-l length]
[
-s offset]
[
file...]
display file in hexadecimal - seq
[
-f format]
[
-s separator]
[
-w]
[
first[
step]]
lastprint a sequence of numbers
Inspecting contents of the file system
Traditional
- basename path
[
suffix]
keep the last component of a path - basename
[
-s suffix]
path... - df
[
-h]
[
--si]
[
file...]
report free disk space - dirname path...strip the last component of a path
- du
[
-as]
[
-bh]
[
--si]
[
file...]
estimate file space usage - file
[
--mime-type]
file...
guess the file type - find
[
path...]
[
expression]
search for files- where the expressions are3:
- ( expression )grouping
- ! expressionnegation
- expression
[
-a]
expressionand (default) - expression -o expressionor
- -deletedelete files and directories, implies
-depth
- -depthprocess files depth-first
- -exec cmd
[
arg...
]
\; execute cmd for each file found - -executableby current user
- -group nameowner by group with the given name (or gid)
- -maxdepth n, -mindepth nlimit search depth
- -name pattern, -iname patternname matches pattern (case insensitive)
- -path pattern, -ipath patternpath matches pattern (case insensitive)
- -print?
- -print0?
- -prunereject a subtree
- -readableby current user
- -sizesize of the file is3:
- n
[
units]
exactly n units:- 512-byte blocks if units not given
- cbytes
- kkibibytes (kiB)
- Mmebibytes (MiB)
- Ggibibytes (GiB)
- +n
[
units]
greater than (<) n units, units as above - -n
[
units]
smaller than (>) n units, units as above
- n
- -type type
,
-type typesfile type is:- bblock special file
- ccharacter special file
- ddirectory
- lsymbolic link
- pnamed pipe
- fregular file
- ssocket
- -user nameowner by user with the given name (or uid)
- -writableby current user
- where the expressions are3:
- ls
[
-ailrt]
[
-A]
[
--sort=by]
[
file...]
list files - realpath
[
-e]
[
-m]
[
--relative-to=dir]
file...
resolve a path - readlink file
...
display the contents of a symbolic link - stat
[
-c format]
file...
display file status - sha256sum
[
file...],
sha512sum[
file...]
calculate a checksum
Modern
- btrfs filesystem du
[
-s]
[
--si]
file...
calculate disk usage for a file in Btrfs - tree
[
-L levels]
[
dir...]
print a directory tree
Modifying contents of the file system
Traditional
- chgrp
[
-R]
[
-c]
[
--reference=file]
group file...
change group ownership - chmod
[
-R]
[
-c]
[
--reference=file]
mode file...
change file mode - chown
[
-R]
[
-c]
[
--reference=file]
owner[
:group]
file...
change ownership - dd
- ln
[
-sf]
[
-rT]
source target create links - ln
[
-sf]
[
-rT]
source...
target-dir - mkdir
[
-p]
[
-m mode]
dir...
make directories - mkfifo
[
-m mode]
name...
create a named pipe - mv
[
-f]
source targetmove or rename files - mv
[
-f]
source...
target-dir - rm -f
[
-rv]
[
file...]
- rm -f
[
-rv]
[
file...]
remove files or directories - rmdir
[
-p]
dir...
remove empty directories - touch
[
-acm]
[
-d datetime]
[
-r reference]
[
-t time]
file...
modify file timestamps
Modern
- fallocate
[
-l length]
[
-n]
filechange space allocation of a file - fallocate
[
-d]
[
-n]
filedig holes in a file - mknod name b
|
c major minorcreate a block or character special file - truncate
[
-c]
[
-s size]
file...
grow or shrink files
Block devices
- lsblk
[
-bfJln]
[
-o output-list]
[
device...]
list block devices - lsblk -Hlist available columns (for use with -o)
- mkfs.fstype
[
fstype-args...]
device[
fstype-args...]
create a file system - mount
[
-t type]
list mounted file systems - mount
[
-o options]
[
-t type]
device mountpointmount a file system - mount --bind
|
--rbind|
--move old-dir new-dirremount part of file hierarchy
Account and privilege control
- chsh?
- doas?
- id
[
user]
- id -G
[
-n]
[
user]
- id -g
[
-rn]
[
user]
- id -u
[
-rn]
[
user]
- last?
- lastlog?
- newgrp
[
group]
change to a new group - passwd?
- su?
- sudo?
- who
[
-abu]
display who is on the system
Process management
Traditional
Modern
- htop?
- killall?
- pgrep?
- pkill?
systemd
Service management
- systemctl
[
--user]
[
list-units]
[
--failed]
[
pattern...]
- systemctl
[
--user]
status - systemctl
[
--user]
status[
pattern...]
- systemctl
[
--user]
start pattern? - systemctl
[
--user]
stop pattern? - systemctl
[
--user]
restart pattern? - systemctl
[
--user]
reload pattern? - systemctl
[
--user]
enable[
--now]
unit? - systemctl
[
--user]
disable[
--now]
unit? - systemctl
[
--user]
list-timers - systemctl
[
--user]
edit unit? - systemctl
[
--user]
daemon-reload - systemctl
[
--user]
kill[
--kill-whom=whom]
[
--signal=signal]
pattern?
Logs
- journalctl
[
--user]
[
-u unit]
[
-b boot]
[
-e]
[
-f]
[
-n n]
- journalctl
[
--user]
--list-bootsfor use with the -b option - journalctl
[
--user]
--disk-usage - journalctl
[
--user]
--vacuum-size=nM|
Gdelete oldest logs to fit size limit - journalctl
[
--user]
--vacuum-time=ndaysdelete logs older than n days
System
- hostname?
- uname?
- reboot?
- poweroff?
- dmesg?
- lsmod?
- modprobe?
- free?
- iotop?
- sysctl?
Misc
- gdb?
- less
[
-R]
file? - strace?
- stty?
- time?
- watch
[
-d]
[
-n fractional-seconds]
cmd[
arg...]
refresh command output regularly