Professional Documents
Culture Documents
Working on a terminal
(Dated: 04 August 2022)
In its most conventional sense, a computer terminal is nothing but a device to provide
instructions, input and output data to and from a computer [1] or some computing sys-
tem. In most cases, they were some devices with a type-writer like keyboard and a visual
display unit, as shown in the picture below.
FIG. 1. This is the picture of the video display unit terminal DEC VT100. Courtesy: Wikipedia
What we call a terminal today, is actually a terminal emulator i.e., an application that
emulates the functions of a conventional terminal and allows you to perform a host of
tasks on a computer or connect to a different machine. A terminal can be a graphical
user interface (GUI) like your desktop and file manager on your computer or it can be
text based. Most commonly, people refer to command line interfaces (CLI) as termi-
nals. These terminals run applications such as Windows command prompt or cmd.exe
or Powershell, or Unix shells or command line interpreters such as sh (Boune shell), bash
(Bourne again shell) or zsh (Z shell) and take instructions in the forms of command typed
into the interface.
So, what is shell and bash? Shells are command line interpreters that allows you to
directly interact with a operating systems to perform various computing tasks. Unix
shells such as bash provides an interactive command language that allows Unix based
operating systems such as Linux and MacOS to execute various commands. Similarly,
1
FIG. 2. A snapshot of the Unix shell bash being run on iTerm2 terminal on a Macbook Pro.
a Windows shell allows you to interact with its operating system. Shell commands and
scripts are typically run using a terminal emulator. Bash or Bourne again shell is one of
the most commonly used Unix shells, while others such as zsh or Z shell are becoming
more popular. In windows, Powershell is a powerful command line interpreter and can
now also be run on Linux and MacOS. Several modern command line interpreters such
as Powershell, bash and zsh allow a fair amount of cross-compatibility in commands for
ease of use.
Why do we need to use bash? Using command line interpreters give you unprece-
dented control in executing and running command using significantly lower resources.
The last part is obvious as GUI requires more space to load and operate, but the first part
is clear in the example below. Suppose, you have a large folder with data received from
experiments in CERN (say there a million *.dat files). All these are labeled with name
of experiment, location and dates. If you were to copy files of the experiments done on
12.06.2022, you would need to look for all files with name xxxxxxxxx12062022.dat. Using
a command line one can make a new directory and simply type the text:
mkdir Dropbox/ n e w p r o j e c t s /
cp Work/CERN projects/ A t l a s / * 1 2 0 6 2 0 2 2 . dat Dropbox/ n e w p r o j e c t s /
As such, command line interpreters such as bash are very useful when multiple com-
mands or search queries are made, with multiple and a wide range of options that can
be entered more easily as a text or script rather than handling click and move options
2
in conventional GUI. Importantly, some programming tasks may not necessarily have a
GUI to work with. Other usefulness include the ability to quickly perform remote logins
to another computer such as a high-performance computer or cluster (commonly called
a supercomputer), using cryptographic protocols such as Secure Shell Protocol (SSH).
Such options make it very popular with scientists, engineers, programmers and system
administrators [2].
See Fig. 3 to see the different folders under the tree under parent directory ’/home’.
3
FIG. 3. The directory tree with different folders under the parent directory ’/home’. A specific
path to a folder ’/home/username/Work/CERN projects/ATLAS’ is highlighted in red.
Options for ls
username@comp:∼$ ls -a (list all files including hidden)
username@comp:∼$ ls -l (list all files with size and permission)
username@comp:∼$ ls -lh (list all files with size in human readable form)
username@comp:∼$ ls *.txt (list all files with extension ‘txt’)
username@comp:∼$ ls foldername | wc -l (list the number of files in ’foldername’)
username@comp:∼$ ls * (list all subdirectories and files at the next level)
4
This is a programming course
username@comp:∼$ echo * (print the name of all files and folders)
• cat – Print the contents of a file. Also used for creating, viewing and concatenating
different files.
username@comp:∼$ cat file1 (outputs the content of file1)
Physics Department
5
IIT Bombay
username@comp:∼$ cat file1 file2 (outputs the content of both the files)
Physics Department
IIT Bombay
Powai
Mumbai
username@comp:∼$ cat -n file1 (outputs the content with linenumber)
1 Physics Department
2 IIT Bombay
username@comp:∼$ cat > newfile (creates a new file called ’newfile’)
Name of institute (User input; Ctrl+d to save file)
username@comp:∼$ cat newfile
Name of institute
username@comp:∼$ cat file1 >> newfile (appends content of ’file1’ to ’newfile’)
username@comp:∼$ cat newfile
Name of institute
Physics Department
IIT Bombay
username@comp:∼$ cat file1 file 2 file 3 >> mergedfile (appends contents of all files
to a new file called ’mergedfile’)
6
• grep – global search for regular expression and print out. Searches a file for patterns
or characters, and display the line containing it.
username@comp:∼$ grep -i ”fantasy” rhapsody.txt (search for ’fantasy’ in the file)
Is this just fantasy?
username@comp:∼$ grep -c ”this” rhapsody.txt (number of lines containing ’this’)
2
username@comp:∼$ grep -l ”fantasy” * (files that containing ’fantasy’)
rhapsody.txt
username@comp:∼$ grep -i ”Is” rhapsody.txt (search for string ’is’ in the file)
Is this the real life?
Is this just fantasy?
username@comp:∼$ grep -w ”Is” rhapsody.txt (search for whole word ’Is’ in the
file)
Is this the real life?
Is this just fantasy?
username@comp:∼$ grep ”y?$” rhapsody.txt (search for lines ending with ’y?’)
Is this just fantasy?
username@comp:∼$ grep -e ”fantasy” -e ”reality” rhapsody.txt (multiple searches)
Is this just fantasy?
No escape from reality.
7
username@comp:∼$ cp ∼/Work/CERN projects/ATLAS/*.dat ∼/Newfolder/Data/
(copy all *.dat files from ATLAS to Data)
username@comp:∼$ cp -r ∼/Work/CERN projects/ATLAS/ ∼/Newfolder/Data/
(copy the entire folder ATLAS to Data)
username@comp:∼$ cp -r ∼/Work/CERN projects/ATLAS/ ∼/Newfolder/Newdata
(creates a directory Newdata and copies the entire folder ATLAS)
8
Hello Darkness!
Options and pathname operations for mv are similar to cp. Please note that mv
deletes the original file (renames), so caution is of utmost important while using
the command.
• other commands – whoami: returns the active username; clear: clears the terminal
screen; exit: exits the terminal screen or window by exiting all shell commands
and closing remote connections; Ctrl + C: terminates any ongoing process; chmod:
change permission to read, write or access directories, or execute files [4]. Also, try
these commands awk [4], yes, factor, rev etc. Moreover, understand the use of ‘ * ’
in directory names and filename.
III. PATHNAME
Let us look at the example below with regards to the directory tree in Fig. 3:
username@comp:∼$ pwd
/home/username/Work (we are at the directory ‘Work’)
username@comp:∼$ cd CERN projects/ATLAS
(We state the relative path to change directory to ‘ATLAS’ from the folder under working
9
directory ‘Work’)
username@comp:∼$ pwd
/home/username/Work (we are at the directory ‘Work’)
username@comp:∼$ cd /home/username/Work/CERN projects/ATLAS
username@comp:∼$ pwd
/home/username/Work/CERN projects/ATLAS
(But now we provide the absolute path to the folder ‘ATLAS’ from the ‘/home’ directory.)
Note we assume that ‘/home’ is the root directory here, which may not always be the
case. The pwd command will show you the absolute path starting from the root directory.
10