Ultimate Terminal Cheat Sheet

- Developer Tools -
Ultimate Terminal Cheat Sheet

What`s inside

A complete Terminal cheat sheet for everyday use.
(from beginners to advanced users).

About the Content

Everything you will ever need to know about the terminal and to make use of the command-line interface efficiently.

Ch 1 - Introduction

A shell is a user interface for accessing an operating systems services such as :

⇒ File management

⇒ Process management

⇒ Batch processing

⇒ Operating system monitoring and configuration

It is know as a shell because it is the outermost layer around the operating system.


In General, There are two types of Operating system shells :

Command line shells – provide a command-line interface (CLI) to the operating system

graphical shells – provide a graphical user interface (GUI)



The command-line interface is often referred to as the console, the terminal or simply shell.

So, When we say shell, we will be referring to the CLI, though note that there can be a graphical version of the shell as well.


shell = command line interface ( though one can argue )


Operating system shells

⇒ In Windows – MS-DOS prompt – C:\>

⇒ In OS X – Bash shell – ozesh-mac:~ oz$

⇒ In Linux – Bash shell – ozesh@warcraft-asus:~$


The Command Line Interface or CLI is an interface, where the user types a command ( a sequence of characters – typically a command followed by its parameters ) and executes it by pressing the Return key.


The program which handles the interface is called a command-line interpreter or command-line-processor.


Usage and benefits

⇒ Requires fewer system resources

⇒ Automation of repetitive tasks simplified by line editing

⇒ History mechanism for storing frequently used sequences.

⇒ System administration and maintenance


Types of CLI

Operating system command-line interfaces (helps access the operating system services)

Application command-line interfaces (helps access the services provided by the application)


In 1964 – The shell

An MIT Computation Center staff member Louis Pouzin developed the RUNCOM tool for executing command scripts while allowing argument substitution. Pouzin coined the term ‘shell‘ to describe the technique of using commands like a programming language ( commands used as building blocks for writing more commands).


Later on, after going through Christopher Strachey, a British scientist’s macro-generator design, he wrote a paper on how to implement the idea in the Multics operating system


In 1965 – Schroeder’s Multics shell

After Pouzin returned to his native France in 1965, An American Software engineer – Glenda Schroeder, noted and implemented the first command-line user interface shell.


In 1971 – The Thompson shell

Modeled after Schroeder’s Multics shell, Ken Thompson wrote the first Unix shell ( V6 shell ) that was introduced in the first version of Unix.

It was a simple command interpreter, not designed for scripting.

It introduced several innovative features to the command-line interface and let to the development of the later Unix shell.


Mid-1975 – The Mashey shell (PWB shell)

It was a modified version of the Thompson shell that was maintained by John Mashey, Dick Haight, Alan Glasser.

It was released with Version 5 Unix and was available till Version 6 Unix.


In 1977 – The Bourne shell (sh)

It was developed by Stephen Bourne at Bell Labs and was used as the default shell for Version 7 Unix as a replacement for the V6 shell.


It was commonly used as an interactive command interpreter and as a scripting language. It contains most of the features that are commonly considered to produce structured programs.


Bourne shell let to the development of many other categories of shells like:

⇒ Korn shell (ksh)

⇒ Almquist shell (ash)

⇒ Bourne-again shell (Bash)


Ch 2 - Open up a Terminal

Windows comes with its own flavors of operating system shells such as the Command Prompt, the Powershell and the Windows Subsystem for Linux. Or you can always switch to a Unix style shell.


To open up a shell :

Command Prompt: Press Win + R→ type cmd→ press Enter ↵

Powershell: Press Win + R → type powershell→ press Enter ↵

Unix Shells:

⇒ Install Git for windows ( recommended)

⇒ Install Windows sybsystem for Linux

⇒ Install a Unix/Linux Emulator or a Secure Shell (SSH)


The macOS operating system includes the Terminal ( Terminal.app) as its default Terminal emulator. You can choose from a variety of shells such as Korn shell, tcsh, bash, zsh etc to use within your terminal emulator.

Zsh is the default Unix Shell for macOS Catalina or later releases.

For macOS Mojave or earlier releases, the default Unix Shell is Bash.


Open up a Terminal via Finder:

⇒ In Finder→ select the Go menu→ select Utilities→ locate Terminal in the Utilities folder → open it.

⇒ In Finder→ select the Go menu→ use the macSpotlight→ search Terminal→ press return ↵.


Which shell is your terminal emulator currently using ?

type echo $SHELL in the terminal window and press return ↵

which-shell .

Some familiar terminal emulators found in Linux distros are Gnome Terminal, KDE Konsole and xterm.

The default Unix Shell for Linux operating systems is usually Bash.


You can easily access the unix shell in Linux via the applications menu or the search bar by running the terminal emulator installed on the machine.


Simple ways to open up a Terminal :

⇒ Press Ctrl + Alt + T→ opens a terminal window in some linux flavors.

⇒ Press Alt + F2→ type gnome-terminal→ press enter ↵.


Check the calendar:

$ cal

List files and directories:

$ ls $ ls -l # use -l for more information

Gather information about logged in users:

$ whoami # get current user $ users # logged in users $ who # detailed logged in users information

The following commands give the same output:

$ who am i # get more detailed information $ who foo bar $ who mom love $ who -m

Ch3 - Printing Text

echo is the basic command for printing text in the terminal

It puts a new line at the end of every echo invocation by default.


$ echo “Bananas” # Using double quotes Bananas $ echo I hate apples # Using no quotes I hate apples $ echo ‘What do you like?’ # Using single quotes What do you like?


side effect: echo without quotes wont let us use special characters like ( ! ) the exclamation.


Printing special characters such as ! @ # $ %can be done by either using quotes or my escaping them with a slash \ .


$ echo Hello \! Hello ! $ echo “Hello !” Hello !


Echos can also accept escape sequences in double quoted strings as an argument. When using escape sequences use -e


$ echo “1\t2\t3” # wont work $ echo -e “1\t2\t3” 1 2 3


printf takes quoted strings or arguments delimited by spaces. It does not have a new line as in the echo command.


$ printf “%-5s %-15s %-4s\n” SN Name Points printf “%-5s %-15s %-4.2f\n” 1 Ozesh 9.98 printf “%-5s %-15s %-4.2f\n” 2 Scott 6.78 printf “%-5s %-15s %-4.2f\n” 3 Harry 10 SN  Name      Points 1   Ozesh     9.98 2   Scott     6.78 3   Harry     10.00


%-5s : string substitution with left alignment with width = 5

5 is the number of characters reserved for the variable.

If was not specified, the string would have been aligned to the right.


Format substitution characters

%s: string specifier for string output

%b: string specifier similar as %s ( allows us to interpret escape sequences with an argument )

%d: integer specifier for showing integer values

%f: floating point value specifier

%x: lowercase hexadecimal value specifier



printf format substitution


You can also apply color and background color to the text that is being printed.


$ echo -e “\e[1;31m This text is red \e[0m” This text is red


\e[1;31m : sets the color ( Replace 31 with the required color code )

\e[0m : resets the color


Color Codes:

Reset = 0, Black = 30 Red = 31, Green = 32 Yellow = 33, Blue = 34 Magenta = 35, Cyan = 36, and White = 37


Background color Codes:

Reset = 0 , Black = 40 Red = 41, Green = 42 Yellow = 43, Blue = 44 Magenta = 45, Cyan = 46, and White = 47


Ch4 - Variables & Environment Variables

An Envirnoment variable is a dynamic-named value that is often initialized on system startup and is used by the shell environment and the operating system to store special values. It can affect the way a process behaves in a computer. A running process can make use of the data stored in an environment variable in various ways.


They were introduced in their modern form in 1979 with Version 7 Unix. From PC DOS 2.0 in 1982, all succeeding Microsoft operating system, including Microsoft Windows and OS/2 also have included them as a feature although with a somewhat different syntax, usage and standard variable names.


In Unix based systems, each process has its own set of environment variables. When a process is created, it inherits a duplicate environment of its parent process unless explicitly changed by the parent. An environment variable changed in a process, will only affect itself and the child processes.


View all the environment variables related to a process or terminal:

$ env

Add a new path to the path environment:

$ PATH=”$PATH:/home/user/bin” export PATH

Ch5 - Mathematics

To assign a value to a variable, we do not use spaces between the operand, operator and the value :

$ quantity=4 # Joint operand, operator and value $ echo $quantity # prepend a $ sign to the variable 4

$ val1=4 $ val2=5 $ let result=val1+val2 $ echo $result => 9
$ val1=4 $ let val1++ $ echo $val1 => 5 $ let val1– $ 4
$ val1=4 $ let val1+=6 => 10 $ let val1-=6 => 4

bc or Basic Calculator is a simple command line utility that allows you to perform scientific/financial calculations.

Interactive mode:

$ bc bc 1.06 Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty’. a=30 b=2 a+b 32 quit

Non-Interactive mode:

$ echo “4 * 0.56” | bc 2.24 $ val1=10 $ echo “$val1 * 5.56” | bc 55.60 $ result=`echo “$val1 * 5.56” | bc` 55
$ echo “scale=2;3/8” | bc .37$ echo “scale=3;3/8” | bc .375
$ echo “scale=2;3/8” | bc .37 $ echo “scale=3;3/8” | bc .375
$ no=100 $ echo “obase=2;$no” | bc 1100100 $ echo “obase=10;ibase=2;$no2” | bc 100
$ echo “sqrt(100)”| bc 10 $ echo “10^2” | bc 100

Basic Movements

Move back one characterCtrl + b
Move forward one characterCtrl + f
Delete current characterCtrl + d
Delete previous characterBackspace
UndoCtrl + _
Move to the start of lineCtrl + a
Move to the end of lineCtrl + e
Move forward a wordMeta or Alt + f
Move backward a wordMeta or Alt + b
Clear the screenCtrl + l or Command + k (on mac)
Cut starting from the cursor to the end of lineCtrl +k
Cut starting from cursor to the end of wordMeta or Alt + d
Cut starting from cursor to the start of wordMeta or Alt + Backspace
Cut from cursor to previous whitespaceCtrl + w
Paste the last cut textCtrl + y
Loop through and paste previously cut textMeta + y (use it after Ctrl + y)
Loop through and paste the last argument of previous commandsMeta + .
Search as you type(repeat)Ctrl + r and type the search term
Search the last remembered search termCtrl + r twice
End the search at current history entryCtrl + j
Cancel the search and restore original lineCtrl + g

Meta is the Alt key.


Enabling Meta key on a Mac

Open Terminal > Preferences > Settings > Keyboard and enable it.

Use option as meta key. Meta key, by convention, is used for operations on word.


Ch6 - Files & File Descriptors

A Unix file system consists of ordinary files, special files and directories that are organized in a tree like structure where a single directory can hold multiple files and directories.


Basic Navigation:

Goto home directory

$ cd ~

Goto last directory

$ cd –

Move back one or more directories:

$ cd .. # move back one directory $ cd ../.. # move back multiple directories

Print working directory

$ pwd

List contents within a directory

$ ls directory-name

Changing directory

$ cd directory-name-or-path

List files and directories

$ ls

List files and directories

$ ls -l # use -l for more information total 16 -rw-r–r– 1 ojesh staff 15 Feb 28 13:00 notes -rw-r–r– 1 ojesh staff 10 Feb 28 13:32 readme.txt drwxr-xr-x 2 ojesh staff 64 Feb 28 13:31 test Info: 1st column: represents file type and permission 2nd column: number of memory blocks used 3rd column: file owner 4th column: group of the owner 5th column: file size in bytes 6th column: created or modified date 7th column: file or directory name

Metacharacter ( * ):

Use * to match 0 or more characters

$ ls readme* # look for file with prefix – readme $ ls *.txt # files with the txt extension

Display hidden files :

$ ls -a

Create a directory

$ mkdir {dir-name}

Remove a directory

$ rmdir {dir-name}

Create a file

$ touch your-file-name

Edit a file

$ vi your-file-name # depends on your file editor

View file content:

$ cat your-file-name $ cat -b your-file-name # display line number

Count words:

$ wc your-file-name

Copy files:

$ cp source-file destination-file

Rename a file:

$ mv old-file-name new-file-name

Delete a file:

$ rm file-name $ rm file1 file2 # remove multiple files

Every file in Unix has permission attributes ( Owner permissions, Group permissions, Other permissions ). These permissions can be a combination of one or more modes – read, write and execute.

⇒ Read (r) ( View contents of a file )

⇒ Write (w) ( Modify or remove the contents of a file)

⇒ Execute (x) ( Run the file as a program )


Changing Permissions:

The permissions of a file or a directory can be changed using the chmod ( change mode) command

⇒ + ( Add a permission )

⇒ – ( Remove a permission )

⇒ = ( Set a permission )


User denotations:

⇒ u ( user/owner )

⇒ g ( group )

⇒ o ( other )

⇒ a ( all )


Permission Types

(0) No permission: —

(1) Execute: –x

(2) Write: -w-

(3) Execute + Write: -wx

(4) Read: r–

(5) Read + Execute: r-x

(6) Read + Write: rw-

(7) Read + Write + Execute: rwx



$ chmod 764 filename

Here, 764 means:

⇒ the owner can Read+Write+Execute

⇒ a user group can Read + Write

⇒ World can only read

Shown as : -rwxrw-r–


Set other user permission:

$ chmod o=rwx filename

Remove read permission for user:

$ chmod u-r filename

Add execute permission for the user group:

$ chmod g+x filename

Change ownership:

$ chown user filename $ chown user:group filename

Change group owner only:

$ chgrp group_name filename

They are integers that are associated with an open file or data stream ( input and output). They keep track of open files.


Reserved File descriptors:

0 – stdin ( standard input )
1 – stdout ( standard output )
2 – stderr ( standard error )


Writing to a file : (The content of the file will be truncated before writing)

$ echo “Hello my friend” > ~/abc.txt

Append to a file :

$ echo “More text” >> ~/abc.txt

View the file contents :

$ cat ~/abc.txt

Ch7 - Aliases

Create an Alias:

$ alias new_command=’command sequence’

Aliases are temporary by default. They exist until we close the current terminal.


To store the aliases permanently: add it to the ~/.bash_rc file. Commands in .bash_rc file are always executed whenever a new shell process is spawned.

$ echo ‘alias cmd=”command sequence”‘ >> ~/.bashrc