0% found this document useful (0 votes)
70 views31 pages

Basic UNIX and Linux Commands Guide

Personal use
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)
70 views31 pages

Basic UNIX and Linux Commands Guide

Personal use
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

B.

E 2nd Year 4th Sem 230330107016

Practical:-1
AIM:- Study of Basic commands of UNIX.
INTRODUCTION:
The UNIX operating system is a set of programs that act as a link between the computer and the user

⮚ UNIX was originally developed in 1969 by a group of AT&T employees at Bell Labs,including
Ken Thompson, Dennis Ritchie, Douglas McIlroy, and Joe Ossanna.

⮚ There are various UNIX variants available in the market. Solaris UNIX, AIX, UP UNIX and
BSD are few examples. Linux is also a flavor of UNIX which is freely available.
UNIX ARCHITECTURE:
The main concept that unites all versions of UNIX is the following four basics:
1. Kernel: The kernel is the heart of the operating system. It interacts with hardware and most of
the tasks like memory management, job scheduling and file management.
2. Shell: The shell is the utility that processes your requests. When you type in a command at
your terminal, the shell interprets the command and calls the program that you want. The shell uses
standard syntax for all commands. C Shell, Bourne Shell and Korn Shell are most famous shells
which are available with most of the UNIX variants.
3. Commands and Utilities: There are various command and utilities which you would use in
your day to day activities. cp, mv, cat and grep etc. are few examples of commands and utilities.
There are over 250 standard commands plus numerous others provided through 3rd party software.
All the commands come along with various optional options.
4. Files and Directories: All data in UNIX is organized into files. All files are organized into
directories. These directories are organized into a tree-like structure called the file system.
BASIC UNIX COMMANDS:
File Commands:
1. Command: ls
To lists the files in the current directory.
ls has many options:
-l - long list (Displays lots of info)

MGITER/CE/OS/2025 1
B.E 2nd Year 4th Sem 230330107016

-S - lists by size
-h - lists file sizes in human readable format
-F - List files of Directory
-a - Displays all files including hidden files.
-b - Displays nonprinting characters in octal.
-c - Displays files by file timestamp.
-C - Displays files in a columnar format (default) -d
- Displays only directories.
-f - Interprets each name as a directory, not a file.
-g - Displays the long format listing, but excludes the owner name.
-i - Displays the inode for each file.
-L - Displays the file or directory referenced by a symbolic link.
-m - Displays the names as a comma-separated list.
-n - Displays the long format listing, with GID and UID numbers.
-o - Displays the long format listing, but excludes group name.
-p - Displays directories with /
-q - Displays all nonprinting characters as ?
-r - Displays files in reverse order.
-R - Displays subdirectories as well.
-t - Displays newest files first. (Based on timestamp) -u
- Displays files by the file access time.
-x - Displays files as rows across the screen.
-1 - Displays each entry on a line.
2. Command: cd

⮚ cd stands for change directory.


⮚ cd dir_name
Moves to directory called dir_name

⮚ cd ~ Moves to your home directory


⮚ cd .. Moves one level hierarchy down from the current directory

MGITER/CE/OS/2025 2
B.E 2nd Year 4th Sem 230330107016

⮚ cd .. /../ Moves two level hierarchy down from the current directory
⮚ cd - Moves to your previous directory
3. Command: mkdir
To create a new directory use “mkdir”.
Syntax: $ mkdir directoryname $mkdir
–p dir1/dir2/dir3.

It will create the directory tree.dir3 will created under dir2 and dir2 create under dir1.
$ mkdir dir5
$ cd !$ . It will point the location of dir5.
$ mkdir dir5; cd dir5.
It will create dir5 first and then point the location of [Link] above two commands. (‘;’
called command separator)
4. Command: cat
This command is used to create, view and concatenate files.
$ cat filename It will display the contents of the file filename.
$cat >flie1 Success is not a destination.
$cat flie1 file2 >file3 The above command is used to write contents of the file1 and file 2 into file3
5. Command: cp
Syntax $ cp [options] Source Destination
Copies Source into Destination
$ cp file1 file2 Copies file1 into file2
Prompts before overwriting
6. Command: mv
To move a file to different location use “mv”.
$ mv [options] Source Destination mv
can also be used to rename a file. $ mv
filename1 filename2 (Rename file)
$mv /home/kacper/top.v /hdd/kacper/backup/
Moves the top.v into backup directory

