I picked this Bash cheatsheet from a GitHub site recently, can’t recall where but it immediately made its way into my personal Markdown notes. Keep handy if you work with ’nix OSes on-the-daily, or just need a refresher.

Bash Shell Cheatsheet

The main topics of this cheatsheet include an intro to the shell, navigating around the shell, common commands, environment variables, connectors, piping, I/O redirection, permissions, and keyboard shortcuts.

Introduction to the Shell

The shell is a program, in our case, called “bash” which stands for Bourne Again Shell. How the shell works is it takes your commands and gives them to the operating system to perform. In order to interact with the shell, we use “terminal emulators” such as the gnome-terminal, eterm, nxterm, etc.

On a Linux system, files are organized in a hierarchical directory structure. This means there is a starting directory called the root directory. This directory contains files and subdirectories that lead into other subdirectories.

pwd

The pwd command, short for print working directory, displays your current location in the directory structure.

cd

The cd command allows you to enter a new directory.

SyntaxExplanation
cdnavigate to home directory
cd ~navigate to home directory
cd ..navigate backwards to parent directory
cd -navigate to previous working directory
cd Directory1navigate to directory named Directory1
cd Directory1/Directory2navigate to directory, Directory2, through path

mkdir

The mkdir command makes a new directory in your current directory.

Common Commands

man

The man command directs you to the command manuals.
For example, the following command gives us all the information we need about the command cat.

1
  $ man cat

cat

The cat command reads a file passed as a parameter and by default print its contents to standard output.
Passing multiply files as parameters concatenates the files and then prints to standard output.

echo

The echo command prints its arguments to standard output.

1
2
  $ echo Hello World
  Hello World

If you call echo without any parameters, the command prints a new line.

The head command reads the first 10 lines of any passed in text and prints its contents to standard output. You can change the default 10 lines to any number by manually passing in the desired size. For example, the following prints all 50 lines of the file.

1
$ head -50 test.txt

tail

The tail command reads the last 10 lines of any passed in text and prints its contents to standard output. You can change the default 10 lines to any number by manually passing in the desired size. For example, the following prints all 50 lines of the file.

1
  $ tail -50 test.txt

You can also view in real time any text appended to the file with the -f flag.

1
$ tail -f test.txt

less

The less command gives you a way to navigate through a passed file or block of text. Unlike the more command, less allows you to move backward through the file as well.

1
2
$ less test.txt
$ ps aux | less
Common less keyboard shortcutsDescription
GMoves to end of file
gMoves to beginning of file
:50Moves to the 50th line of the file
qExits less
/searchtermSearches for any string matching ‘searchterm’ below the current line
/Moves you to the next match for your previous ‘searchterm’ below the current line
?searchtermSearches for any string matching ‘searchterm’ above the current line
?Moves you to the next match for your previous ‘searchterm’ above the current line
upMoves up a line
downMoves down a line
pageupMoves up a page
pagedownMoves down a page

true

The true command always returns the exit status zero to indicate success.

false

The false command always returns the exit status non-zero to indicate failure. ###$? $? is a variable that will return the exit code of the last command you ran.

1
2
3
4
5
6
$ true
$ echo $?
  0
$ false
$ echo $?
  1

grep

The grep command is a search function.
Passing a string and a file searches the file for the given string and prints the occurrences to standard output.

1
2
3
4
5
  $ cat users.txt
  user:student password:123
  user:teacher password:321
  $ grep 'student` file1.txt
  user:student password:123

grep can take multiple files as parameters and regular expressions to specify a pattern in text.

Common flagsDescription
-iremove case sensitivity
-rsearch recursively through directories
-wsearch only whole words
-cprints number of times found
-nprints line found on with phrase
-vprints invert match
See regex tutorial

sed

The sed command is a stream editor that performs text transformations on an input.
Common use of this command is to replace expressions which takes the form s/regexp/replacement/g For example, the following replaces all occurrences of the phrase “Hello” with “Hi”.

1
2
3
4
  $ cat test.txt
  Hello World
  $ sed 's/Hello/Hi/g' test.txt
  Hi World

See sed tutorial

history

The history command prints out an incremented command line history.
It is common to use the grep command with the history command in order to search for a particular command. For example, the following searches your history for all occurrences of the string g++.

1
2
3
  $ history | grep g++
  155  g++ file1.txt
  159  g++ file2.txt

export

The export command sets an environment variable to be passed to child processes in the environment.
For example, the following exports the variable “name” with the value “student”.

1
  $ export name=student

ps

The ps command, short for process status, prints out information about the processes running.

1
2
3
  $ ps
  PID TTY          TIME CMD
  35346 pts/2    00:00:00 bash

There are four items displayed:

  • process identification number (PID)
  • terminal type (TTY),
  • how long process has been running (TIME)
  • name of command that launched the process (CMD)

awk

The awk command finds and replaces text by searching through files for lines that have a pattern.
Syntax: awk 'pattern {action}' test.txt

wget

The wget command downloads files from the web and stores it in the current working directory.

1
  $ wget https://github.com/mikeizbicki/ucr-cs100

