Handy cheat sheet for the terminal, starting with basic commands. Note, these are for Ubuntu/Linux systems.
ls Lists directories and files
ls -a Shows all
ls -l Shows long form of listing, permissions (Output displayed in five columns. Column 1 contains a 10-character string, something like drwxrwxr-x, where 'd' is directory, 'r' is read, 'w' is write, 'x' is execute. The first 'rwx' corresponds to the user, the second set to the group, and the last to everyone else. The example above missing the 'w' would mean everyone else does not have permission to write. More on permissions is below 1. Column 2 states the user, column 3 the group, column 4 the size, and column 5 the date.)
pwd Print working directory, outputs home directory, different for each user (also represented by
cd change directory
A note about relative paths vs. absolute paths:
- Relative paths must exist according to where you currently are within a directory.
- Absolute paths begin with a
/and are relative to the home directory, therefore,
$ cd ~would take you home. May also use
..or a combination of
../..to navigate using an absolute path.
Remember the tab key is useful for autofill, as well as up and down arrow keys to scroll through previous commands.
less program to view/edit files, same for
cat. Use arrow keys to navigate one line at a time, or spacebar to scroll one page at a time.
nano is the editor I've been using to work on the remote terminal for this blog through DigitalOcean.
cp copy, but unlike
mv, will leave the original file in place. By default,
cp only works on files. To recursively copy a directory, use in conjunction with
-r flag; for example,
$ cp -r directoryname.
rm remove (Again, for directories, recursively, pass
mkdir make directory. To create nested directories with
mkdir, use in conjunction with
-p; for example,
$ mkdir -p docs/notes/sub.
Users & Permissions
whoami to see what user you are currently logged in as
adduser to add a user, may require
su swithch user; for example
$ su Username
exit ends user session and takes you back to original user
chmod changes mode of a file, and contains four characters. The first, specifies who (user, group or other), second is add or remove (
-), third ability to read, write or execute, and lastly, specify the file name. Executing this command could look something like
$ o-w hello.txt meaning permission to write to the file hello.txt have been removed for everyone else.
chown changes owner for users and groups. To change a group, use
: to separate the user and group. For example,
$ sudo chown user:group hello.txt.
sudo run command as a super user without being root or needing to know the root password
!! equivalent to running previous command, called "bangs". Helpful for use with
sudo, removes necessity to re-type a lengthy previous command; for example,
$ sudo !!.
A process is an instance of a program. There are a few tools useful when handling them.
top task manager to view processes, live updates. Use '?' to view help
ps program that lists processes, run
ps aux to view all processes for all users
grep program that searches patterns and shows filtered results for an input. Helpful to use in conjunction with
ps; for example,
$ ps aux | grep top, would list processes for the program top.
Using 'ctrl+z' will pause the current job
fg brings the most recent job back into the foreground
jobs prints list of all jobs in the current session
& sends program to the background immediately
term terminate. Also, using 'ctrl+c' signals to process you want to terminate.
kill program that sends a signal to a process. Use with PID (process id number) of process you want to kill. As a last resort,
$ kill -KILL PID or
$ kill -9 PID.
env prints out environment variables, many are automatically set by the system and appear in ALLCAPS
echo prints out a given command. With environment variables, prints out the value of the variable; for example
$HOME will return the path of the home directory. Variables can be set or changed by listing the current value, followed by '=', then a new value; for example,
$ MESSAGE="hello world". Keep in mind variables are specific to processes where they were initially set, therefore you would need to
export MESSAGE to be visible to child processes.
bash start a new session within your current session
Finding & Searching
find locate a file. To find a file by name, pass the name option; for example,
$ find . -name "hello.txt" where
. is current directory. To search system, use
grep or "global regular expression print" looks through files for patterns (regular expressions) and prints them out. To search for the word "this", for example,
$ grep "this" hello.txt.
man grep and
man find pulls up manuals for both. A few nice ways to use grep:
$ grep -n "this" will return line numbers with results,
-i removes grep's default case sensitivity, and
-v inverts matches.
Standard In's and Standard Out's
Standard in is typically the keyboard, and standard out is typically terminal. However, these can be configured in useful ways, say, to change the output to be a file instead.
< changes standard input
> changes standard output, overwrites
>> appends contents to the end of current file
| pipe input of Command A to Command B
sort sort lines of standard input and sends to standard output
These are different depending on your flavor of Linux, but the basic steps are:
- Download source file
- Untar the file
sudo make install
Permissions can also be represented in Octal Notation. Each file has a specified user, group, or other. For each of these, read, write and execute, making three sets of 'rwx' or nine total. These can also be represented numerically, where 'r' is always assigned the value of 4, 'w' the value of 2, and 'w' the value of 1. When added together, 4+2+1, the sum is 7. Therefore, rwxr-xr-- would be represented as 754. Absence of all three, ---, is always 0. Therfore, rw-r----- would be 640. ↩