0% found this document useful (0 votes)
30 views25 pages

Linux Lab Manual

The document provides an overview of various Linux commands including 'ls', 'pwd', 'mkdir', 'cd', 'rmdir', 'cat', 'cp', 'mv', 'uname', 'touch', 'ln', 'clear', 'ps', 'grep', 'echo', 'wget', 'whoami', 'sort', 'cal', 'whereis', 'df', and 'wc', detailing their usage and options. It also covers arithmetic operators in Bourne Shell and demonstrates the use of the 'wc' command for counting lines, words, and characters in files. Additionally, it includes examples of file manipulation and command outputs.

Uploaded by

niti.patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views25 pages

Linux Lab Manual

The document provides an overview of various Linux commands including 'ls', 'pwd', 'mkdir', 'cd', 'rmdir', 'cat', 'cp', 'mv', 'uname', 'touch', 'ln', 'clear', 'ps', 'grep', 'echo', 'wget', 'whoami', 'sort', 'cal', 'whereis', 'df', and 'wc', detailing their usage and options. It also covers arithmetic operators in Bourne Shell and demonstrates the use of the 'wc' command for counting lines, words, and characters in files. Additionally, it includes examples of file manipulation and command outputs.

Uploaded by

niti.patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Experiment 1

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.

Commonly Used Options in `ls` command in Linux


Options Description

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.

-R List files and directories recursively, including subdirectories.

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.

-h Print file sizes in human-readable format (e.g., 1K, 234M, 2G).

-d List directories themselves, rather than their contents.


2. pwd command

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.

4. cd: Change Directory

localhost:~# cd MIT // right now it is in localhost


localhost:~/MIT# // now it is in MIT Directory

localhost:~/MIT# cd // by cd command can come out from directory


localhost:~# // here again it is local host
5. rmdir command

The rmdir command is used to delete permanently an empty directory. To perform


this command the user running this command must be having sudo privileges in the
parent directory.
Command:
localhost:~# rmdir MIT // remove MIT directory by rmdir command

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:

localhost:~# cat > aids // create cat aids file


this is MIT cillege. // content of file

save and exit the file by ctlr+D .

6. cp command

The cp command of Linux is equivalent to copy-paste and cut-paste in Windows.

localhost:~# cp aids ds // it create new file ds and paste


contented aids file in this.
localhost:~# ls
MIT bench.py hello.c readme.txt
aids ds //ds file created hello.js

localhost:~# cat ds // CHECK content of ds file


this is MIT cillege. // here it is same as aids file.

7. mv command

The mv command is generally used for renaming the files in Linux.


Command:
localhost:~# mv ds ids // here name of ds file change and new
name is ids.
localhost:~# ls // check by ls command
MIT bench.py hello.js readme.txt
aids hello.c ids // here ds has changed by ids.

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

11. touch command

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.

14. clear command

The clear command is a standard command to clear the terminal screen.


Command: *This was the terminal before the command.

Output:
15. ps command

ps command in Linux is used to check the active processes in the terminal.


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.

18. echo command

echo command in Linux is specially used to print something in the terminal


Command:

Output:

19. wget command

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:

20. whoami command

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:

21. sort command

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: (The content of multiple.txt file in the terminal)


Now we will sort the outcome using the sort command
Command:

Output:

Here first we checked the file content using the cat command and then we sorted it
alphabetically using the sort command.

22. cal 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:

23. whereis command


whereis command in Linux is generally used to see the exact location of any
command typed after this. Let’s see how this performs.
Command:

Output:

24. df command

df command in Linux gets the details of the file system.


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 −

Operator Description Example

