You are on page 1of 10

PH434 Autumn 2022 – Class # 1

Working on a terminal
(Dated: 04 August 2022)

I. WHAT IS A TERMINAL, SHELL AND BASH?

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].

II. WORKING WITH BASH I – COMMANDS

• pwd – Print the working directory


username@comp:∼$ pwd
/home/username

See Fig. 3 to see the different folders under the tree under parent directory ’/home’.

• cd – Change directory from username to ATLAS via Work


username@comp:∼$ cd Work/CERN projects/ATLAS
username@comp:∼$ pwd
/home/username/Work/CERN projects/ATLAS
username@comp:∼$ cd .. (changes directory back one level)
username@comp:∼$ pwd
/home/username/Work/CERN projects
username@comp:∼$ cd (takes you directly to /username)
username@comp:∼$ pwd
/home/username

• mkdir – Make a new directory or folder


username@comp:∼$ mkdir Newfolder
username@comp:∼$ mkdir Work/CERN projetcs/Newfolder

• rmdir – Remove a directory or folder


username@comp:∼$ rmdir Work/CERN projetcs/Newfolder

• rm – Remove a file (xxxx.dat) or folder


username@comp:∼$ rm Work/CERN projetcs/Newfolder/xxxx.dat

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.

username@comp:∼$ rm -r Work/CERN projetcs/Newfolder

• ls – Used to list the contents of a directory or folder


username@comp:∼$ cd Work/CERN projects/ATLAS (change folder to /ATLAS)
username@comp:∼$ ls
list all the *.dat files in the folder /ATLAS
username@comp:∼$ cd (takes you directly back to /username)
username@comp:∼$ ls Work/CERN projects/ATLAS
list all the files in the folder /ATLAS but current directory is /username

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)

• echo – Print the text on the screen (blue is the output)


username@comp:∼$ echo ”This is a programming course”

4
This is a programming course
username@comp:∼$ echo * (print the name of all files and folders)

Option echo -e (allows interpretation of \ command)


username@comp:∼$ echo -e ”Physics \nIIT Bombay \nPowai”
Physics
IIT Bombay
Powai
username@comp:∼$ echo -e ”Physics \bIIT Bombay \bPowai”
PhysicsIIT BombayPowai
username@comp:∼$ echo -e ”Physics \cIIT Bombay \bPowai”
Physics
username@comp:∼$ echo -e ”Physics \tIIT Bombay \tPowai”
Physics IIT Bombay Powai

• touch – Create a new file or timemark an old file


username@comp:∼$ mkdir newfolder
username@comp:∼$ cd newfolder
username@comp:∼$ pwd
/home/username/newfolder
username@comp:∼$ touch filename (create a file called filename)
username@comp:∼$ ls
filename
username@comp:∼$ touch filename1 filename2 (create two more files)
username@comp:∼$ ls
filename filename1 filename2

Options for touch: See the following reference [3].

• 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’)

Using touch and cat in unision:


username@comp:∼$ touch rhapsody.txt (create a file called rhapsody.txt)
username@comp:∼$ cat >> rhapsody.txt (write to rhapsody.txt)
Is this the real life?
Is this just fantasy?
Caught in a landside,
No escape from reality. (User input; Ctrl+d to save file)
username@comp:∼$ cat rhapsody.txt
Is this the real life?
Is this just fantasy?
Caught in a landside,
No escape from reality.

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.

• cp – Copy files and folder from one location to another


username@comp:∼$ pwd (find the current directory)
/home/username/Newfolder
username@comp:∼$ ls
rhapsody.txt
username@comp:∼$ cp rhapsody.txt ∼/Desktop/ (copies the file ’rhapsody.txt’
from Newfolder to Desktop. The ∼ implies /home/username)
username@comp:∼$ cp -r ∼/Newfolder/ ∼/Desktop/ (copies the folder New-
folder to Desktop. Option -r implies recursive copy, which is needed for folders)
username@comp:∼$ mkdir Data (create a new folder)

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)

Other option cp:


username@comp:∼$ cp -i file1.txt Newdata/ (Asks permission before overwrit-
ing)
cp: overwrite ’Newdata/file1.txt’? y/n
username@comp:∼$ cp -i file1.txt Newdata/ (Creates a backup)
username@comp:∼$ ls Newdata/
file1.txt file1.txt∼ file2.txt
username@comp:∼$ cp -p file1.txt Newdata/ (preserves all time stamps and per-
mission)

• mv – Move or rename files and folder from one location to another.


username@comp:∼$ cd Newdata/
username@comp:∼$ ls
file1.txt file1.txt∼ file2.txt
username@comp:∼$ mv file2.txt filez.txt (rename file2 to filez)
username@comp:∼$ ls
file1.txt file1.txt∼ filez.txt
username@comp:∼$ cat file1.txt
Hello world!
username@comp:∼$ cat filez.txt
Hello Darkness!
username@comp:∼$ mv filez.txt file1.txt (content of file1 is replaced by filez and
filez is deleted)
username@comp:∼$ ls
file1.txt file1.txt∼
username@comp:∼$ cat file1.txt

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.

• locate – Command to find files by name. Also see find [4].


username@comp:∼$ locate rhapsody.txt (show all directories with the file)
/home/username/Newfolder/rhapsody.txt
/home/username/Desktop/rhapsody.txt
username@comp:∼$ locate rhapsody.txt -n 5 (show only 5 instances if many)
username@comp:∼$ locate rhapsody.txt -c (show number of instances)
2

• 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

A pathname can be relative or absolute. An absolute path to a particular location (file


or folder) specifies the path from the /home (or root) directory, whereas a relative path is
defined as the path from working directory (as seen in pwd).

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’)

Alternatively, once could have also used:

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.

Also, note that there is always a ‘/’ in front of ‘/home’)

[1] For more information, see Computer terminal.


[2] See this video on Youtube to learn more about bash.
[3] Use the link for different options for the touch command.
[4] Use the links to find more about the find, chmod, and awk in Linux.

10

You might also like