MGITER/CE/OS/2025 3
B.E 2nd Year 4th Sem 230330107016

$mv -i /home/kacper/top.v /hdd/kacper/backup/


Asks before over writing the file
7. Command: rm
To remove a file use “rm”. Syntax: $ rm filename rm –i * prompts you
before deleting a file. The “i” stands for interactive. rm –rf * recursively
removes all files and subdirectories in your current directory, without
prompting to delete files.

Be very careful, deletions are permanent in UNIX/LINUX.


8. Command: rmdir
To remove a empty directory use “rmdir”.
Syntax :$ rmdir directoryname
9. Command: pwd
To find your current path use “pwd”.
Syntax: $ pwd
Displays the present working directory.
10. Command: chmod
The chmod command changes the access mode of one file or multiple files.
The syntax for the chmod command is:
chmod [option] mode files Options:
-R Descend directory arguments recursively while setting modes.
-f Suppress error messages if command fails.
Mode:
Who u=user, g=group, o=other, a=all (default)
Opcode

+ means add permission


- means remove permission
= means assign permission and remove the permission of fields Permission
r=Read, w=write, x=Execute, s=set uid bit, t=sticky bit
u=user, g=group, o=other, l=mandatory locking

MGITER/CE/OS/2025 4
B.E 2nd Year 4th Sem 230330107016

Examples: chmod 751 tech chmod u=rwx, g=rx, o=x


tech chmod =r tech

Practical:-2
AIM:- Study of Basic commands of Linux.
Display Commands:
1. Command: less
“less” displays a file, allowing forward/backward movement within it.
Use “/” to search for a string in the file.
Press “q” to quit.
Syntax:$ less [options]filename
Options
-c clears the screen before displaying.
+n starts printing from nth line
2. Command: head
“head” displays the top part of a file. By
default it shows the first 10 lines.
-n allows you to change the number of lines to be shown.
Syntax:$ head [options]filename
Example: “head –n50 [Link]” displays the first 50 lines of the [Link]
$head -18 filename
Displays the first 18 lines of the file called filename.
3. Command: tail
Displays last 10(by default) lines of a file.
Same as head command.
Syntax:$ tail filename

MGITER/CE/OS/2025 5
B.E 2nd Year 4th Sem 230330107016

Displays the last 10 lines from the ending


$tail -12 filename
Displays the last 12 lines from the ending
4. Command: more
Read files and displays the text one screen at a time.
Syntax: $ more [options] filename Options:
-c clears the screen before displaying.
-n displays the first n lines of the file. We can also see next lines by pressing [Enter]
+n displays the lines from nth line.

MGITER/CE/OS/2025 6
B.E 2nd Year 4th Sem 230330107016

Practical:-3
AIM:- Study of Advance commands and filters of Linux/UNIX.
File Permissions Commands:
Each file in UNIX/LINUX has an associated permission level.
This allows the user to prevent others from reading/writing/executing their files or directories.
Use “ls –l filename” to find permission level of that file.
The permission levels are “r”
means “read only” permission.
“w” means “write” permission.
“x” means “execute” permission.
In case of directory, “x” grants permission to list directory contents.
1. Command: chmod
If you own a file, you can change its permissions with “chmod”.
Syntax: $ chmod [user/group/others/all]+[permission] filename
2. Command: chgrp
Change the group of the file.
3. Command: chown
Change the owner of the file.
System Resource Commands:
1. Command: date
Reports the current date and time.
Syntax:$ date
List of user commands:
1. Command: who
Lists all users currently on system.
Syntax: $ wh
2. Command: who am i
Reports the details about the command user.
Syntax:$ who am i

MGITER/CE/OS/2025 7
B.E 2nd Year 4th Sem 230330107016

3. Command: whoami
Reports the username of the command user.
Syntax:$ whoami
Commands for showing file details:
1. Command: wc
$ wc[options] filename.
Gives the number of lines, words and characters in a file called filename
wc –l filename Gives the number of lines wc –w filename Gives the
number of words wc –c filename
Gives the number of characters
2. Command: sort
It prints the lines of the file in sorted order.
Syntax: $ sort filename
3. Command: touch
Used to create empty file.
Syntax :$touch file2
It will create the file called file2 of size zero bytes, if file2 doesn’t exist. Used
to change the time stamps.(i.e. dates and times of the recent modification or
access)
For example to change the last access time of file6 to 10:10 a.m. May 2, 2009, it
can be done in the following way.
$ touch –d ‘2 May 2009 10:10’ file6.
It will change the time stamp of the [Link] can be verified by the following
way.
$ ls –l file6
-rw-r--r-- 1 root root 120 May 2 2009 file6
$ touch –t 08021130 file7
It can change the date and time. The expression 08021130 denotes the
Month, day, hour and minute. In general the expression type is

