Unix Terminal Cheat Sheet
Below is a table of the most common unix terminal commands and what they do. Always remember that when using a terminal, your terminal is in a particular directory (also called a folder), and the commands you do are going to operate relative to that directory, or on files in that directory.
| Command | Meaning |
|---|---|
| pwd | print working directory: it shows you what folder/directory you are in |
| ls | list directory: it shows you what files and sub-directories are in here |
| ls -a | list all: it will include hidden names that begin with a dot |
| ls -l | list long: it will include detailed information about each file |
| cd dir | change directory: this changes the folder/directory that your terminal is in; dir is relative unless it begins with a / (forward slash); . and .. are special directory names that mean current directory and parent directory |
| cd | cd with no argument moves you back to your home directory |
| cat filename | prints the contents of the file onto your terminal |
| more filename | like cat, except it displays one page at a time |
| mkdir dirname | makes a new subdirectory (sub-folder) inside the current one |
| rm filename | deletes a file; be careful if you are in a Git workspace! |
| mv filename newname | renames a file, possibly moving it into a different directory; be careful if you are in a Git workspace! |
| pico filename | edits a text file inside the terminal window; control actions are listed at the bottom |
| gedit filename & | pops up a separate editor window application, and edits the file; the & symbol makes the application run in the background, so you get a command prompt back immediately |
| code . & | starts VSCode and opens the current directory as a project |
| man command | bring up a manual page that describes the command. Try “man cd” for fun! |
| Common Compiling Commands | |
| g++ -o execname filename.cpp | compile the program in filename.cpp and name the output executable program execname (often the root of the source file name) |
| g++ -c filename.cpp | compile the source file only to object code; do not try to create an executable; resulting file is filename.o |
| g++ -o execname many-files | create an executable from many source and/or object files |
| make target | use the available Makefile to build the named target |
| g++ -Wall other-compiling-args | tell g++ to warn you about everything; very good! |
| gcc options_as_above | compile a plain-C program (not C++) |
| Common Git Commands | |
| git clone URL | makes a new clone of a Git repository |
| git pull | fetches and merges external changes of a Git repo into this one |
| git add filename | tells Git to stage the file for committing |
| git commit -m ‘message’ | commits the currently staged changes, with the commit message as given; always provide a commit message! |
| git push | pushes the local commits up to the remote repository (on GitHub, perhaps) |
| git mv filename newname | changes the name of a file in a Git workspace and tells Git about the new name |
| git rm filename | removes a file in a Git workspace and tells Git about it; the file will still be in the repository! (as history) |
You can also do some special operations on your command line after the command and any arguments you give it.
| Operation | Meaning |
|---|---|
| cmdline & | put the command in the background, meaning a command prompt comes back immediately and does not wait for the command to finish |
| cmdline > filename | redirect any output from this program into the named file |
| cmdline < filename | take the contents of the file and use it as input to this command |
| cmdline | cmdline | take the output of the first command and use it as input to the second command; this is called a command pipeline |
| Ctrl-C | stop and kill whatever command is out of control, possibly your own program in an infinite loop! |
| Ctrl-Z | stop and suspend whatever command is running; you can then enter the command bg to let it continue in the background |
| Ctrl-D | end input to a command; if a command is taking input, this tells it the end of input has been reached |