5.0 KiB
#Shell Cheatsheet
I am only going over POSIX shell things here as there are a bunch of extensions found in some shells / on some systems but not others. The sections below should apply to any UNIX-like system. This cheatsheet will by no means be a comprehensive guide to UNIX (it’s a cheatsheet, not a handbook) but you can always find out more information about any given command right there on the shell.
Many systems come with manuals, try man COMMAND
or
info COMMAND
to find out more about most utilities
(try man man
, for example).
Alternatively, if no manual is available,
passing the flag -h
or --help
will usually print some information.
I find myself consulting these constantly because nobody is expected
to remember everything beyond the basics and it’s usually faster than googling it.
If you are trying to learn how to use the shell, I encourage you to start with the examples section at the end and work your way backwards from there to figure out what they do. I also highly recommend you check the manuals or help information for any command you want to use - especially if it was given to you by a stranger on the internet.
##Shell features:
The shell’s main tasks are providing a text based user interface to an operating system as well as running scripts. To facilitate this, the shell runs the programs the user/script specifies and deals with the output.
Streams Normally, there are two output streams and one input stream for a running program: Standard output (stdout), standard error (stderr), standard input (stdin). By default, they are passed to the stdout, stderr, and stdin of the shell, which will end up in your terminal when you run a shell interactively. There are two notable exceptions to this which you can specify:
-
Reditects: You can send the output of a command to a file instead of the standard output/error streams.
COMMAND > FILE
redirects stdout of COMMAND into FILE.COMMAND 2>FILE
redirects stderr into FILE.COMMAND 2>&1
redirects stderr into stdout. You can have multiple redirects per command though more than two usually don’t make sense. The most common example of this is redirecting stderr to stdout and the combined stdout into a file like so:COMMAND > FILE 2>&1
. -
Pipes: The output of one command can be used as the input of another:
COMMAND | OTHER_COMMAND
Variables
There are two kinds of exit code if, esle while, for pipes, redirects ctrl-c
Builtin commands:
cd
[change directoy] A shell (or any process for that matter) is always working from a specific working directory. Pretty much self-explanatory what this does.exit
exit (optionally with an exit code though that feature is more useful for scripts)export
make the given shell variable an environment variableunset
unset a variablejobs
show programs that are currently running in the backgroundfg
get a program from the background in the foreground
Basic commands:
pwd
[print working directory] self-explanatoryls
[list] Get a listing of the specified directory. If none given, the current directory is used.cat
[concatenate] Originally created to concatenate files, it is most commonly used to print the contents of a file to the standard output. It takes file paths as arguments.mv
[move] Move or rename a file or directory. It takes two arguments: Origin and destination.cp
[copy] Copy a file or directoy. It takes two arguments: Origin and destination.touch
echo
output text to standard outputtest
[more commonly known as[ ... ]
] used to check whether a condition is true or false, result is passed back using exit codedf
[disk free] show available disk spacedu
[disk usage] show how much space a given file or directoy is usingsu
[switch user] self-explanatoryrm
[remove] remove a file or directoymkdir
[make directory] self-explanatoryfind
find a file or directoy by specified criteriagrep
search for regular expressions in textps
id
uname
[UNIX name] get information about the operating system - usually name, architecture, and build informationchmod
[change mode] change file permissionschown
[change owner] change file ownershipsed
[stream editor] edit text on the flyvi
unholy abommination of a text editor :)kill
kill a given processless
ormore
print only a screen worth of text at a time and wait so you have the time to read it
Commonly used special files . .. dev null dev urandom
Honorable mentions: The following things won’t be available on all systems, but are really handy when they are:
free
shows information about used/free RAM on Linux systemsnano
nice user-friendly terminal text editor available on many systemshtop
nice user-friendly terminal task managerwhich
easy way to find out where the binary for a given command is locatedsl
a great way to infuriate anyone who happens to type too quicklysudo
become root (or any other user) by authenticating the current user instead of the target user given the current user is permitted to do so - useful when root login is disabled