Linux Lab Manual
Linux Lab Manual
1. Is command
The ls command is commonly used to identify the files and directories in the
working directory. This command is one of the many often-used Linux commands
that you should know.
This command can be used by itself without any arguments and it will provide us the
output with all the details about the files and the directories in the current working
directory. There is a lot of flexibility offered by this command in terms of displaying
data in the output. Check the below image for the output.
known as a long format that displays detailed information about files and
-l directories.
-a Represent all files Include hidden files and directories in the listing.
Sort files and directories by their last modification time, displaying the most
-t recently modified ones first.
-r known as reverse order which is used to reverse the default order of listing.
-S Sort files and directories by their sizes, listing the largest ones first.
known as inode which displays the index number (inode) of each file and
-i directory.
known as group which displays the group ownership of files and directories
-g instead of the owner.
The pwd command is mostly used to print the current working directory on your
terminal. It is also one of the most commonly used commands.
Now, your terminal prompt should usually include the entire directory. If it doesn’t,
this is a quick command to see which directory you’re in. Another purpose for this
command is when creating scripts because it can help us find the directory in which
the script was saved. The below pictures are the output with the command.
Command:
Output:
3. mkdir command
This mkdir command allows you to create fresh directories in the terminal itself. The
default syntax is mkdir <directory name> and the new directory will be created.
For example, if you want to create a directory as “GeeksforGeeks” then the basic
syntax would be:
mkdir GeeksforGeeks
In case you want to create another directory inside the main directory
GeeksforGeeks to store projects, you can use the following command to do
so. mkdir GeeksforGeeks/projects
Command:
localhost:~# mkdir MIT
localhost:~# ls
MIT bench.py hello.c hello.js readme.txt
Directory is in blue.
localhost:~# ls // crosscheck it by ls
bench.py hello.c hello.js readme.txt
localhost:~#
Here we used the ls command to check the directories present there and used rmdir
<directory name> to delete the directory and again the ls command to view the
directories after deleting the same.
6.cat command:
6. cp command
7. mv command
Here we used the ls command to check the directories and then used mv <file
name> <Renamed file name> to rename the files, and then again we used
the ls command to view the renamed file as you can see in the output screenshot.
9. uname command
The uname command is used to check the complete OS information of the system.
Check out the command and the output below
Command:
localhost:~# uname
Linux
The touch command creates an empty file when put in the terminal in this format as
touch <file name>
Command:
localhost:~# cd MIT
localhost:~/MIT# touch aids5
localhost:~/MIT# ls
aids5
We used the ls command to check the current directories in the terminal and then
used the touch command to create an empty file and then again we used ls to find
out the created file in the terminal.
12. ln command
The ln command is used to create a shortcut link to another file. This is among the
most important Linux commands to know if you want to operate as a Linux
administrator.
Command:
Output:
Here we used mkdir to create two directories and then we used ln with an -s to
create a soft link in it.
Output:
15. ps command
Output:
17. grep command
The grep command is used to find a specific string in a series of outputs. For
example, if you want to find a string in a file, you can use the syntax: <Any
command with output> | grep “<string to find> “
For Example:
cat Files.txt | grep “new”
Command:
Output:
In this command, we first used cat <file name> to view the content of the file, and
then we used cat <file name> | grep “string” to check the string in it.
Output:
The wget command in the Linux command line allows you to download files from
the internet. It runs in the background and does not interfere with other processes.
Here is the basic syntax: wget [option] [url]
Command:
wget http://sample.com/sample-menu.php
Output:
The whoami command provides basic information that is extremely useful when
working on multiple systems. In general, if you are working with a single computer,
you will not require it as frequently as a network administrator.
Command:
Output:
The sort command is used generally to sort the output of the file. Let’s use the
command and see the output.
Command: (We are using the cat command to see the file content)
Output:
Here first we checked the file content using the cat command and then we sorted it
alphabetically using the sort command.
The cal command is not the most famous command in the terminal but it functions
to view the calendar for a particular month in the terminal. Let’s see how this works.
Command:
Output:
Output:
24. df command
Output:
Here we have used df -h as simply typing df will return the output in bytes which is
not readable, so we add -h to make the outputs more readable and understandable.
25. wc command
wc command in Linux indicates the number of words, characters, lines, etc using a
set of options.
wc -w shows the number of words
wc -l shows the number of lines
wc -m shows the number of characters present in a file
Let’s see one example of these options
Command:
Output:
Here we used the touch command to create a text file and then used
the echo command to input a sentence that contains six words and we used the wc -
w command to calculate the number of words in it.
Experiment 2
The following arithmetic operators are supported by Bourne Shell.
Assume variable a holds 10 and variable b holds 20 then −
+ (Addition) Adds values on either side of the operator `expr $a + $b` will give
30
- (Subtraction) Subtracts right hand operand from left hand `expr $a - $b` will give -
operand 10
* (Multiplication) Multiplies values on either side of the `expr $a \* $b` will give
operator 200
/ (Division) Divides left hand operand by right hand `expr $b / $a` will give 2
operand
% (Modulus) Divides left hand operand by right hand `expr $b % $a` will give
operand and returns remainder 0
!= (Not Equality) Compares two numbers, if both are different [ $a != $b ] would return
then returns true. true.
It is very important to understand that all the conditional expressions should be inside square
braces with spaces around them, for example [ $a == $b ] is correct whereas, [$a==$b] is
incorrect.
All the arithmetical calculations are done using long integers.
Example
Here is an example which uses all the arithmetic operators −
https://www.tutorialspoint.com/execute_bash_online.php link for bash script
#!/bin/sh
a=2
b=3
sum=$(($a + $b))
echo "sum:" $sum
sum=$((1+ 2 ))
echo "sum:" $sum
Experiment 3
3. Create a file called wlcc.txt with some lines and display how many lines, words and characters are
present in that file.
2. -w: This option prints the number of words present in a file. With this option wc
command displays two-columnar output, 1st column shows number of words present
in a file and 2nd is the file name.
With one file name
$ wc -w state.txt
7 state.txt
localhost:~# wc -w wlcc.txt
10 wlcc.txt
localhost:~# wc -w wlcc.txt wlcd.txt
10 wlcc.txt
4 wlcd.txt
14 tota
Number of word in linux file
3. -c: This option displays count of bytes present in a file. With this option it
display two-columnar output, 1st column shows number of bytes present in a file
and 2nd is the file name.
With one file name
$ wc -c state.txt
58 state.txt
$ ls gfg | wc -l
7
Experiment 4
4. Append ten more simple lines to the wlcc.txt file created above and split the appended file into 3
parts. What will be the names of these split files? Display the contents of each of these files. How
many lines will be there on the last file?
here we have splited wlcc file in 3line in each part . means each part having 3 lines. and each part
having name like “ -- aa”, “—ab”, “—ac”.
As we can see wlcnac is last partitioned file and we can see its content by cat command .
Experiment 5
5. Given two files each of which contains names of students. Create a program to display only those
names that are found on both the files.
localhost:~# ls -li
total 80
68698 drwxr-xr-x 2 root root 60 Jul 24 11:40 MIT
67459 -rw-r--r-- 1 root root 114 Jul 5 2020 bench.py
67457 -rw-r--r-- 1 root root 76 Jul 3 2020 hello.c
67456 -rw-r--r-- 1 root root 22 Jun 26 2020 hello.js
68710 -rw-r--r-- 1 root root 14 Jul 24 13:08 newaa
68711 -rw-r--r-- 1 root root 4 Jul 24 13:08 newab
67458 -rw-r--r-- 1 root root 151 Jul 5 2020 readme.txt
68705 -rw-r--r-- 1 root root 21 Jul 24 13:01 split.txtaa
68706 -rw-r--r-- 1 root root 41 Jul 24 13:01 split.txtab
68707 -rw-r--r-- 1 root root 14 Jul 24 13:05 wlcaa
68708 -rw-r--r-- 1 root root 15 Jul 24 13:05 wlcab
68709 -rw-r--r-- 1 root root 33 Jul 24 13:05 wlcac
68700 -rw-r--r-- 1 root root 62 Jul 24 12:52 wlcc.txt
68701 -rw-r--r-- 1 root root 18 Jul 24 12:44 wlcd.txt
68712 -rw-r--r-- 1 root root 14 Jul 24 13:10 wlcnaa
68713 -rw-r--r-- 1 root root 15 Jul 24 13:10 wlcnab
68714 -rw-r--r-- 1 root root 33 Jul 24 13:10 wlcnac
68702 -rw-r--r-- 1 root root 62 Jul 24 13:03 xaa
68703 -rw-r--r-- 1 root root 15 Jul 24 12:53 xab
68704 -rw-r--r-- 1 root root 33 Jul 24 12:53 xac
localhost:~# ls -ld wlcnaa
-rw-r--r-- 1 root root 14 Jul 24 13:10 wlcnaa
localhost:~# ls -li wlcnaa
68712 -rw-r--r-- 1 root root 14 Jul 24 13:10 wlcnaa //get inode of any desired file
name
68712 -rw-r--r--
localhost:~#
7.Study & use of the Command for changing file permissions.
Linux is a multi-user operating system, so it has security to prevent people from
accessing each other’s confidential files.
When you execute a “ls” command, you are not given any information about the
security of the files, because by default “ls” only lists the names of files. You can get
more information by using an “option” with the “ls” command. All options start with
a ‘-‘. For example, to execute “ls” with the “long listing” option, you would type ls -
l
When you do so, each file will be listed on a separate line in a long format. There is
an example in the window below. Syntax:
ls
ls -l
localhost:~# ls -l
total 16
-rw-r--r-- 1 root root 114 Jul 5 2020 bench.py
-rw-r--r-- 1 root root 76 Jul 3 2020 hello.c
-rw-r--r-- 1 root root 22 Jun 26 2020 hello.js
-rw-r--rw- 1 root root 151 Jul 5 2020 readme.txt
localhost:~#
Letter
s Definition
‘x’ “execute” the file. This permission is given only if the file is a program.
The group permissions apply only to the group that has been
assigned to the file or directory, they will not affect the actions
`g` group of other users.
All
All three (owner, groups, others)
`a` three
this is a file
~
~
~
~
~
~
- myfile 1/1 100%
this is a file
~
~
~
~
~
~
I myfile 1/1 100% // here I is showing that it is insert mode//
this is a file
~
~
~
~
~
~
- myfile 1/1 100%// by doing Esc I is removed//
:this is a file
~
~
~
~:
11. Write a shell script that accepts any number of arguments and prints them in the reverseorder.
#!/bin/bash
fi
echo " total no. of rgument : $#"
echo " argument are :$*"
echo " number of argument in reverse"
rev=" "
for i in $*
do
rev=$i" "$rev
done
echo $rev
~
Output
12. Write a shell script to find the smallest of three numbers that are read from the keyboard.
#!/bin/bash
echo " enter number"
read a
read b
read c
if [ $a -eq $b -a $b -eq $c ]
then
echo " All number are equal"
exit
fi
if [ $a -lt $b ]
then
s1=$a
s2=$b
else
s1=$b
s2=$a
fi
if [ $s1 -gt $c ]
then
s2=$s1
s1=$c
fi
echo "smallest number is $s1"
~
~
Output
localhost:~/test# ./smallnum
enter number
3
4
1
smallest number is 1