Reading Time: 14 minutes

What is Bash?

Bash, or the Bourne Again SHell, is based on the Bourne shell. It is the default shell command language interpreter for GNU Linux/Unix and is accessed via a terminal. The shell was the original method for interacting with the operating system before GUIs were implemented. It has the ability to execute single commands, understand regular expressions, as well as scripts written in the bash language. It is currently the default interactive shell on most all modern Linux distributions.

What is a Terminal?

A terminal is the interface used to execute and streamline work on a computer without a GUI (graphical user interface). When a non-root user logs in, we see a tilde dollar sign, indicating this is a


In the prompt, you can see specific information relating to the user type. For example, in the prompt above we see the following information.

  1. Who the user is (a user named user).
  2. What the server name is (a server named host).
  3. What your default working directory is (~, which signifies the users home directory).
  4. What your user type is (a non-root user as noted by the '$' symbol).

The special character represented by the dollar sign ("$") is one of many special character types used as the basic building blocks of bash. The string information representing the bash prompt is stored in a special environment variable called PS1. This information indicates we are logged in as a non-root user. If you are logged in as the root user, the character would show a pound sign ("#") instead.

The bash prompt itself can be modified to show various information types depending on the information noted in the PS1 variable.

Bash Commands

The standard format for running commands on the terminal or via the command line interface (or CLI) is as follows:

Command [option(s)] [flags] [file(s)]

Control Operators

Control operators add logic to the command line allowing increased granularity and functionality.

  • semicolon (;) - Add two or more commands on the same line separated by a ';' to run them sequentially.
  • ampersand (&) - When a command ends with an ampersand, the command will execute in the background. A notification will be seen when the command completes.
  • dollar question mark ($?) - The exit code of the previous command is stored in the shell parameter $? so it can be reused in a follow-up command.
  • double ampersand (&&) - When && is used, the second command is run only if the first command completes.
  • double vertical bar (||) - The second command is executed only if the first command fails.
  • using (&&) and (||) - Using both options in a command creates a logical AND or logical OR statement similar to an if-then-else statement on the command line.
