107 lines
5.0 KiB
Markdown
107 lines
5.0 KiB
Markdown
#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:
|
||
|
||
1. 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`.
|
||
|
||
2. 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 variable
|
||
- `unset` unset a variable
|
||
- `jobs` show programs that are currently running in the background
|
||
- `fg` get a program from the background in the foreground
|
||
|
||
__Basic commands:__
|
||
- `pwd` [print working directory] self-explanatory
|
||
- `ls` [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 output
|
||
- `test` [more commonly known as `[ ... ]`] used to check whether a condition is true or false, result is passed back using exit code
|
||
- `df` [disk free] show available disk space
|
||
- `du` [disk usage] show how much space a given file or directoy is using
|
||
- `su` [switch user] self-explanatory
|
||
- `rm` [remove] remove a file or directoy
|
||
- `mkdir` [make directory] self-explanatory
|
||
- `find` find a file or directoy by specified criteria
|
||
- `grep` search for regular expressions in text
|
||
- `ps`
|
||
- `id`
|
||
- `uname` [UNIX name] get information about the operating system - usually name, architecture, and build information
|
||
- `chmod` [change mode] change file permissions
|
||
- `chown` [change owner] change file ownership
|
||
- `sed` [stream editor] edit text on the fly
|
||
- `vi` unholy abommination of a text editor :)
|
||
- `kill` kill a given process
|
||
- `less` or `more` 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 systems
|
||
- `nano` nice user-friendly terminal text editor available on many systems
|
||
- `htop` nice user-friendly terminal task manager
|
||
- `which` easy way to find out where the binary for a given command is located
|
||
- `sl` a great way to infuriate anyone who happens to type too quickly
|
||
- `sudo` 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 |