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 > FILEredirects stdout of COMMAND into FILE.COMMAND 2>FILEredirects stderr into FILE.COMMAND 2>&1redirects 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.exitexit (optionally with an exit code though that feature is more useful for scripts)exportmake the given shell variable an environment variableunsetunset a variablejobsshow programs that are currently running in the backgroundfgget 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.touchechooutput 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-explanatoryfindfind a file or directoy by specified criteriagrepsearch for regular expressions in textpsiduname[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 flyviunholy abommination of a text editor :)killkill a given processlessormoreprint 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:
freeshows information about used/free RAM on Linux systemsnanonice user-friendly terminal text editor available on many systemshtopnice user-friendly terminal task managerwhicheasy way to find out where the binary for a given command is locatedsla great way to infuriate anyone who happens to type too quicklysudobecome 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