MGITER/CE/OS/2025 8
B.E 2nd Year 4th Sem 230330107016

MMDDhhmm.
4. Command: tee
Sends the output in two directions at a time Syntax:
tee [options] filename.

Ex : $ls –l |tee file2


It will list all the files in the current directory and as well stores in the file called
file2.
$ls –l |tee –a file2
It will append the list to file2.
5. Command: cmp
This cmp command tests whether two files are identical and reports position of
first character.
It shows 0 if they are identical, otherwise it shows 1.
Syntax: $ cmp filename1 filename2
6. Command: find
This command is used to find the location of a file.
Syntax: find path selection criteria
Ex: $find . –name “top.v”
It will look for top.v in the current directory.
$find . –type d
Finds all directories.
$find /home/kacper -type d
Finds all directories and subdirectories inside the kacper
$find /home/kacper -type d -name “.*”
Finds all hidden directories
$find . –type f
Finds all normal files $find
. –type f –name”.*”
Finds all hidden files

MGITER/CE/OS/2025 9
B.E 2nd Year 4th Sem 230330107016

7. Command: diff
$ diff filename1 filename2
It compares two files for differences.
$vimdiff filename1 filename2 or sdiff filename1 filename2
It will display the two files side by side Text
Processing Commands:
1. Command: grep
The grep command is to search the regular expressions in LINUX files. fgrep searches for exact
strings and egrep uses “extended” regular expressions.
Syntax:$ grep [options] regexp [filename]
grep “module” top.v It will look for
module in top.v grep -i Ignores the case
-n Display numbers
-e Multiple patterens(grep –e “patrn1” –e “patrn2” filename)

MGITER/CE/OS/2025 10
B.E 2nd Year 4th Sem 230330107016

Practical:-4
Write a shell script to enter a string and print it.
Input:
echo "Enter String" read
str
echo "print string="$str

output:

MGITER/CE/OS/2025 11
B.E 2nd Year 4th Sem 230333107014

Practical:-5
Write a shell script to find arithmetic operation on number given by user.

Input:
echo "Enter The First Number:"
read num1

echo "Enter The Second Number:"


read num2

# Perform arithmetic operations


echo "--------------------------------------------"
echo "| Arithmetic operations on $num1 and $num2 |"
echo "--------------------------------------------"
echo "ADDITION : $(($num1 + $num2))"
echo "SUBTRACTION : $(($num1 - $num2))"
echo "MULTIPLICATION : $(($num1 * $num2))"
echo "DIVISION : $(($num1 / $num2))"
echo "MODULE : $(($num1 % $num2))"

output:

MGITER/CE/OS/2024 12
B.E 2nd Year 4th Sem 230330107004

Practical:-6
Write a shell script to display multiplication table of given number.
echo "Which Number To Generate Multiplication Table"

Input :
read number
i=1
while [ $i -le 10 ]
do
echo " $number * $i =`expr $number \* $i ` "
i=`expr $i + 1`
done

output:

Practical:-7
Write a shell script to find greater number out of three number.

MGITER/CE/OS/2025 13
B.E 2nd Year 4th Sem 230330107004

Input:
echo "Enter the value of a:"
read a
echo "Enter the value of b:"
read b
echo "Enter the value of c:"
read c
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo "A is maximum"
elif [ $b -gt $c ]
then
echo "B is maximum"
else
echo "C is maximum"
fi

output:

MGITER/CE/OS/2025 14
B.E 2nd Year 4th Sem 230330107004

Practical:-8
Write a shell script to generate mark sheet of students take three subjects and
calculate and display total marks, percentage and class obtain. echo
"*****************" echo "Student Marksheet" echo "*****************" echo
"EnterOperating

