Os Record
Os Record
DATE: 17.08.2023
COMMAND:
pwd
OUTPUT:
/home/edith
COMMAND:
ls
OUTPUT:
Desktop temp os
COMMAND:
cd
pwd
OUTPUT:
/home/
COMMAND:
pwd
OUTPUT:
/home/Edith/os
COMMAND:
cd
OUTPUT:
Path changed to home dir
6. From your HOME directory, list the contents of the directory ‘temp’ under your HOME directory:
COMMAND:
ls os
OUTPUT:
Ex1
7. Copy the file 'first-a' from the HOME directory to the directory ‘temp’:
COMMAND:
cp first-a os/copy
OUTPUT:
Copy created
8. Verify that the file has been copied:
COMMAND:
cd os
ls
OUTPUT:
copy
9. Copy the file 'first-a' to another file called 'First-a' under your HOME directory:
COMMAND:
cd
cp first-a First-a
OUTPUT:
First-a created and is a copy of first-a
10. List the contents of the files 'first-a' and 'first-b' through one command:
COMMAND:
cat first-a first-b
OUTPUT:
Contents of first-a and first-b displayed
EX NO: 3 WORKING WITH UNIX COMMANDS -2
DATE: 17.08.2023
COMMAND:
mkdir $HOME/temp/newdir
ls
OUTPUT:
newdir
2. From your HOME directory, move all files from the directory 'temp' to the directory 'newdir':
COMMAND:
mv $HOME/temp/* $HOME/temp/newdir/
OUTPUT:
All content copied
COMMAND:
mv $HOME/temp/newdir $HOME/temp/olddir
ls
OUTPUT:
olddir
COMMAND:
ls $HOME/temp
OUTPUT:
olddir
5. Remove the file 'first-a' from 'olddir' and check to see if it has been removed:
COMMAND:
rm $HOME/temp/olddir/first-a
OUTPUT:
first-a removed
6. Rename the file 'gold-words' under the directory 'olddir' as 'golden-words' and verify that the file
has been renamed:
COMMAND:
mv $HOME/temp/olddir/gold-words $HOME/temp/olddir/golden-words
ls
OUTPUT:
golden-words
7. Change the working directory to the parent directory of your HOME directory and display the name
of this directory:
COMMAND:
cd ..
pwd
OUTPUT:
home/edith
COMMAND:
cd $HOME
pwd
OUTPUT:
home/edith
9. Remove all the files under the 'olddir' under the 'temp' directory:
COMMAND:
rm $HOME/temp/olddir/*
OUTPUT:
All files removed
COMMAND:
cat 9ABC
OUTPUT:
HELLO WORLD!!
11. List out the names of all files that end with '.c':
COMMAND:
ls * .c
OUTPUT:
ex1.c ex2.c
EX NO: 4 WORKING WITH UNIX COMMANDS -3
DATE: 17.08.2023
1. List out the names of all files that are 5 characters long:
COMMAND:
ls ?????
OUTPUT:
file1.c
2. Remove the files whose names are listed out by the command of (List out the names of all files that
end with ‘.c’.) (Exercise 3). Use only one command to do so:
COMMAND:
rm *.c
ls
OUTPUT:
No:’.c’ file in directory
3. Display the names of all files that start with a lowercase alphabet followed by a digit and ending
with '.c' or '.o':
COMMAND:
ls [a-z][0-9]*.c *.o
OUTPUT:
A1.c a2.o
4. Move all the files whose names end with '.c' or '.o' to the directory 'olddir':
COMMAND:
mv *.c *.o ~/olddir/
OUTPUT:
A1.c a2.o
5. In one command, remove all the files in 'olddir' and the directory itself:
COMMAND:
rm -r olddir
OUTPUT:
Directory deleted
COMMAND:
cat xyz
OUTPUT:
cat:xyz: No such file or directory
COMMAND:
PWD
OUTPUT:
PWD: command not found
8. Try creating a directory that already exists and observe what happens:
COMMAND:
mkdir os
OUTPUT:
mkdir: cannot create directory 'os': File exists
COMMAND:
ls
OUTPUT:
temp newdir
10. List out the names of all files in your HOME directory in reverse order:
COMMAND:
cd
ls -r
OUTPUT:
newdir temp
11. Copy the file 'first-a' from the HOME directory to the directory 'temp' under your HOME
directory:
COMMAND:
mv first-a temp/
ls
OUTPUT:
first-a
12. Create a new directory 'letters' under the 'temp' directory:
COMMAND:
mkdir temp/letters
ls
OUTPUT:
first-a letters
13. From your HOME directory, move all files from the directory 'temp' to the directory 'letters':
COMMAND:
mv temp/* temp/letters/
OUTPUT:
Directory and files moved
14. List out the names of all filenames that start with the digit '9':
COMMAND:
ls 9*
OUTPUT:
9abc
EX NO: 5 WORKING WITH SHELL SCRIPTS FUNDAMENTALS
DATE: 22.08.2023
1. Give the command to display the message WARNING… and then sound the bell.
COMMAND:
echo -e "WARNING...\a"
OUTPUT:
WARNING… [and then bell sounds]
2. Give the command to display the message WARNING… followed by a blank line and then sound
the bell.
COMMAND:
echo -e "WARNING...\n\a"
OUTPUT:
WARNING…
[and then bell sounds]
3. Give the command to display the message WARNING… keep the cursor on the same line and then
sound the bell.
COMMAND:
echo -n "WARNING...\a"
OUTPUT:
WARNING…[and then bell sounds]
4. Write a shell script to display the following (blank line) (blank line) The echo command displays its
arguments on the VDU.
COMMAND:
#!/bin/bash
echo -e "\n\nThe echo command"
echo "displays its arguments"
echo "on the VDU."
OUTPUT:
The echo command
Displays its arguments
On the VDU
b. echo {VERSION}
Incorrect: echo {VERSION}
Correct: echo ${VERSION}
6. What would happen if the following command is specified?
COMMAND:
echo “VERSION”
OUTPUT:
VERSION
7. Write a shell script called WELCOME that asks the user to enter his/her name and then displays the
message:
COMMAND:
#!/bin/bash
echo "Enter your name:"
read USERNAME
echo "Hello $USERNAME! Welcome to this UNIX session!"
OUTPUT:
Enter your name:
Edith
Hello Edith! Welcome to this UNIX session!
COMMAND:
read ${VERSION}
OUTPUT:
zsh: not an identifier : UNIX VERSION 5.3
9. Give the command to display the name of the HOME directory from any directory.
COMMAND:
echo $HOME
OUTPUT:
home/edith
10. What would the result of the cd command (without any arguments) be if the HOME variable is set
as follows:
COMMAND:
$HOME="/user/root"
OUTPUT:
No such file or directory
11. Give the command to display the contents of the variable PATH.
COMMAND:
echo $PATH
OUTPUT:
The whole path displayed
12. Give the command to set the PATH to the directories root(/), /usr/bin and /bin, in that order.
COMMAND:
PATH="/usr/bin:/bin:$PATH"
OUTPUT:
Path is set
13. How can a user set the UNIX prompt to the following? Enter Command:>
COMMAND:
PS1="Enter Command:>"
OUTPUT:
Enter Command:>
COMMAND:
PS1="$"
OUTPUT:
$
COMMAND:
echo "${LOGNAME}"
OUTPUT:
Edith
COMMAND:
#!/bin/bash
echo "Hello $USER!"
echo "Your HOME directory is $HOME"
OUTPUT:
Hello Edith!
Your HOME directory is home/Edith
18. What do the variables $0, $1, and $2 contain when the following command is given?
cat data.file
20. When the following command is executed, what will each positional parameter contain? What will
the variable $* and $# contain?
COMMAND:
mv data1 /user/ram/data2
PARAMETERS:
- ‘$0’ contains the name of the script or shell being executed.
- ‘$1’ contains "data1"
- ‘$2’ contains "/user/ram/data2"
- ‘$*’ contains "data1 /user/ram/data2"
- ‘$#’ contains 2 (number of arguments).
EX NO: 6 WORKING WITH SHELL SCRIPTS -1
DATE: 31.08.2023
1. Write a shell script called check-type which asks for the name of a file to be displayed, reads the
name of the file on the same line and displays the contents of the file if it is an ordinary file. If it is not
an ordinary file, then the script should display the message:
CODE:
COMMAND:
bash check.sh
OUTPUT 1:
Enter the file name: a1.sh
Contents of the file
OUTPUT 2:
Enter the file name: xyz
“File does not exist OR is not ordinary, cannot display”
2. Modify #1, so that the script checks to see if the file is an ordinary file and is readable.
CODE:
COMMAND:
bash check.sh
OUTPUT:
CODE:
if [$# -gt 0 ]
then
if [ -f $1 ] && [ -x $1 ]
then
cat "$file"
elif [ -f $1 ] && [ !-x $1 ];then
echo "File exist but can not be executed"
fi
else
echo "No arguments passed"
fi
COMMAND:
bash check.sh
OUTPUT 1:
No arguments passed
OUTPUT 2:
Enter the file name: a1.sh
Contents of the file
EX NO: 7 WORKING WITH SHELL SCRIPTS -2
DATE: 05.09.2023
1. Write a shell script which is to be executed with one argument and which will
a. Display an error message if an argument is not entered or more than one argument is
entered at the command line
b. Check if the argument is a directory file and display the message ”DIRECTORY FILE” if
it is
c. Check if the argument is an ordinary file and display the message ”ORDINARY FILE” if
it is
CODE:
#!/bin/bash
if [ $# -eq 0 ];then
echo "Enter the arguement"
else
if [ -d "$1" ];then
echo "Directory File"
elif [ -f "$1" ];then
echo "Odinary File"
else
echo"Invalid File"
fi
fi
COMMAND:
bash check.sh
OUTPUT:
Odinary File
c. In case the file is ordinary and readable, it should display the file
d. In case the file is not ordinary and/or not readable, then the script should display an error
message.
CODE:
#!/bin/bash
COMMAND:
bash check.sh
OUTPUT:
3. Write a shell script called list-type which displays the following menu
d. Exit
The shell script should be executed with one argument (make a check). Display appropriate
messages if invalid number of arguments are passed, if the file is not a directory, if invalid
option is entered, etc.
CODE:
#!/bin/bash
if [ $# -eq 1 ];then
if [ -d $1 ]
then
echo –e “Long listing\n”
ls –l “$1”
echo –e “Long listing all files\n”
ls –a “$1”
echo –e “Long listing directory\n”
ls –d “$1”
else
echo “File not a directory”
fi
else
echo “Invalid no of arguments”
fi
OUTPUT:
Long listing
List of all the files in the directory
Long listing all files
List of all the files in the directory
Long listing directory
List of all directory
4. Write a shell script which takes as argument either R, W or X and displays the names of all ordinary
files for which the user has read, write and execute permissions, respectively. If no arguments are
entered, then the script should display the message:
Enter either R, W or X:
And accept the argument. If an invalid argument is given, the script should ring the system
bell and display an error message.
CODE:
#!/bin/bash
if [ $# -eq 0 ] || [ $# -gt 1 ];then
echo "Enter valid number of arguements"
else
if [ "$1" != "w" ] && [ "$1" != "r" ] && [ "$1" != "x" ];then
echo "Enter a valid argument”
else
if [ "$1" == "x" ];then
echo "Executable files are:"
find -type f -executable
elif [ "$1" == "r" ];then
echo "Readable files are:"
find -type f -readable
else
echo "Writable files are:"
find -type f -writable
fi
fi
fi
OUTPUT:
5.Write a shell script which takes one argument. If this argument is an ordinary file, display the
following message:
is an ordinary file-display?
If the answer is ‘y’, the file should be displayed if it has read permission; otherwise, the script should
display the following message and abort:
Sorry, has no read permission
If the argument is a directory, display a long listing of all files in that directory after getting
confirmation from the user and checking for permissions as above.
If the argument is a special file, display an appropriate message, otherwise display an error message
and exit.
CODE:
#!/bin/bash
if [ $# -eq 0 ] || [ $# -gt 2 ];then
echo "Enter Valid arguement"
else
if [ -f "$1" ];then
read -p "$1 is an odinary file-display?" choice
if [ "$choice" == "y" ];then
if [ -r "$1" ];then
cat "$1"
else
echo "Sorry, $1 has no read permission"
fi
else
exit 1
fi
elif [ -d "$1" ];then
read -p "$1 is a directory. Do you want a long list?" choice
if [ "$choice" == "y" ];then
ls -al "$1"
else
exit 1
fi
elif [ -c "$1" -o -b "$1" -o -p "$1" -o -S "$1" -o -h "$1" ];then
echo "The file you have entered is a special file"
else
echo "Invalid file or directory"
fi
fi
OUTPUT:
1. Write a shell script that checks if an argument string contains anything so that the script will stop
executing if the argument string is empty.
CODE:
If [ -n $1 ]
then
echo “String not empty”
else
echo “String empty”
fi
COMMAND:
bash ex8-1.sh
OUTPUT:
String empty
COMMAND:
bash ex8-1.sh a1.sh
OUTPUT:
String not empty
CODE:
read string
length=$(#string)
echo “Length: $length”
COMMAND:
bash ex8-2.sh
OUTPUT:
Edith
Length: 7
CODE:
if [ $# -gt 0 ]
then
var = $1
reverse = $(echo $var | rev)
echo “Reversed string: $reverse”
else
echo “Enter an argument”
fi
COMMAND:
bash ex8-3.sh edith
OUTPUT:
ahuG
1, 3, 2, 4, 3, 5, 4, 6, . . . , 100
CODE:
for ((i=1,j=3;i<=100,j<=100;i++,j++))
do
echo “$i,$j”
done
COMMAND:
bash ex8-4.sh
OUTPUT:
1, 3, 2, 4, 3, 5, 4, 6, . . . , 100
5. Write a shell script which will ask your full name (First name, Middle name, Last Name),
Year of Birth and Login name. The script should display the following
Welcome (Your full name)
Good Morning (Your first name)
Your Age is
Your user id is
Your home directory is
Number of Processes currently Running
Day of week is
Current time is
EX NO: 9 WORKING WITH SHELL SCRIPTS -4
DATE: 19.09.2023
1. Write a shell script to find the sum of digits of a number which is passed as a command line
argument.
CODE:
OUTPUT:
2. Write a shell script to reverse a given number which is passed as a command line argument.
CODE:
CODE:
EX NO: 10 WORKING WITH FILE COMMANDS
DATE: 20.09.2023
1. Under your current directory, display the names of all files and directories using the find command.
2. Create a data file called emp.dat which contains, empno,name,deptno,location and year of birth. Write a
shell script to display
3. the name, year of birth and location of the employee by accepting the employee
4. number as input. (Use While loop/For loop)
5. List the names of only the ordinary files in the directory subject and its subdirectories, under your
current working directory.
6. Find a count of the ordinary files in the directory subject.(use find command)
7. Using ls command, display the number of ordinary files in the directory subject and compare it with
the result of #3.
8. Display the names of all files and directories (and their files) in the parent directory of your HOME
directory.
jan.COMMAND:
find . –type f –name “jan*”
OUTPUT:
Jan.sh
85.COMMAND:
Find . –type f –name “*84” –o –name “*85”
OUTPUT:
Ex84 ex385
12. List the files in the directory subject which have been modified in exactly 9 days.
13. List the files in the directory subject which have been modified in more than 7 days.
14. List the files in the directory subject which have not been modified for the past 7 days.
15. Give a command to display the content s of all ordinary files in the current directory and all its
subdirectories.
COMMAND:
find . –type f –exec cat {}\;
OUTPUT:
Contents of all files are displayed
16. Give a command to remove all ordinary files with names ending in ‘o’ from the subdirectory 2016
and its subdirectories.
COMMAND:
find 2016 –type f –name “*o” –exec rm {}\;
OUTPUT:
Files are removed
17. Give a command to copy all ordinary files with names ending in 16 from the directory 2016 to the
directory backup which is under your current working directory.
COMMAND:
cp –r 2016/*16 backup/
OUTPUT:
Files are copied
18. Give a command to remove all ordinary files from the directory 2015 (and from its subdirectories)
which have been modified 7 days from the current day, with confirmation from the user before
removal of each file.
COMMAND:
Find 2015 . –type f –ctime –T –exec rm –i {}\;
OUTPUT:
Files are removed
19. Using find command remove all .bak files under the directory subject and its subdirectories except
the file data.bak.
COMMAND:
find subject . –type f –name “*.bak” . –name “data.bak” –exec rm{}\;
OUTPUT:
data.bak alone present
20. Give the command to compare the files x.c and y.c
21. Identify the directories under your HOME directory in which the files whose names begin with
prog exist.
COMMAND:
Find ~/ . –type –name “prog*” –exec dirname {}\;
OUTPUT:
program
22. Locate the files prog3 under your HOME directory. (Assume the file is found in 2 directories)
23. Use the cmp command to find out if the two files are the same or differ. If they are same, delete
that file which has been modified in exactly 1 day.
COMMAND:
Cmp file1.txt file2.txt && find . –type f –name “file1.txt” –ctime -1 –exec rm {}\;
OUTPUT:
File removed
21.A file called empcodes contains a list of all Employee codes sorted in numeric order. Another file
called empinc contains a list of Employee Codes of all employees who are to receive incentives in the
current month. This file is sorted code-wise. How would you display a list of Employee codes of all
employees who are not receiving incentive in that month?
COMMAND:
Comm. -23 empcodes empinc
OUTPUT:
LIST OF EMPLOYEES DISPLAYED
22. Give a command to compare the file category with the Standard input.
COMMAND:
cmp category
OUTPUT:
comparission
23. Give a command to compare the file fruits.txt with the standard input and display the lines
common to both.
COMMAND:
cmp –fruits.txt
OUTPUT:
Common files displayed
24. Display the lines which are unique from the file trans.dat
COMMAND:
Sort trans.dat | uniq –u
OUTPUT:
File compared
COMMAND:
sort trans.dat | uniq -d
OUTPUT:
Duplicate lines
26. Display the count of each line from the file trans.dat
COMMAND:
Sort trans.dat | uniq –c
OUTPUT: 5
EX NO: 11 WORKING WITH FILTERS- grep
DATE: 21.09.2023
~ is the field separator, insert sample records of your own, and perform the following
operations on it.
a. Give the command that would print records of all employees whose Last name begins
with S.
b. Give the command that would print out the records of all employees whose basic pay is
between Rs 15000 and Rs 20000
c. Give the command that would print out the records of all employees born in 1985.
d. Give the command to get the records of all employees in Grade S1 and S2.
e. Give the command that would print out the records of all employees in Accounts
department and in Grade E.
f. Give the command that would print out the records of all employees in RND department
with three years of experience.
g. Give command that would print out the records of all employees whose employee code is
between 3010 and 3059
h. Give the command that will display the records of all employees from Chennai.
j. Give the command that will store the records of all employees from Bombay in a file
called Bombaydata.
EX NO: 12 WORKING WITH FILTERS- sort
DATE: 26.09.2023
a. Give the command to display the records of all employees born in the year 1980 in sequence
of employee code from emp.dat.
b. Give the command to display the records of all employees in the department RND with 3 to 7
years of experience in sequence of years of experience.
c. Give command to display records of all employees who are not in the department of MKT,
sorted by department code within which by Grade.
d. Give command to sort the file trans.dat by customer code and display the records for Product
numbers P01 to P09.
e. Give the command to sort and display the file trans.dat by Product number
f. Give the command to display the number of transactions for the Product number P02.