+ (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

= (Assignment) Assigns right operand in left operand a = $b would assign


value of b into a

== (Equality) Compares two numbers, if both are same then [ $a == $b ] would


returns true. return false.

!= (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

to check to numbers are equal


a=2
b=3
if test "$a" -eq "$b"
then
echo " a and b is equal"
else
echo " both arenot queal"
fi
The above script will produce the following result −
a + b : 30
a - b : -10
a * b : 200
b/a:2
b%a:0
a is not equal to b
The following points need to be considered when using the Arithmetic Operators −
 There must be spaces between the operators and the expressions. For example, 2+2 is
not correct; it should be written as 2 + 2.
 Complete expression should be enclosed between ‘ ‘, called the inverted commas.
 You should use \ on the * symbol for multiplication.
 if...then...fi statement is a decision-making statement which has been explained in the
next chapter.

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.

wc command in Linux with examples


wc stands for word count. As the name implies, it is mainly used for counting
purpose.
 It is used to find out number of lines, word count, byte and characters
count in the files specified in the file arguments.
 By default it displays four-columnar output.
 First column shows number of lines present in a file specified, second column
shows number of words present in the file, third column shows number of
characters present in file and fourth column itself is the file name which are
given as argument.
Syntax:

localhost:~# cat > wlcc.txt


this
is
MIT
collge
.
this
is ai ds brach
localhost:~# wc wlcc.txt
7 10 44 wlcc.txt
7 line 10 words 44 characters wlcc is file name .
Note : When more than file name is specified in argument then command will
display four-columnar output for all individual files plus one extra row displaying
total number of lines, words and characters of all the files specified in argument,
followed by keyword total. Options: 1. -l: This option prints the number of
lines present in a file. With this option wc command displays two-columnar output,
1st column shows number of lines present in a file and 2nd itself represent the file
name.
With one file name
localhost:~# wc -l wlcc.txt
7 wlcc.txt
7 line only

With more than one file name


localhost:~# wc -l wlcc.txt wlcd.txt
7 wlcc.txt
4 wlcd.txt
11 total
Gives number of line in both file and total number of 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

With more than one file name


$ wc -w state.txt capital.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

With more than one file name


$ wc -c state.txt capital.txt
58 state.txt
39 capital.txt
97 total
4. -m: Using -m option ‘wc’ command displays count of characters from a file.
With one file name
$ wc -m state.txt
56 state.txt

With more than one file name


$ wc -m state.txt capital.txt
58 state.txt
39 capital.txt
97 total
5. -L: The ‘wc’ command allow an argument -L, it can be used to print out the
length of longest (number of characters) line in a file. So, we have the longest
character line Arunachal Pradesh in a file state.txt and Hyderabad in the
file capital.txt. But with this option if more than one file name is specified then the
last row i.e. the extra row, doesn’t display total but it display the maximum of all
values displaying in the first column of individual files. Note: A character is the
smallest unit of information that includes space, tab and newline.
With one file name
$ wc -L state.txt
17 state.txt

With more than one file name


$ wc -L state.txt capital.txt
17 state.txt
10 capital.txt
17 total
6. –version: This option is used to display the version of wc which is currently
running on your system.
$ wc --version
wc (GNU coreutils) 8.26
Packaged by Cygwin (8.26-1)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Paul Rubin and David MacKenzie.


Applications of wc Command
1. To count all files and folders present in directory: As we all know ls command
in unix is used to display all the files and folders present in the directory, when it is
piped with wc command with -l option it display count of all files and folders
present in current directory.
$ ls gfg
a.txt
b.txt
c.txt
d.txt
e.txt
geeksforgeeks
India

$ 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?

localhost:~# split wlcc.txt -l3 wlcn


localhost:~# ls
MIT newaa split.txtab wlcc.txt wlcnac
bench.py newab wlcaa wlcd.txt xaa
hello.c readme.txt wlcab wlcnaa xab
hello.js split.txtaa wlcac wlcnab xac
localhost:~# cat wlcaa //part 1 with file name wlcaa (postfix aa added)
this
is
MIT //3 line in parts ,patr1 named wlcnaa
localhost:~# cat wlcnab
collge
.
This // part 2 named is wlcnab (part 2 postfix ad added)

localhost:~# cat wlcnac


is ai ds brach
jdbgfjkhsdfbksjdf //rest 2 line in part3 named wlcnac
localhost:~#

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

localhost:~# split wlcc.txt -l3 wlcn

Herer in last” wlcn” is prefix with 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:~# cat > name1


a
a
b
b
c
d
e
k
l
localhost:~# cat > name2
a
b
b
c
m
l
y
localhost:~# cat name1 name2|sort|uniq -d
a
b
c
l
6. Create a program to find out the inode number of any desired file.

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:~#

here it is all files with their read,write,execute permission .

There’s a lot of information in those lines.


1. The first character = ‘-‘, which means it’s a file
‘d’, which means it’s a directory.
2. The next nine characters = (rw-r–r–) show the security
3. The next column shows the owner of the file. (Here it is `root`)
4. The next column shows the group owner of the file. (Here it is `root` which has
special access to these files)
5. The next column shows the size of the file in bytes.
6. The next column shows the date and time the file was last modified.
7. Last Column = File_name or Directory_name. (For example, here are: prac, snap,
test, example)

Security permissions in Linux


First, you must think of those nine characters as three sets of three characters (see
the box at the bottom). Each of the three “rwx” characters refers to a different
operation you can perform on the file.
--- --- ---
rwx rwx rwx
user group other

Read, write, and execute.

Letter
s Definition

‘r’ “read” the file’s contents.

‘w’ “write”, or modify, the file’s contents.

‘x’ “execute” the file. This permission is given only if the file is a program.

Symbols: `+`, `-` and `=`


Operator
s Definition

`+` Add permissions

`-` Remove permissions

`=` Set the permissions to the specified values

User, group, and others

Reference Class Description

The user permissions apply only to the owner of the file or


`u` user directory, they will not impact the actions of other users.
Reference Class Description

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.

The other permissions apply to all other users on the system,


`o` others this is the permission group that you want to watch the most.

All
All three (owner, groups, others)
`a` three

Reading the security permissions

For example: “rw- r-x r–“


 “rw-“: the first three characters `rw-`. This means that the owner of the file can
“read” it (look at its contents) and “write” it (modify its contents). we cannot
execute it because it is not a program but a text file.
 “r-x”: the second set of three characters “r-x”. This means that the members of
the group can only read and execute the files.
 “r–“: The final three characters “r–” show the permissions allowed to other users
who have a UserID on this Linux system. This means anyone in our Linux world
can read but cannot modify or execute the files’ contents.

we can change permission also.

localhost:~# chmod g+x hello.c


localhost:~# ls -l
total 16
-rw-r--r-- 1 root root 114 Jul 5 2020 bench.py
-rw-r-xr-- 1 root root 76 Jul 3 2020 hello.c // here permissionhas changed by chmod
command//
-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:~#
8. Write a pipeline of commands, which displays on the monitor as well as saves the
information about the number of users using the system at present on a file called usere.ux.

9. localhost:~# cat >user.ux


10. this is a
11.
12. b
13. v
14. c
15. f
16. localhost:~# ls -l
17. total 28
18. -rw-r--r-- 1 root root 114 Jul 5 20
20 bench.py
19. -rw-r--r-- 1 root root 76 Jul 3 20
20 hello.c
20. -rw-r--r-- 1 root root 22 Jun 26 20
20 hello.js
21. -rw-r--r-- 1 root root 18 Jul 26 14:
29 name1
22. -rw-r--r-- 1 root root 14 Jul 26 14:
29 name2
23. -rw-r--r-- 1 root root 151 Jul 5 20
20 readme.txt
24. -rw-r--r-- 1 root root 19 Jul 26 14:
41 user.ux

Created user.ux file here.

localhost:~# ls -l|grep user.ux|wc –l //here we grep user.ux file


name from list of file and count it by wc –l command//
1 // number where user.ux is present in file//
localhost:~#

9. Execute shell commands through vi editor.


Vi editor is editor like in word. Here we can create file ,modify n delete the files.
To work with vi editor there are some steps to follow.
1. Open vi mode with “ vi<file name>” like vi myfile
2. localhost:~# vi myfile
3.
below screen will open

this is a file
~
~
~
~
~
~
- myfile 1/1 100%

Now press “ i ” to insert the text .

this is a file
~
~
~
~
~
~
I myfile 1/1 100% // here I is showing that it is insert mode//

Than type anything in file.


We can come out from insert command by pressing Esc button. So I will remove from here.

this is a file
~
~
~
~
~
~
- myfile 1/1 100%// by doing Esc I is removed//

Now press shift+:

:this is a file
~
~
~
~:

Above screen will appear.


Now press “ wq “ to save and quit the file.
To see file use cat command like cat myfile.
“ q!” it will quit the file without save.

Now perform replce,delete,undo,remove one character from front of line.


 First if it is insert mode on than esc from this mode.
 Than to replace any character press the cursor where character is present, than press “
r “.
 To delete complete line put cursor at front of line and press double time“ d “.
 And to undo press “ u “.
 To remove character from line press “ x “at front of line.
 To search any word in file press “ / “ than searching word.
 From bottom to tom search press “ ? “ than searching word.
By “ less <file name> “ like myfile we can also open file.
To go at End of file use “ shift+g ”

11. Write a shell script that accepts any number of arguments and prints them in the reverseorder.

#!/bin/bash

echo "cheking number of argument"


if [ $# -eq 00 ]
then
echo "no argument supiled"
exit

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

localhost:~# ./rever hello this is AIDS


cheking number of argument
total no. of rgument : 4
argument are :hello this is AIDS
number of argument in reverse
AIDS is this hello

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

https://bellard.org/jslinux/vm.html?url=alpine-x86.cfg&mem=192 command line

https://www.tutorialspoint.com/execute_bash_online.php shall script

You might also like