108 Unix
108 Unix
Tutorial – 1
4. Display date and time in format shown in brackets ( Sun June 19,11:40 PM )
[06/09/2022 - 10:25.41] ~/desktop/unix/demo
[Kevin.DESKTOP-5I719O9]$ date +"%a %B %d , %I : %M%p"
Tue September 06 , 10 : 26AM
6. Execute a command to know the kernel version of operating system you are working on.
[06/09/2022 - 10:26.31] ~/desktop/unix/demo
[Kevin.DESKTOP-5I719O9]$ uname -r 1.7.5(0.225/5/3)
1
TYBCA SEM-5 Div-4 Unix Roll No – 108
2
TYBCA SEM-5 Div-4 Unix Roll No – 108
12. display all files from current directory having first and last character as number.
[06/09/2022 - 10:31.41]
~/desktop/unix/demo [Kevin.DESKTOP-
5I719O9]$ touch 1f0 1sp2
[06/09/2022 - 10:32.07]
~/desktop/unix/demo [Kevin.DESKTOP-
5I719O9]$ ls [0-9]*[0-9]
1f0 1sp2
13. display the list of all file names that contains only 3 latters.
[06/09/2022 - 10:32.19]
~/desktop/unix/demo[Kevin.DESKTOP-
5I719O9]$ ls ???
1f0
17. copy those files that must contains 3rd character in the file name as digit to the directory "xtemp".
[06/09/2022 - 10:34.28]
~/desktop/unix/demo
[Kevin.DESKTOP5I719O9]$ mkdir
xtemp
[06/09/2022 - 10:35.31]
~/desktop/unix/demo/xtemp [Kevin.DESKTOP-
5I719O9]$ ls
1f0 as1
3
TYBCA SEM-5 Div-4 Unix Roll No – 108
19. copy the "helix" directory to "tmp" directory.
[06/09/2022 - 10:36.07] ~/desktop/unix/demo/xtemp
[Kevin.DESKTOP-5I719O9]$ cp -R helix tmp
4
TYBCA SEM-5 Div-4 Unix Roll No – 108
Tutorial – 2
1. list the content of currrent directory having file names as number.
[28/08/2022 - 14:42.25] ~/desktop/unix3
[Kevin.DESKTOP-5I719O9] $ ls | grep -E '^[0-
9]+$'123
3. remove all files containing digit as the 2nd latter as their names.
[28/08/2022 - 14:59.40] ~/desktop/unix3
[Kevin.DESKTOP-5I719O9] $ rm ?[0-9]*
rm: d1: is a directory
5
TYBCA SEM-5 Div-4 Unix Roll No – 108
7. compare 2 files named sc1, sc2 and store the common content in file result.
[28/08/2022 - 15:33.50] [29/08/2022 - 15:33.50] ~/desktop/unix3
[Kevin.DESKTOP-7GLGQEA] $ cat > sc1 abc def ghi
8. find how many number of lines from sc1 and sc2 are common.
[28/08/2022 - 15:42.27] ~/desktop/unix3
[Kevin.DESKTOP-5I719O9] $ comm -1 -2 sc1 sc2 |
wc -l2
9. display only those files containing the more then 5 character as there names ( the file names having last two characters as digit).
[28/08/2022 - 15:45.41] ~/desktop/unix3
[Kevin.DESKTOP-5I719O9] $ cat > abc12i am abc
10. create the directory named "maxx" and copy all files having only capital letters.
[29/08/2022 - 14:14.09] ~/desktop/unix3
[Kevin.DESKTOP-5I719O9] $ cp `ls [A-Z]` max
6
TYBCA SEM-5 Div-4 Unix Roll No – 108
13. display only the last accessed file from current directory.
[29/08/2022 - 13:58.49] ~/desktop/unix3
[Kevin.DESKTOP-5I719O9] $ ls -t|
head -n1vbe
14. create file named emp_list having empid and date of joining.
[29/08/2022 - 14:09.27] ~/desktop/unix3
[Kevin.DESKTOP-5I719O9] $ cut -d"|" -f1,4 emp > emp_list
18. display the path of the directory where all your mail are stored.
[29/08/2022 - 14:15.02] ~/desktop/unix3
[Kevin.DESKTOP-5I719O9] $ echo $MAIL
7
TYBCA SEM-5 Div-4 Unix Roll No – 108
structureexport
|-- color
|-- dir1
|-- file1.lst
|-- m1
| |-- f1
| | `-- emp.lst
| `-- f2
|
`-- menu1.lst
|-- m2
| |-- d1
| `-- file123
`-- m3
|-- abc.php
|-- dir1
| `-- tmp
`-- xyz.java
$ mkdir export
$ cd m1
$ mkdir f1 f2
$ cd f1
$ cd..
$ cd..
8
TYBCA SEM-5 Div-4 Unix Roll No – 108
$ cd m2
$ cd ..
$ cd dir2
$ ls
DFD UNIX asd[0-9]* c2 d1 emp export m2 psp01 sc2 xyz.java
HJKU abc c- c3 dir1 emp* fil1.txt m3 result sps.php
M abc12 c1 color dir2 emp_list m1 maxx sc1 vbe
20. copy file emp.lst to tmp directory ( in m3 dir ) using relative path.
[29/08/2022 - 15:37.46] ~/desktop/unix3
[Kevin.DESKTOP-5I719O9] $ cp m1/f1/emp.txt
m3/f1
23. assign write permission only for group members and the rest permissions remain intact for the directory "d1".
[29/08/2022 - 15:54.10] ~/desktop/unix3
[Kevin.DESKTOP-5I719O9] $ chmod 424 d1
9
TYBCA SEM-5 Div-4 Unix Roll No – 108
24. copy the content of file emp.lst to file123, make back up of file 'file123' and rename it with file_bkp
[29/08/2022 - 15:55.45] ~/desktop/unix3/m2
[Kevin.DESKTOP-5I719O9] $ cp file123
file_bkp
29. provide the permissions to the file "color" in such a way that only the owner can perform read and write operation while
groupmembers and others can only execute a file.
[29/08/2022 - 16:22.43] ~/desktop/unix3
[Kevin.DESKTOP-5I719O9] $ chmod 611
color
10
TYBCA SEM-5 Div-4 Unix Roll No – 108
Tutorial – 3
1. write a command to display content of top 3 largest file in a
workingdirectory.
[29/08/2022 - 14:29.19] ~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ ls -s | head -
n2
0 f1
0 f2
7. Replace 'hello' with “HELLO” in input file finish and write those lines to output file fout.sh
[29/08/2022 - 14:44.51] ~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ cat f2
HELLO
8. extract all username and their home directory form /etc/passwd file.
[29/08/2022 - 14:44.58] ~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ cat /etc/passwd | cut -d ':' -f 1,6
SYSTEM:LocalService:NetworkService:
Administrators:Administrator:/home/Administrator DefaultAccount:/home/DefaultAccountGuest:/home/Guest
Hasti:/home/Hasti WDAGUtilityAccount:/home/WDAGUtilityAccount
9. Locate lines of file where the second and second last character of the lines are same.
[29/08/2022 - 14:50.08] ~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ grep
'^.\(.\).*\1.$' f2
Unix shell programming
11
TYBCA SEM-5 Div-4 Unix Roll No – 108
10. Display all lines of files that contains “hello” pattern in it.
[29/08/2022 - 14:50.12] ~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ grep 'hello' `grep -l
'hello' *`f1:hello
f3:hello
13. List all files of working directory having at least 4 characters in filename.
[29/08/2022 - 14:59.57] ~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ find . -maxdepth 1 -type f -name '????*' -print
18. Write a command to locate lines that begin and end with (.).
[31/08/2022 - 12:09.46] ~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ grep '^\..*\.$' f2
20. Write a command to replace all occurrences of “he” with “she” and “hello” with “hi” in file f1.
[31/08/2022 - 12:17.15] ~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ sed 's/he/she/' -e 's/hello/hii/' f1
sed: s/he/she/: No such file or directory
Hi
Hi
[31/08/2022 - 12:18.05] ~/desktop/unix/pract
12
TYBCA SEM-5 Div-4 Unix Roll No – 108
[Kevin.DESKTOP-5I719O9]$ sed
's/he/she/' f2Hii
HELLOunix is subjectshe is very anoying this is tshe life
unix sshell programmingunix is open source Good night
bin
display
bin
display
21. Display those lines having exactly 10 alphabates from file f1.
[31/08/2022 - 12:18.36] ~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ grep '^[A-Za-z]
\{10\}$' f2
22. Copy file content of f1 to file f2 if f1 exist otherwise write error message to file f2.
[31/08/2022 - 12:19.48] ~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ cp f1 f2
23. Search those files from current directory which have more than 5 links.
[31/08/2022 - 12:23.30] ~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ find . -links +5 -
print
25. Replace all occurrences of “linux OS” with “unix OS” in file f1.
[31/08/2022 - 12:26.04] ~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ sed 's/linux os/unix
os/g' f3kevin
hello
good
morning
byyyy
HELLOunix is subjectshe is very anoying this is tshe life
unix sshell programmingunix is open source Good night
bin
display
bin
display
27. Display name of all files of working directory having pattern “The”.
[31/08/2022 - 12:27.00] ~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ grep -l 'The' *
28. Display lines of file f1 that begin with any capital letter.
[31/08/2022 - 12:35.32] ~/desktop/unix/pract
13
TYBCA SEM-5 Div-4 Unix Roll No – 108
29. Write a sed command to extract first word of each line. Assuming that there is no white space character at beginning of line.
[31/08/2022 - 12:35.55] ~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ sed 's/ .*/ /g' f3
kevin
hello good byyyy
HELLOunix
she
this unix unix Good
bin bin
31. display only those lines of file f1 having length in between 30 to 50 characters.
[31/08/2022 - 12:39.07] ~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ grep
'^.\{10,20\}$' f3Gujan saxena
good morning HELLOunix is subjectshe is very
anoying this is tshe life
unix is open
sourceGood
night
bin
display
bin
display
33. Replace all occurrences of “hello” with “hi” and “he” with “she”.
[31/08/2022 - 12:17.15] ~/desktop/unix/pract
[KEVIN.DESKTOP-5I719O9]$ sed 's/he/she/' -e
's/hello/hii/' f1sed: s/he/she/: No such file or directory /
hi HI
[31/08/2022 - 12:18.05]
~/desktop/unix/pract [KEVIN.DESKTOP-
5I719O9]$ sed 's/he/she/' f2Hii
HELLOunix is subjectshe is very anoying this is tshe life
unix sshell programmingunix is open source Good night
bin
display
bin
display
14
TYBCA SEM-5 Div-4 Unix Roll No – 108
34. Count number of words and lines of files whose filename begins with x.
[31/08/2022 - 12:40.29] ~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ find . -maxdepth 1 -type f -name 't*' -exec wc -wl {} \;
15
TYBCA SEM-5 Div-4 Unix Roll No – 108
Tutorial – 4
4. Write a shell script to accept a string and reverse it without using rev() function, apply proper validation
vi reverse.sh
echo -n "Enter a String: "
read str k="a"
if test -z "$str"
then
echo "String is Null"
else
len=${#str}
printf "reverse string is:"
while [ $len -gt 0 ]
do
echo -n "`echo $str | cut -c $len`"
len=`expr $len - 1`
done
echo
fi
~
~
[10/09/2022 - 22:41.53] ~/desktop/unix/kevin
[Kevin.DESKTOP-7GLGQEA] $ sh reverse.sh
Enter a String:kevin
reverse string is:nivek
5.Write a shell script to find a file with maximum size in the current directory, also print the no of words, characters, and
lines along with the content of file
fname=`ls -lS | tr -s " " | grep '^-' | cut -d ' ' -f 9 | head -n 1`
echo "File with max size is : $fname"
echo "this is Contents of $fname : "
echo " *contents start* "
echo "============================================"
echo
cat $fname
echo "============================================"
echo " *contents end* "
echo
echo
echo "Total no of lines in $fname = `wc -l < $fname`"
echo "Total no of words in $fname = `wc -w < $fname`"
File with max size is : script.sh
this is Contents of script.sh :
//contents start
fname=`ls -lS | tr -s " " | grep '^-' | cut -d ' ' -f 9 | head -n 1`
echo "File with max size is : $fname"
echo "this is Contents of $fname : "
echo " *contents start* "
echo "============================================"
echo
16
TYBCA SEM-5 Div-4 Unix Roll No – 108
cat $fname
echo "============================================"
echo " *contents end* "
echo
echo
echo "Total no of lines in $fname = `wc -l < $fname`"
echo "Total no of words in $fname = `wc -w < $fname`"
echo "Total no of characters in $fname = `wc -c < $fname`"
============================================
*contents end*
Total no of lines in script.sh = 16
Total no of words in script.sh = 94
Total no of characters in script.sh = 561
6.Write a shell script which works like a calculator(enter the values via command line argument)
a=$1
b=$2
echo "number one is: $a"
echo "number two is: $b"
echo "Enter Choice :"
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
read ch
Result : 10 – 20
-10
17
TYBCA SEM-5 Div-4 Unix Roll No – 108
7 Write a shell script which checks whether an entered user is valid or not.
Q-8) Write an awk script to print the fields 2,3,4 and 6 from test.txt file which contains the pattern ‘unix’
Q-9) Write an awk script that displays first two-words of each line of the file f1
18
TYBCA SEM-5 Div-4 Unix Roll No – 108
Q-10) Write an awk script which prints file contentsbottom to top
OR
Write an awk script which print frequency of eachword in the input file
Note: Suggestions are invited
‘{
line[NR]=$0
}
END{
for(i=NR;i>=1;i--)
print line[i]
}’ f6
swap
parth
vishv
akshit
19
TYBCA SEM-5 Div-4 Unix Roll No – 108
20