Skip to content

Instantly share code, notes, and snippets.

@dkniffin
Last active April 21, 2021 06:50
Show Gist options
  • Save dkniffin/02f9e1bedd7f4d5fc62a to your computer and use it in GitHub Desktop.
Save dkniffin/02f9e1bedd7f4d5fc62a to your computer and use it in GitHub Desktop.
Terminal 101/Cheat sheet/rosetta stone

The goal of this gist is to provide a list of the most common/useful shell commands.

Commands

command stands for common usage description
man manual man display the man page for the given comand. If you're wondering how to use any command, this is the go-to place for help. It provides information about what the command does, how to use it, what flags it has, and lots more.
<command> --help While it's not an actual command, I thought it'd be useful to include it. While man will get you the most thorough information about a command, many commands don't provide man pages. Also sometimes it's useful to get a less verbose help message. In those cases, --help/-h is what you want.
<command> -h ^ see above ^
cd change directory cd <directory> move to the given directory
cd .. move up a directory
cd / move to the root of your directory tree
cd ~ move to your home directory (same as cd $HOME)
ls list ...? ls display a list of non-hidden files in your current directory
ls -la display all files in your current directory, along with file attributes, such as permission, owner, and size
cat concatenate cat <file> display (print) the contents of the given file
history history shows all commands in your history. In most shells, these are stored in a file in your home directory (eg .zsh_history, .bash_history, etc)
`history grep `
echo echo "string" display the given string. Very useful in combination with pipes
echo $VARIABLE display the given variable
pwd present working directory pwd display the path of the directory you're in
pwd -P display the full path (with symlinks expanded) of the directory you're in
ps processes ps display a list of processes running on the computer
ps aux show additional information about the processes
ps -elf another processes list output
git ?? version control. For more information specifically about git, there are many good guides out there.
rm remove rm <file> remove the given file
rm -rf <directory> DANGER the -r makes it recursive, so it will delete all directories and files below the given directory. the -f stands for "force", and it's basically saying, don't ask me about every file you're about to remove. Be very careful when you use this. It irreversibly removes files and directories!
sudo Super-User DO sudo <command> see below
brew homebrew brew install <package> (OSX only) installs the given package. For example, brew install ruby would install ruby (if it's not already). This requires installing homebrew
brew cask install <app> (OSX only) installs the given app (from the app store). This requires installing cask

Directories

The linux/mac operating system is organized into a tree structure, called a directory. Starting at the root (/), each directory (or folder), can contain any number of other directories, files, symlinks, and other special files.

Directories can be used in many commands. Often the usage will indicate where a directory should go, with something like <directory> or path or /path/to/file.

Special directories

raw text meaning
/ root of the directory tree
$HOME your home directory
~ a shortcut for $HOME
~user user's home directory
. current directory
.. up one directory

Pipes

In the terminal, another immensely useful tool is the pipe (|). In case you don't know how to type this thing, look between your enter and backspace keys. Yep, shift-\.

The pipe is used to send the output of one command into another. For example, you can use history above to print all the commands you've run. Then, if you do history | grep <word> you'll get a list of commands containing <word>.

Sudo

sudo stands for Super-User DO. It basically runs the command as a user called root, which has privileges to do anything.

Obligitory warning

sudo is dangerous. Unless you have a really good reason to use it, don't. If you don't know what you're doing, you can seriously mess up your system. That said, there are times when you do need it. For example, if you are removing files outside of your home directory, changing permissions on files you don't own, or installing packages (with some package managers).

sudo make me a sandwich

Other useful shell things

Depending on your shell, some of these may not work. They all work in zsh. Most of them work in bash.

thing what it does example
!! shortcut for "last command" rm <thing> -> "Permission denied" -> sudo !! (short for sudo rm <thing>)
!$ shorcut for "last part of last command" ls -la <thing> -> oops, I meant to cat that -> cat !$ (short for cat <thing>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment