You are on page 1of 7

EXPERIMENT: 1

Aim: Use of Basic Unix Shell Commands: ls, mkdir, rmdir, cd, cat, banner, touch, file, wc, sort,
cut, grep, dd,dfspace, du, ulimit.

1. ls Command: The ls command is used to display a list of content of a directory.


Syntax:
ls

2. mkdir Command: The mkdir command is used to create a new directory under any directory.
Syntax:
mkdir <directory name>

3. rmdir Command: The rmdir command is used to delete a directory.


Syntax:
rmdir <directory name>

4. cd Command: The cd command is used to change the current directory.


Syntax:
cd <directory name>

5. cat Command: The cat command is a multi-purpose utility in the Linux system. It can be
used to create a file, display content of the file, copy the content of one file to another file, and
more.
Syntax:
cat [OPTION]... [FILE].. //To create a file, execute it as follows:
cat > <file name> // Enter file content
Press "CTRL+ D" keys to save the file. To display the content of the file, execute it as
follows:
cat <file name>

6. banner command: banner command in linux is used to print the ASCII character string in
large letter to standard output.
Syntax:
banner text

7. touch Command: The touch command is used to create empty files. We can create multiple
empty files by executing it once.
Syntax:
touch <file name>
touch <file1> <file2> ....

8. file command: file command is used to determine the file type. It does not care about the
extension used for file. It simply uses file command and tell us the file type. It has several
options.
Syntax:
file <filename>

9. wc Command: The wc command is used to count the lines, words, and characters in a file.
Syntax:
wc <file name>

10. sort Command: The sort command is used to sort files in alphabetical order.
Syntax:
sort <file name>

11. cut Command: The cut command is used to select a specific column of a file. The '-d' option
is used as a delimiter, and it can be a space (' '), a slash (/), a hyphen (-), or anything else. And,
the '-f' option is used to specify a column number.
Syntax:
cut -d(delimiter) -f(columnNumber) <fileName>

12. grep Command: The grep is the most powerful and used filter in a Linux system. The 'grep'
stands for "global regular expression print." It is useful for searching the content from a file.
Generally, it is used with the pipe.
Syntax:
command | grep <searchWord>

13. dd command: dd is a command-line utility for Unix and Unix-like operating systems
whose primary purpose is to convert and copy files.
 On Unix, device drivers for hardware (such as hard disk drives) and special device files
(such as /dev/zero and /dev/random) appear in the file system just like normal files.
 dd can also read and/or write from/to these files, provided that function is implemented
in their respective drivers
 As a result, dd can be used for tasks such as backing up the boot sector of a hard drive,
and obtaining a fixed amount of random data.
 The dd program can also perform conversions on the data as it is copied, including byte
order swapping and conversion to and from the ASCII and EBCDIC text encodings.
Some practical examples on dd command
(i). To back up the entire hard disk: To backup an entire copy of a hard disk to another hard
disk connected to the same system; execute the dd command as shown. In this dd command
example, the UNIX device name of the source hard disk is /dev/hda, and device name of the
target hard disk is /dev/hdb.
Syntax:
# dd if=/dev/sda of=/dev/sdb

(ii). To backup a Partition: You can use the device name of a partition in the input file, and in
the output either you can specify your target path or image file as shown in the dd command.
Syntax:
# dd if=/dev/hda1 of=~/partition.img
(iii). To create an image of a Hard Disk: Instead of taking a backup of the hard disk, you can
create an image file of the hard disk and save it in other storage devices. This method is
typically faster than other types of backups, enabling you to quickly restore data following an
unexpected catastrophe. It creates the image of a hard disk /dev/hda.
Syntax:
# dd if=/dev/hda of=~/hdadisk.img

(iv). To restore using the Hard Disk Image: To restore a hard disk with the image file of an
another hard disk, the following dd command can be used
Syntax:
# dd if=hdadisk.img of=/dev/hdb

(v). To create CDROM Backup: dd command allows you to create an iso file from a source
file. So we can insert the CD and enter dd command to create an iso file of CD content.
Syntax:
# dd if=/dev/cdrom of=tgsservice.iso bs=2048

14. df Command: Linux df command is used to display the disk space used in the file system.
The 'df' stands for "disk filesystem." It defines the number of blocks used, the number of blocks
available, and the directory where the file system is mounted.
Syntax:
df [OPTION]... [FILE]...

15. Ulimit command: With this command we can enforce control at the global, group or user
level furthermore we can also prevent unwanted processes and users from consuming a lot of
system resources.
Syntax:
Ulimit options
16. du command: Command du stands for Disk Usage. It is used to check the information of
disk usage of files and directories on a system. Command du displays a list of all the files along
with their respective sizes. By default, size given is in kilobytes. File names are used as
arguments to get the file size.
Syntax:
du

17. ulimit command: ulimit is admin access required Linux shell command which is used to
see, set, or limit the resource usage of the current user. It is used to return the number of open
file descriptors for each process. It is also used to set restrictions on the resources used by a
process.
Syntax:
ulimit -a
EXPERIMENT: 2

Aim: Commands related to inode, I/O redirection and piping, process control commands, mails.

COMMANDS RELATED TO INODE


An Inode number is a uniquely existing number for all the files in Linux and all UNIX type
systems. When a file is created on a system, a file name and Inode number is assigned to it.
Generally, to access a file, a user uses the file name but internally file name is first mapped with
respective Inode number stored in a table.

Find Inode number of File: To check inode number of file use following command. The first
field in output is an inode number of the file.
Syntax:
ls -il filename