nc

The nc command, short for netcat, is a utility used to debug and investigate the network.
See nc tutorial

ping

The ping command tests a network connection.

1
2
3
4
5
6
  $ ping google.com
  PING google.com (74.125.224.34) 56(84) bytes of data.
  64 bytes from lax17s01-in-f2.1e100.net (74.125.224.34): icmp_req=1 ttl=57 time=7.82 ms
  --- google.com ping statistics ---
  1 packets transmitted, 1 received, 0% packet loss, time 8ms
  rtt min/avg/max/mdev = 7.794/8.422/10.792/0.699 ms

The statistics at the end show an overview of how many connections went through before we called ^C and how long it took.

git

Git is a version control system that is commonly used in the industry and in open source projects.
See git tutorial

Environment Variables

Environment variables are named variables that contain values used by one or more applications.
The PATH variable contains a list of directories where systems look for executable files.
The HOME variable contains the path to the home directory of the current user.
The PS1 variable is the default prompt to control appearances of the command prompt.

Connectors

Connectors allow you to run multiple commands at once.

ConnectorDescription
&&first command always executes and the next command will only execute if the one before it succeeds
``
;first command and the following commands always execute
1
2
3
4
5
6
7
  $ true && echo Hello
  Hello
  $ false || echo Hello
  Hello
  $ echo Hello ; ls
  Hello
  test.txt file1.txt file2.txt

Piping

Pipes connect multiple commands together by sending the stdout of the first command to the stdin of the next command. For example, the following sends the ls output to head so that only the top 10 items get printed.

1
  $ ls -l | head

Input/Output Redirection

Output Redirection

Standard output redirection uses the symbols > and >>.
For example, the following sends the output of ls into the file instead of printing to the screen.

1
2
3
  ls > files.txt
  $ cat files.txt
  file1.cpp sample.txt

If the file isn’t already in your working directory, the file gets created. If the file already exists, then the contents of the command overwrites what is already in the file.
To avoid overwriting a file, the >> command appends to the end of the file instead.

Input Redirection

Standard input redirection uses the symbol <.
For example, the following causes sort to access its input from the file instead of the keyboard.

1
2
3
4
5
6
  $ cat files.txt
  c
  b
  $ sort < files.txt
  b
  c

The sort command prints the contents of the file and prints to the screen because we haven’t redirected its output. But we can combine I/O redirection into one command line, such as:

1
  $ sort < files.txt > files_sorted.txt

Advanced Redirection

Adding a & with the > symbol results in redirecting both standard out and standard error. For example, the test.cpp file prints the string “stdout” with cout and the string “stderr” with cerr.

1
2
3
4
5
  $ g++ test.cpp
  $ ./a.out >& test.txt
  $ cat test.txt
  stdout
  stderr

The > symbol alone only redirects standard output.
If you only want to redirect a specific file descriptor you can attach the file descriptor number to >.

NameFile DescriptorDescription
stdin0standard input stream
stdout1standard output stream
stderr2standard error output stream
For example, if I only wanted to redirect “stderr” to the file test.txt from the above example, I would do the following:
1
2
3
4
5
  $ g++ test.cpp
  $ ./a.out 2> test.txt
  stdout
  $ cat test.txt
  stderr

Permissions

The command ls -l prints out a lot of information about each file that is informative about the permissions.

1
2
  $ ls -l test.txt
  -rw-rw-r--  1  user  group  1097374 January 26 2:48 test.txt
Output from example aboveDescription/Possible Outputs
  • | File type:
    -= regular file
    d= directory rw- | Permissions for owner of file rw- | Permissions for members of the group owning the file r– | Permissions for all other users user | name of user owning the file group | name of group owning the file

chmod

The chmod command, short for change mode, changes the permissions of a file.
There is a combination of letters that need to be known in order to change specific users’ permission.

LetterUser
uUser who owns it
gUsers in the group
oOther users not in the group
aAll users
You call chmod by describing which actions you want to perform and to which file.
The - symbol represents taking away permissions while the + symbol represents adding permissions.
The following example makes the file readable and writable to the user who owns it and the group.
1
2
3
  $ chmod ug+rw test.txt
  $ ls -l test.txt
  -rw-rw----  1  user  group  1097374 January 26 2:48 test.txt

Alternatively, we can use chmod with hex numbers. You can think of each permission setting as a bit where it is a 1 if there is permission for the file and 0 otherwise.

1
2
3
4
  rwx = 111 = 7
  rw- = 110 = 6
  r-x = 101 = 5
  r-- = 100 = 4

Each set of permissions represents a single digit so the following commands have the same outcome as above.

1
  $ chmod 660 test.txt

See permissions tutorial

Keyboard Shortcuts

ShortcutDescription
CTRL-AMove cursor to beginning of line
CTRL-EMove cursor to end of line
CTRL-RSearch bash history
CTRL-WCut the last word
CTRL-UCut everything before the cursor
CTRL-KCut everything after the cursor
CTRL-YPaste the last thing to be cut
CTRL-_Undo
CTRL-LClears terminal screen