root@host:~# rm file && echo Success! || echo Failure!
rm: cannot remove file?: No such file or directory
  • pound sign (#) - Essentially a command comment. Anything written after a pound sign is ignored by the shell.
  • escaping special characters (\) - The backslash \ enables the terminal to use escape characters.
root@host:~# echo dog \; cat
dog ; cat
root@host:~# dog\ \ \ cat
dog   cat
  • end of line backslash(/) - a command ending in a backslash is continued on the next line. The shell will wait on the expansion and execution of a command until a new line without backslash is seen.
root@host:~# echo The dog is a\
> good \
> boy.
The dog is a good boy.

Linking Commands

One of the most significant options when using the Linux CLI is the ability to link or “pipe” commands together using the pipe symbol. The pipe symbol “|”, if used between commands, allows us to run command 1, capture the output of that command, and then feed that output into command 2 and command 3. 

The power of piping lies in its ability to take a large amount of data, sort through it quickly, output the needed info, and push it into the next command. Additional command filters are used to separate out more refined data. This process can effectively be used to filter through gigabytes of log data easily.

In the example below, we are using a souped-up du command to pull directory info, and then sorting through that directory, and then using awk, apply multiple filters to sort again by Kb, Mb, and Gbs.

du -sk ./* | sort -nr | awk 'BEGIN{ pref[1]="K"; pref[2]="M"; pref[3]="G";} { total = total + $1; x = $1; y = 1; while( x > 1024 ) { x = (x + 1023)/1024; y++; } printf("%g%s\t%s\n",int(x*10)/10,pref[y],$2); } END { y = 1; while( total > 1024 ) { total = (total + 1023)/1024; y++; } printf("Total: %g%s\n",int(total*10)/10,pref[y]); }'

This demonstrates the power of bash to easily gather, sort, and classify data.

The xargs command takes the output of one command and pipes it out as an argument of a second command. While commands like find or grep can accept standard input as a parameter, other commands cannot. Using xargs, it allows commands like rm, echo and mkdir to accept standard input as an argument.

xargs [options] [command [initial-arguments]]
tar -tf file.tar.gz | xargs rm -r

Bash Keyboard Shortcuts

  • CTRL+B - moves backward one character
  • CTRL+C - halts the current command
  • CTRL+D - deletes one character back or logs out of the current session
  • CTRL+E - moves to end of line
  • CTRL+F - moves forward one character
  • CTRL+G - aborts the current editing command /rings the terminal bell
  • CTRL+H - deletes one character under cursor (same as DELETE)
  • CTRL+J - same as RETURN
  • CTRL+K - deletes (kill) forward to the end of line
  • CTRL+L - clears the screen and re-display the line
  • CTRL+M - same as RETURN
  • CTRL+N - next line in the command history
  • CTRL+O - same as RETURN, then displays next line in the history file
  • CTRL+P - previous line in the command history
  • CTRL+R - searches backward
  • CTRL+S - searches forward
  • CTRL+T - transposes two characters
  • CTRL+U - kills backward from point to the beginning of the line
  • CTRL+V - makes the next character typed verbatim
  • CTRL+W - kills the word behind the cursor
  • CTRL+X - lists the possible filename completions of the current word
  • CTRL+Y - retrieves (yank) the last item killed
  • CTRL+Z - stops the current command, resume with fg in the foreground or bg in the background
  • ALT+B - moves backward one word
  • ALT+D - deletes next word
  • ALT+F - moves forward one word
  • ALT+H - deletes one character backward
  • BACKSPACE - deletes one character backward
  • DELETE - deletes one character under the cursor
  • history - shows command line history
  • !! - repeats the last command
  • !<n> - refers to command line 'n'
  • !<string> - refers to command starting with 'string'
  • exit - logs out of current session

Bash Basics

  • env - This command displays all environment variables
  • echo $SHELL - This command displays the shell you are using
  • echo $BASH_VERSION - This command displays bash version
  • bash - if you want to use bash (type exit to go back to your previously opened shell)
  • whereis bash - locates the binary, source and manual-page for a command
  • which bash - locates which program is executed as 'bash' (default: /bin/bash. This can change across environments)
  • clear - This command clears content on the window (hide displayed lines)
  • reset - This command restores a console to a normal state

File Commands

These shell commands allow us to manipulate files.

ls: If you run the ls command (without any additional parameters), the program will simply list the contents of the current directory in a short form.

ls [option(s)] [file(s)]
  • -l a detailed list
  • -a displays all hidden files
  •  ls -lah is aliased as ll on some systems

The cp command copies a sourcefile to a targetfile.

cp [option(s)] sourcefile targetfile
  • -i : The -i flag prompts for confirmation before an existing file is overwritten
  • -r : The -r flag copies subdirectories recursively 

The mv command copies from a sourcefile to a target file, then removes the source file.

mv [option(s)] sourcefile targetfile
  • -b : The -b flag creates a backup copy of the source file prior to moving it
  • -i : The -i flag waits for confirmation before the existing target file is overwritten

The rm command removes the files from the file system. Directories themselves are not removed by rm unless the -r option is used.

rm [option(s)] file(s)
  • -r Removes any existing subdirectories
  • -i Waits for confirmation before deleting each file
  • -rf deletes with confirmation
    (use with EXTREME CAUTION as this command removes the file or directory forever.)

As you can below, we created several directories and then using rm via the xargs command, we can remove those same directories.

root@host:~# echo '1 2 3' | xargs mkdir
root@host:~# ll
total 4
drwx------ 1 root root 4096 Jul 17 18:10 ./
drwxr-xr-x 1 root root 4096 Dec  9  2019 ../
-rw-r--r-- 1 root root 3106 Apr  9  2018 .bashrc
drwxr-xr-x 1 root root 4096 Jul 17 17:52 .cache/
drwx------ 1 root root 4096 Jul 17 17:52 .config/
-rw-r--r-- 1 root root  148 Aug 17  2015 .profile
drwxr-xr-x 1 root root 4096 Jul 17 18:10 1/
drwxr-xr-x 1 root root 4096 Jul 17 18:10 2/
drwxr-xr-x 1 root root 4096 Jul 17 18:10 3/

root@host:~# echo '1 2 3' |xargs rm -rf
root@host:~# ll
total 4
drwx------ 1 root root 4096 Jul 17 18:10 ./
drwxr-xr-x 1 root root 4096 Dec  9  2019 ../
-rw-r--r-- 1 root root 3106 Apr  9  2018 .bashrc
drwxr-xr-x 1 root root 4096 Jul 17 17:52 .cache/
drwx------ 1 root root 4096 Jul 17 17:52 .config/
-rw-r--r-- 1 root root  148 Aug 17  2015 .profile

The ln command creates a link from the source file to the target file, using a different name. This link will point directly to the source file on the same file system. However, if ln uses the -s option, it creates a symbolic link that points to the directory location where the source file is located, which enables linking across file systems.

ln [option(s)] sourcefile targetfile
  • The -s flag creates a symbolic link.

Directory Commands

Using the cd command alone without any parameters changes directories to the user's home directory.

cd [options(s)] [directory]

The mkdir command creates a new directory. The same command can also create multiple directories.

mkdir [option(s)] directoryname
mkdir {test1,test2,test3}

The rmdir command deletes a specific directory, assuming it is empty.

rmdir [option(s)] directoryname

The chown command transfers ownership of a file to the user, with the specified username.

chown [option(s)] file(s)

Using the -R flag changes file and directory ownership in all subdirectories.

The chgrp command modifies or assigns the group ownership of a file or folder.

chgrp [option(s)] groupname file(s)

Ownership and Permissions

The chmod command changes the permissions access.

chmod [options] mode file(s) 

This parameter has three parts: group, access, and access type. The group option accepts the following characters:

  • u = user
  • g = group
  • w = world

Access is allowed by using the '+' symbol and denied by the '-' symbol.

The following options control the access type itself:

  • r = read
  • w = write
  • x = execute 
  • s = Set uid bit - the program or application is loaded as if the owner of the file started it.

Compress or Locate Commands

The gzip program compresses the contents of a file using mathematical algorithms. Any files compressed in this manner are assigned the .gz extension. Files will then need to be decompressed before they can be used. To compress multiple files or entire directories, the tar command must be used.

gzip [parameters] file(s)
  • -d The -d flag decompresses the packaged files, so they return to their original size. They can then be processed normally (using a command like gunzip).

The tar command adds files into an archive. Using compression is optional. The tar command is quite complex, with numerous options available. Some of the most frequently used options are:

tar options archive file(s)
  • -f The -f flag writes the output to a file and not to the screen.
  • -c The -c flag creates a new tar archive.
  • -r The -c flag adds a file(s) to an existing archive.
  • -t The -t flag outputs the contents of an archive.
  • -u The -u flag adds files only if they are newer than the files already contained in the archive.
  • -x The -x flag extracts the files from an archive.
  • -z The -z flag packs the resulting archive with gzip.
  • -j The -j flag compresses the resulting archive with bzip2.
  • -v The -v flag lists files processed.
tar zxvf file.tar.gz

Examples: The command above will extract the tarball into the current directory. The z flag is for gzip, x is for extract, v is for verbose, f is for file.

The command below will compress the 'directory' folder into a new tarball. The c flag is for compress. The p flag is for preserve permissions.

tar czpvf file.tar.gz directory/

An archived file that is created by tar will end with the .tar extension. If a tar archive was also compressed using gzip, the ending would be .tgz, or .tar.gz. If it was compressed using bzip2, the output would be .tar.bz2.

The locate command will find and specify where a file is located, including the directory where it is located. We can also use wildcards to indicate file names. One major drawback of locate is that it will be unable to find any files created after the latest update of its database.

locate pattern(s)

The locate database is updated by running the command 'updatedb' as the root user.

The updatedb command runs an update of the database used by locate. It will include files in all existing directories when run by the root user. 'updatedb' can be run in the background by appending an ampersand (&) to the command, to work can continue within the same command line Example:

updatedb [options(s)]
updatedb &

The find command searches for a file within a stated directory. The first arguments identify the options needed and then, the path where to start the search is noted. This is followed by the expressions. The find command scans the actual directory, unlike locate, which uses a database.

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

find [option(s)] 

Commands to Access File Contents

The cat command prints the contents of a file to the screen without interruption.

cat [option(s)] file(s)

-n The -n flag numbers the output on the left margin. We can also list the file contents in reverse by using the tac command. Example:

tac filename

The less command browses the contents of a file. We can scroll using the page up or down with PgUp and PgDn command half a screen, or we can scroll down a full screen using the spacebar. To move to the beginning or end of a file, use the Home and End keys. To exit the program, press Q.

less [option(s)] file(s)

The grep command locates a search string in a specified file(s). If the string is found, the command will display the line in which the search string was found, including the file name.

grep [option(s)] searchstring filenames
  • -i The -i flag ignores the case of the string.
  • -l The -l flag displays the name of the file, but not the lines of text.
  • -n The -n flag shows the line number where it found a match.
  • -l The -l flag lists the files where the search string did not occur.

The diff command compares the contents of any two files. The output produced by diff lists all lines that do not match. 

diff [option(s)] file1 file2

The vim command to open a file for viewing and editing using the vi or vim text editor. The other standard editor is nano. Below are some universal shortcuts for the vi commands:

vi(m) filename
  • :q - This will make you quit out of vi without saving.
  • :q! - This will let us quit vi without saving any changes made to the file.
  • :wq - This will make you quit vi after writing or saving the changes you made.
  • a - Begin inserting text.
  • dd - This will delete the line you are currently on.
  • G # - Goto line number, This can be used to jump to a specific line number.
  • /text - Search the file for the text specified after the /.
  • n - Goto next instance of text specified by the search.
  • esc - Escapes you from edit or insert mode.
  • u - Undo last change made.

The zcat command prints the content of gzipped file to the current screen.

zcat filename.gz

Sed - The sed command (or stream editor) searches, enables find and replace, or insertion or deletion of data within a file. 


The sed command below delete both leading and trailing whitespaces from each line in a file.

sed 's/^[ \t]//;s/[ \t]$//'

The awk command uses a scripting language for manipulating data and creating reports. The awk programming language does not require compiling, and allows a user to employ variables, logical operators, string functions, and numeric functions. Awk lets programmers write small programs using statements using patterns of text that can be within the lines of a document, and then employ an action based on when a match is found. In essence, awk searches files to see if they contain text which matches specific parameters and then executes an action.

awk options 'selection _criteria {action }' input-file > output-file

user@host:~$ awk '{print}' file.txt

Commands to Modify the File System

The mount command is used to mount a hard drive, CD-ROM, or other drive media to a Linux file system directory.

mount [option(s)] [<device>] mountpoint
  • -r This flag mounts the drive as read-only
  • -t This flag specifies the filesystem.

The umount command unmounts a mounted drive from the file system.

umount [option(s)] mountpoint
To prevent data loss, only run this command before taking a backup of the data from the drive.

Commands to Gather System Information

The df (or disk free) command displays information about the total disk space.

df [option(s)] [directory]
  • -H shows the number of occupied blocks in gb, mb, or kb in a human-readable format
  • -t Defines the type of file system (ext2, nfs, etc.)

The du command shows the total amount of disk space occupied by files and subdirectories within the current directory.

du [option(s)] [path] 
  • -a This flag displays each individual file size
  • -h Shows human-readable output
  • -s Displays only the calculated total size

The free command displays information about swap space and RAM usage, showing both the used and total amount from both.

free [option(s)]
  • -b Displays output in bytes
  • -k Displays output in kilobytes
  • -m Displays Output in megabytes

The date program shows the systems current date and time. 

date [option(s)]
user@host:~$ date
Thu Jul 23 15:31:43 EDT 2020

Commands to Identify or Modify Running Processes

The top command shows an overview of currently running processes. 

top [options(s)]

The ps command displays open programs or processes in the terminal.

ps [option(s)] [process ID]
  • faux - the faux flags show a list of all processes, the owner.

Executing the kill command, stops the specified process ID. It sends a TERM signal or a KILL signal. 

kill [option(s)] process ID

The kill -l command lists all the available kill signals as noted below.


The killall command uses the process name (opposed to the process ID), causing all processes named to be killed.

killall [option(s)] processname

Networking Commands

The ping command is the basic tool for testing the functionality of TCP/IP networks.

ping [option(s)] host name|IP address
  • -c The -c flag determines the number of packages to send and then ends once sent. 
  • -f flood ping - This flag sends continuous data packets
  • -i This value defines the sending interval in seconds between two data packages

The nslookup command uses DNS to resolve a domain name to an IP addresses. The command below uses nslookup to display the nameserver records for

nslookup [-option] [name | -] [server]
nslookup -type=ns

The telnet command uses an internet protocol which enables us to work on remote hosts across a network. 

telnet [option(s)] host name/IP address

Miscellaneous Commands

This command will alter a file so that it is unchangeable in any way until you un-chattr it.

chattr [ -RVf ] [ -v version ] [ mode ] files
chattr +i

The lsattr command will list that files attributes on stdout on a Linux extended file system

lsattr [ -RVadv ] [ files]
lsattr -v

The passwd command allows users or root to change a password

passwd [option(s)] [username]

The su command logs in to different user accounts in a running session. When no username is specified, we are prompted for the root user's password. The root user does not need a password to su into a user's account.

The halt command shuts down the system.

halt [option(s)]

The reboot command is similar to the halt command, but the system immediately reboots.

reboot [option(s)]

The clear command clears the visible area of the terminal CLI screen. 


The man command bring up a user manual for a command. 

man [option(s)] keyword(s)

Command Quick Reference

  • cat [filename] - Display file’s contents to the standard output device. 
  • cd /directorypath - Changes directory.
  • chmod [options] mode filename - Changes a file’s permissions.
  • chown [options] filename - Changes the ownership of a file.
  • clear - The Clear command clears the screen.
  • cp [options] source destination - Copies directories and files.
  • date [options] - Display or set the system date and time.
  • df [options] - Display used and available disk space.
  • du [options] - Show how much space each file takes up.
  • file [options] filename - Determines what type of data is within a file.
  • find [pathname] [expression] - Searches for files matching a specific pattern.
  • grep [options] pattern [filename] - Searches files or output for a specific pattern.
  • kill [options] pid - Stops a process. If the process is hung or refuses to stop, we can use the 'kill -9 pid' command.
  • less [options] [filename] - Views the contents of a file, one page at a time.
  • ln [options] source [destination] - Create shortcut.
  • locate filename - Search file system for specified filename.
  • lpr [options] - Send a print job.
  • ls [options] - List directory contents.
  • man [command] - Display help information for command.
  • mkdir [options] directory - Create a new directory.
  • mv [options] source destination - Rename or move directory or file(s).
  • passwd [name [password]] - Change password.
  • ps [options] - Show snapshot of running processes.
  • pwd - Shows current directory path.
  • rm [options] directory - Remove (delete) file(s) and/or directories.
  • rmdir [options] directory - Delete empty directories.
  • ssh [options] user@server - Log in to another Linux machine remotely. 
  • su [options] [user [arguments]] - Switch to another user account.
  • tail [options] [filename] - Display the last n lines of a file.
  • tar [options] filename - Store or extract files from a .tar.gz or .tgz).
  • top - Show systems resource usage.
  • touch filename - Create an empty file.
  • who [options] - Display who is logged on.


The bash scripting language used within the command line interface has been an integral part of Linux since its inception and continues to represent an indispensable tool for server maintenance and upkeep. The original thought when creating these small programs was, to accomplish one task and do it well. Over the years, many of these commands have continued to be improved on and updated to reflect the changing needs of the users. Users have come to heavily rely on the abilities that the bash scripting language provides to automate multiple tasks that would encompass a significantly larger amount of time. Overall, the ability to interact at the command line level, both as a user and root, increases our abilities and functionality when interacting with the system.

About the Author: David Singer

I am a g33k, Linux blogger, developer, student, and former Tech Writer for My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....

Have Some Questions?

Our Sales and Support teams are available 24 hours by phone or e-mail to assist.


Latest Articles

How Does Containerization Work?

Read Article

Microsoft Exchange Server Security Update

Read Article

How to Monitor Your Server in WHM

Read Article

How to Monitor Your Server in WHM

Read Article

How to Fix Typical WordPress Errors

Read Article