Input:
echo "*****************"
echo "Student Marksheet"
echo "*****************"
echo "Enter Operating System Marks:"
read os
echo "Enter C++ Marks:"
read cpp
echo "Enter Java Marks:"
read java
echo "*****************"
total=`expr $os + $cpp + $java`
echo "Total Marks:"$total
percentage=`expr $total / 3`
echo "Percentage:" $percentage %
if [ $percentage -ge 60 ]
then
echo "Class: First Class Distinction"
elif [ $percentage -ge 50 ]
then
echo "Class: First class"
elif [ $percentage -ge 40 ]
then

MGITER/CE/OS/2025 15
B.E 2nd Year 4th Sem 230330107004

echo "Class: Second class"


else
echo "Class: Fail"
fi
echo "*****************"

Output:

Practical:-9

MGITER/CE/OS/2025 16
B.E 2nd Year 4th Sem 230330107004

Write a shell script to find factorial.


Input:
factorial()
{
if [ $1 -eq 0 ]; then
echo 1
else
echo $(( $1 * $(factorial $(( $1 - 1 ))) ))
fi
}
echo "Enter a number to find factorial:"
read num
if [[ $num -lt 0 ]]; then
echo "Factorial is not defined for negative numbers."
elif [[ $num -eq 0 ]]; then
echo "Factorial of 0 is 1."
else
result=$(factorial $num)
echo "Factorial of $num is $result"
fi

output:

MGITER/CE/OS/2025 17
B.E 2nd Year 4th Sem 230330107004

Practical:-10
Write a shell script to check given number is prime number or not.
Input:
echo enter number
read n
c=0
i=2
t=$(( n-1 ))
while [ $i -le $t ]
do
q=$(( n%i ))
if [ $q -eq 0 ]
then
c=$(( c+1 ))
fi
i=$(( i + 1 ))
done
if [ $c -eq 0 ]
then
echo "prime"
else
echo "not prime"
fi

output:

MGITER/CE/OS/2025 18
B.E 2nd Year 4th Sem 230330107004

Practical:-11
Write a shell script which will generate first n Fibonacci numbers like: 1,
2, 3, 5, 13..
Input:
echo "Enter Numbers."
read n
a=0
b=1
i=3
echo -n $a
echo -n " "
echo -n $b

while [ $i -le $n ]
do
c=`expr $a + $b`
echo -n " "
echo -n $c
a=$b;
b=$c;
i=`expr $i + 1 `
done

output:

MGITER/CE/OS/2025 19
B.E 2nd Year 4th Sem 230330107004

Practical:-12
Write a menu driven program which has following options:
Input:
a) Users of system
b) List of files
c) Present working directory
d) Exit
e)Password

show_users()
{
echo "Users of the system:"
ls /home
}
list_files()
{
echo "List of files:"
ls
}

MGITER/CE/OS/2025 20
B.E 2nd Year 4th Sem 230330107004

show_pwd()
{
echo "Present working directory:"
pwd
}
get_password()
{
echo -n "Enter the password: "
read password
if [ "$password" = "your_password" ]; then
echo "Password accepted!"
else
echo "Incorrect password!"
fi
}
while true; do
echo -e "\nMenu:"
echo "a) Users of system"
echo "b) List of files"
echo "c) Present working directory"
echo "d) Exit"
echo "e) Password"
read -p "Enter your choice: " choice
case $choice in
a) show_users;;
b) list_files;;
c) show_pwd;;
d) echo "Exiting program. Goodbye!"; break;;

MGITER/CE/OS/2025 21
B.E 2nd Year 4th Sem 230330107004

e) get_password;;
*) echo "Invalid choice. Please enter a valid option.";;
esac
done

output:

Practical:-13
AIM:- Write a shell script to check entered string is palindrome or not.

Input:

MGITER/CE/OS/2025 22
B.E 2nd Year 4th Sem 230330107004

echo "Input your string without space"