Inode Changes with Copy, Move and Delete


(i) Copy file: cp allocates a free inode number and placing a new entry in inode table.
Syntax:
cp v.txt v_.txt

(ii) Move or Rename a file: if destination is same file system as the source, Has no impact on
inode number, it only changes the time stamps in inode table.
Syntax:
mv filename path_where_you_want_to_copy

(iii) Delete a file: Deleting a file in Linux decrements the link count and freeing the inode
number to be reused.

Commands to access Inode numbers


(i) Ls -i Command: the flag -i is used to print the Inode number for each file.
Syntax:
ls -i
(ii) Df -i Command: df -i command displays the inode information of the file system.
Syntax:
df -i
(iii) Stat Command: Stat command is used to display file statistics that also displays inode
number of a file.
Syntax:
stat a.txt
COMMANDS RELATED TO I/O REDIRECTION AND PIPING
Commands typically get their input from a source referred to as standard input (stdin) and
typically display their output to a destination referred to as standard output (stdout) as pictured
below:

As depicted in the diagram above, input flows (by default) as a stream of bytes from standard
input along a channel, is then manipulated (or generated) by the command, and command output
is then directed to the standard output.

UNIX provides the capability to change where standard input comes from, or where output goes
using a concept called Input/output (I/O) redirection. I/O redirection is accomplished using a
redirection operator which allows the user to specify the input or output data be redirected to (or
from) a file.

Redirection Operator Resulting Operation


command > file stdout written to file, overwriting if file exists
command >> file stdout written to file, appending if file exists
command < file input read from file
command 2> file stderr written to file, overwriting if file exists
command 2>> file stderr written to file, appending if file exists
command > file 2>&1 stdout written to file, stderr written to same file descriptor

PROCESS CONTROL COMMANDS


What is a Process? An instance of a program is called a Process. In simple terms, any command
that you give to your Linux machine starts a new process. Having multiple processes for the
same program is possible.
Types of Processes:
 Foreground Processes: They run on the screen and need input from the user. For example
Office Programs
 Background Processes: They run in the background and usually do not need user input.
For example Antivirus.
Fg Command: You can use the command “fg” to continue a program which was stopped and
bring it to the foreground. The simple syntax for this utility is:
Syntax:
fg jobname
Top Command: This utility tells the user about all the running processes on the Linux machine.
Syntax:
top

PS Command: This command stands for „Process Status‟. This command is similar to „top‟
command but the information displayed is different. To check all the processes running under a
user, use the command.
Syntax:
ps ux
You can also check the process status of a single process, use the syntax –
Syntax:
ps PID

Kill Command: This command terminates running processes on a Linux machine. To use
these utilities you need to know the PID (process id) of the process you want to kill.
Syntax:
kill PID

NICE Command: Linux can run a lot of processes at a time, which can slow down the speed of
some high priority processes and result in poor performance. To avoid this, you can tell your
machine to prioritize processes as per your requirements. This priority is called Niceness in
Linux, and it has a value between -20 to 19. The lower the Niceness index, the higher would be a
priority given to that task. The default value of all the processes is 0.
Syntax:
nice -n 'Nice value' process name

DF Command: This utility reports the free disk space(Hard Disk) on all the file systems. If you
want the above information in a readable format, then use the command
Syntax:
'df -h'

Free Command: This command shows the free and used memory (RAM) on the Linux system.
You can use the arguments
Syntax:
free -m to display output in MB
free -g to display output in GB

MAIL COMMAND
Linux mail command is a command-line utility that allows us to send emails from the command
line. The mail command can be used directly by the terminal as well as the Shell script. To install
the mailutils package, execute the below command: sudo apt install mailutils

Send mail using the mail command: Sending mails from the command line is a straight
forward process. To send a mail from the terminal, execute the command as follows:
Syntax:
mail -s "Subject" <recipient address>
Specify the mail body in a single line: We can specify the subject and message in a single line.
To specify the message body in a single line, execute the below command:
Syntax:
mail -s "subject" <recipient_address> <<< 'Message'

Fetch message from a file: We can also fetch messages from a file. To send a message from a
file, execute the command as follows
Syntax:
mail -s "Hello World" user@yourmaildomain.com < /home/mail/mailcontent.txt

Specify CC and BCC: We can also attach a bcc and cc address within a command. To attach a
bcc and cc address, use the -b and -c options, respectively.
To add a bcc address, execute the command as follows:
Syntax:
mail -s "Hello World" <recipient address> -b userto< bcc address>
To add a cc address, execute the command as follows:
Syntax:
mail -s "Hello World" <recipient address> -c userto< cc address>

Adding multiple recipients: The mail command allows us to add more than one recipient in a
single mail other than cc and bcc.
Syntax:
mail -s "Hello World" <recipient address1>,<recipient address2>

Specify the sender name and address: We can add the additional header information such as
'FROM' name and address to attach with the email.
Syntax:
$ echo "Message body" | mail -s "Subject" a
From:Sender_name\<Sender mail address\> recipient address
Adding an attachment: We can also attach a file with the mail. The -a option is used to attach a
file along with the mail. To add an attachment, execute the command as follows:
Syntax:
echo "Message body" | mail -s "Subject" -r "<recipient address>"
a /path/to/file <sender address>

Read mails: The mail command can be used to read all your incoming mails. To read the inbox
mails, execute the mail command without any option as follows:
Syntax:
mail

Manual for the mail command: To read the manual for the mail command, execute the man
command as follows:
Syntax:
man mail

You might also like