read vstr
for i in $(seq 0 ${#vstr})
do
rvstr=${vstr:$i:1}${rvstr}
done

echo "Input string was :" $vstr


echo "After Reversng String Is :" $rvstr

if [ "$vstr" = "$rvstr" ]
then
echo "String Is Palindrome."
else
echo "String Is Not Plaindrome."
fi

output:

MGITER/CE/OS/2025 23
B.E 2nd Year 4th Sem 230330107004

Practical:-14
AIM:- Write a shell script to generate sum of all digit of given number.

echo "Enter Number"


read n
sum=0
while [ $n -ne 0 ]
do
x=$(( n%10 ))
sum=$(( sum+x ))
n=$(( n/10 ))
done
echo "Sum=$sum"

output:

Practical:-15
AIM:- Study of UNIX Shell and Environment Variables.
What is a Variable?

In computer science, a variable is a location for storing a value which can be a filename, text, number
or any other data. It is usually referred to with its Symbolic name which is given to it while creation.
The value thus stored can be displayed, deleted, edited and re-saved.

MGITER/CE/OS/2025 24
B.E 2nd Year 4th Sem 230330107004

Variables play an important role in computer programming because they enable programmers to write
flexible programs. As they are related to the Operating system that we work on, it is important to
know some of them and how we can influence them.
Unix shell variable

Name Meaning
$HOM
Absolute pathname to your home directory.
E
$PATH List of directories to search for commands.
$USER Your user name.
$SHEL
Absolute pathname of your login shell.
L
$TERM The type of your terminal.

What are Environment variables?

Environment variables are dynamic values which affect the processes or programs on a computer.
They exist in every operating system, but types may vary. Environment variables can be created,
edited, saved, and deleted and give information about the system behavior. Environment variables
can change the way a software/programs behave.
E.g. $LANG environment variable stores the value of the language that the user understands. This
value is read by an application such that a Chinese user is shown a Mandarin interface while an
American user is shown an English interface. Let's study some common environment variables –

MGITER/CE/OS/2025 25
B.E 2nd Year 4th Sem 230330107004

Variable Description
PATH This variable contains a colon (:)-separated list of directories in which your
system looks for executable files.

When you enter a command on terminal, the shell looks for the command in
different directories mentioned in the $PATH variable. If the command is found, it
executes. Otherwise, it returns with an error 'command not found'.
USER The username

HOME Default path to the user's home directory

EDITO Path to the program which edits the content of files


R

UID User's unique ID

TERM Default terminal emulator

SHELL Shell being used by the user

Accessing Variable values

In order to determine value of a variable, use the


command echo $VARIABLE
Variables are- Case Sensitive. Make sure that you type the variable name in the right letter
case otherwise you may not get the desired results.

MGITER/CE/OS/2025
B.E 2nd Year 4th Sem 230330107004

The 'env' command displays all the environment variables.

Set New Environment Variables


You can create your own user defined variable, with syntax
VARIABLE_NAME= variable_value
Environment variables govern the behavior of programs in your Operating System.

Command Description

echo $VARIABLE To display value of a variable

env Displays all environment variables

VARIABLE_NAME= variable_value Create a new variable

unset Remove a variable

export Variable=value To set value of an environment variable

Practical:-16
Write a shell script to find whether given number is even or.
Input:
MGITER/CE/OS/2025
B.E 2nd Year 4th Sem 230330107004

echo "enter n"


read n
a=`expr $n % 2`
if [ $a -eq 0 ]
then
echo "number is even"
else
echo "number is odd"
fi

output:

Practical:-17
Write a shell script to find the length of given string.

echo "enter string"


read n
l=`expr $n | wc -c`
le=`expr $l - 1`
echo "length=$le"

output:

MGITER/CE/OS/2025
B.E 2nd Year 4th Sem 230330107004

Practical:-18
Write a shell script to perform calculator using switch case.

Input:

echo enter a
read a
echo enter b
read b
echo enter operation [1: addition,2: substraction,3: multiplication,4: division,5: modulo]
read op
case $op in
1) expr $a + $b;;
2) expr $a - $b;;
3) expr $a \* $b;;
4) expr $a / $b;;
5) expr $a % $b;;
esac

output:

MGITER/CE/OS/2025
B.E 2nd Year 4th Sem 230330107004

Practical:-19
Write a shell script to find out number of character, words and lines
from a given file.
Input:
echo "Enter file name"
read f
c=`cat $f | wc -c`
w=`cat $f | wc -w`
l=`cat $f | wc -l`
echo "character=$c"
echo "word=$w"
echo "line=$l"

output:

Practical:-20
Write a shell script to concate two strings and find the length of
resultant string.
Input:
MGITER/CE/OS/2025
B.E 2nd Year 4th Sem 230330107004

echo enter s1
read s1
echo enter s2
read s2
s3=$s1$s2
echo "string=$s3"
l=`expr $s3 | wc -c`
len=`expr $l - 1`

echo "length=$len"

output:

MGITER/CE/OS/2025

You might also like