0% found this document useful (0 votes)
12 views

Unix

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Unix

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO -330

Assignment-1
1. Display the content of current directory.
[06/09/2024 - 10:23.01]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ ls -l

2. Show calender of january 1980.


[06/09/2024 - 10:25.08]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ cal 1 1980
January 1980
Su Mo Tu We Th Fr Sa
12 345
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26 27
28 29 30 31

3. show the current working directory.


[06/09/2024 - 10:25.19]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ pwd
/drives/C/Users/Surali/Desktop/unix/t1

4. Display date and time in format shown in brackets ( Sun June 19,11:40 PM )
[06/09/2024 - 10:25.41]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ date +("%a %B %d , %I : %M%p") (Fri
September 15 , 10 : 26AM)

5. Display the below pattern with echo command


\
\\\
\\\\\
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

[06/09/2024 - 10:26.06]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ echo -e " \\ \n \\ \\ \\ \n \\ \\ \\ \\ \\"
\
\\\
\\\\\

6. Execute a command to know the kernel version of operating system you are working on.
[06/09/2024 - 10:26.31]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ uname -r
3.3.5(0.341/5/3)

7. Execute a command to know your terminal.


[06/09/2024 - 10:26.56]~/desktop/unix/demo
[Surali.DESKTOP5I719O9]$ tty /dev/pty0

8. Execute a command to identify all executables in current working directory.


[06/09/2024 - 10:27.16]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ cat > surali.exe how are
you ? good or not ?

[06/09/2024 - 10:28.11]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ cat surali.exe how are
you ? good or not ?

[06/09/2024 - 10:28.21]~/desktop/unix/demo [Surali.DESKTOP-


5I719O9]$ ls *.exe surali.exe

9. Display a sorted list of files by last access time.


[06/09/2024 - 10:28.33]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ ls -ltu total
1
-rwxr-xr-x 1 surali Domain U 28 Sep 6 10:28 surali.exe drwxr-xr-x 1 surali
Domain U 29 Sep 6 10:23 d1

2
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

10. create five files named f1 to f5.


[06/09/2024 - 10:28.51]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ cat > f1 hye

[06/09/2024 - 10:29.31]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ cat >f2 hyyyyyy

[06/09/2024 - 10:29.41]~/desktop/unix/demo [Surali.DESKTOP-5I719O9]$ cat


>f3 hello [06/09/2024 - 10:29.48]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ cat >f4 good morning

[06/09/2024 - 10:30.06]~/desktop/unix/demo [Surali.DESKTOP-


5I719O9]$ cat >f5 apple

[06/09/2024 - 10:30.17]~/desktop/unix/demo [Surali.DESKTOP-


5I719O9]$ ls d1f1 f2 f3
f4 f5 surali.exe

11. copy the content of f1 and f2 into f3.


[06/09/2024 - 10:30.30]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ cat f1 f2>f3

[06/09/2024 - 10:31.32]~/desktop/unix/demo [Surali.DESKTOP-


5I719O9]$ cat f3 hye hyyyyyy

12. display all files from current directory having first and last character as number.
[06/09/2024 - 10:31.41]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ touch 1f0 1sp2

[06/09/2024 - 10:32.07]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ ls [0-9]*[0-9] 1f0
1sp2

13. display the list of all file names that contains only 3 latters.
[06/09/2024 - 10:32.19]~/desktop/unix/demo [surali.DESKTOP-5I719O9]$ ls ???
1f0
3
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

14. create a file named "-abc" in current working directory.


[ 09/09/2024 - 10:32.40]~/desktop/unix/demo

[Surali.DESKTOP-7GLGQEA]$ cat > abc


hii hello Surali

15count the number of characters of file "-abc".


[06/09/2024 - 10:33.10]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ wc -c -- -abc 16 -
abc

16. rename file "ex1" to "as1".


[06/09/2024 - 10:34.09]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ mv ex1 as1

17. copy those files that must contains 3rd character in the file name as digit to the directory
"xtemp".
[06/09/2024 -
10:34.28]~/desktop/unix/demo
[Surali.DESKTOP5I719O9]$ mkdir xtemp

[06/09/2024 - 10:35.13]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ cp ??[0-9]* ./xtemp

[06/09/2024 - 10:35.21]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ cd xtemp

[06/09/2024 - 10:35.31]~/desktop/unix/demo/xtemp
[Surali.DESKTOP-5I719O9]$ ls 1f0
as1

18. create directory named "helix", "apache".


[12/08/2024 - 12:17.57]/drives/d/tybca316/unix/d1/xtemp
[Surali.DESKTOP-5I719O9]$ mkdir helix apache
4
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

19. copy the "helix" directory to "tmp" directory.


[06/09/2024 - 10:36.07]~/desktop/unix/demo/xtemp
[Surali.DESKTOP-5I719O9]$ cp -R helix tmp

20. move those files having last character as digit to the"apache".


[06/09/2024 - 10:36.07]~/desktop/unix/demo/xtemp
[Surali.DESKTOP-5I719O9]$ cp -R helix tmp

[06/09/2024 - 10:37.00]~/desktop/unix/demo/xtemp
[Surali.DESKTOP-5I719O9]$ cd apache

[06/09/2024 - 10:37.20]~/desktop/unix/demo/xtemp
[Surali.DESKTOP-5I719O9]$ ls 1f0
as1

21. remove file "-abc".


[06/09/2024 - 10:37.49]~/desktop/unix/demo/xtemp
[Surali.DESKTOP-5I719O9]$ rm -- -abc

5
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

Assignment – 2

1. list the content of currrent directory having file names as number.


[28/08/2024 - 14:42.25]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ ls | grep -E '^[0-9]+$' 123

2. display the filename containing only alphabats as a names.


[28/08/2024 - 14:56.18]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ ls | grep -E '^[a-z A-Z]+$' abc

3. remove all files containing digit as the 2nd latter as their names. [28/08/2024 -
14:59.40]~/desktop/unix3 [Surali.DESKTOP-5I719O9]$ rm ?[0-9]* rm: d1: is a directory

[28/08/2024 - 15:02.22]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ ls M
abc d1

4. create the file named " asd[0-9]".


[28/08/2024 - 15:32.31]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ cat>asd\[0-9] \* hello
everyone

5. copy the content of file c- to c1,c2,c3.


[28/08/2024 - 15:06.33]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ cat > c- the moon

[28/08/2024 - 15:09.26]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ cat > c1

[28/08/2024 - 15:10.06]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ cat > c2

[28/08/2024 - 15:10.12]~/desktop/unix3 [Surali.DESKTOP-5I719O9]$


cat > c3

6
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

[28/08/2024 - 15:10.18]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ cat c1 c2 c3> c-

[28/08/2024 - 15:17.21]~/desktop/unix3 [Surali.DESKTOP-5I719O9]$


cat c1 the moon

6. display the date in the following format .e.g. "Today's date is : Sat Jul 30 15:25:31 IST 2011"
[28/08/2024 - 15:25.01]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ echo "Today's date is`date`"

Today's date is Sun Aug 28 15:28:23 IST 2024


7. compare 2 files named sc1, sc2 and store the common content in file result.
[29/08/2024 - 15:33.50]~/desktop/unix3
[Surali.DESKTOP-7GLGQEA]$ cat > sc1 abc
def ghi

[29/08/2024 - 15:39.13]~/desktop/unix3
[Surali.DESKTOP-7GLGQEA]$ cat > sc2
abc def jkl

[29/08/2024 - 15:39.28]~/desktop/unix3
[Surali.DESKTOP-7GLGQEA]$ comm -1 -2 sc1 sc2>result

[29/08/2024 - 15:42.17]~/desktop/unix3
[Surali.DESKTOP-7GLGQEA]$ cat result
abc def

8. find how many number of lines from sc1 and sc2 are common.

[28/08/2024 - 15:42.27]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ comm -1 -2 sc1 sc2 |wc -l 2

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/2024 - 15:45.41]~/desktop/unix3
[Surali.DESKTOP5I719O9]$ cat > abc12 i am abc

[28/08/2024 - 15:48.48]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ cat > psp01 i am psp

7
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

[28/08/2024 - 15:49.44]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ ls ???[0-9][0-9] abc12 psp01

10. create the directory named "maxx" and copy all files having only capital letters.
[29/08/2024 - 14:14.09]~/desktop/unix3 [Surali.DESKTOP-5I719O9]$
cp `ls [A-Z]` max

11. create a file named emp*


[28/08/2024 - 21:41.34]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ cat > emp\*

[28/08/2024 - 21:43.16]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ ls
M abc12 c- c2 d1 psp01 sc1 abc asd[0-9]* c1 c3 emp* result
sc2

12. make a list of employee in following order ( use vieditor ).


empid empname post
1 abcd programmer
2 xyz manager
[29/08/2024 - 13:50.45]~/desktop/unix3 [Surali.DESKTOP-5I719O9]$ cat
emp empid | empname | post 1| awer
| manager
2| sdfg | employee
3| rtyy | director 4|
jbyg | programmer

13. display only the last accessed file from current directory.
[29/08/2024 - 13:58.49]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ ls -t |head
-n1 vbe

14. create file named emp_list having empid and date of joining.

8
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

[29/08/2024 - 14:09.27]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ cut -d"|" -f1,4 emp > emp_list

[29/08/2024 - 14:10.01]~/desktop/unix3 [Surali.DESKTOP-5I719O9]$ cat emp_list


empid
1234

15. copy the content of file "emp*" in emp_master1,emp_master2


[29/08/2024 - 14:10.27]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ cp /emp* emp_master1

[29/08/2024 - 14:10.27]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ cp /emp* emp_master2

16. rename file emp_master2 to backup_emp


[29/08/2024 - 14:12.23]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ mv emp_master2 backup_emp

17. remove file "emp*"


[29/08/2024 - 14:13.13]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ rm /emp*

18. display the path of the directory where all your mail are stored. [29/08/2024 -
14:15.02]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ echo $MAIL

19. create the followingstructure


Export
|-- color
|-- dir1
|-- file1.lst
|-- m1
| |-- f1
| | `-- emp.lst |
`-- f2
|
`-- menu1.lst
|-- m2
| |-- d1 | `-- file123
`-- m3
9
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

|-- abc.php
|-- dir1
| `-- tmp `--
xyz.java

$ mkdir export
$ mkdir color dir1 m1 m2 m3 dir2
$ cat > fil1.txt
Hello Everyone
$ cd m1
$ mkdir f1 f2
$ cd f1
$ cat > emp.txt Hye
Good Morning
$ cd..
$ cd..
$ cat > menu.txt
Hellloooo $cdm2
$ cd m2 $ cat >
file123 I’m here $
cat > file123 Hello
unix
$ cd
..
$ cat > sps.php Hello php

$ cd dir2

$ cat > tmp


Heyyyyyyyy
$ cat > xyz.java Good Night
$ ls
DFD UNIX asd[0-9]* c2 d1 emp export m2 psp01 sc2 xyz.java abc c- c3
dir1 emp* fil1.txt m3 result sps.php M abc12 c1 color dir2 emp_list
m1 maxx sc1

20. copy the content of file emp.lst to file123, make back up of file 'file123' and rename it with
file_bkp

10
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

[29/08/2024 - 15:55.45]~/desktop/unix3/m2 [Surali.DESKTOP-


5I719O9]$ cp file123 file_bkp

21. display the content of "export" directory in a way like question-19 [29/08/2024 -
15:55.57]~/desktop/unix3/m2 [Surali.DESKTOP-7GLGQEA]$ ls
-1d */ -x $ color/:
$ dir1/: tmp $ dir2/: $
m1/:F1F2 $ m2/:
File_bkp $m3/: F1

22. display last modified file.


[29/08/2024 - 16:01.11]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ ls -1t | head -n1 m2

23. make archive file of all the files having .lstextension


[29/08/2024 - 16:19.25]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ find . -name "*.lst"

24. move all .lst files to tmp directory


[29/08/2024 - 16:20.17]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ mv '*.lst' /dir2/tmp

25. provide the permissions to the file "color" in such a way that only the owner can perform
read and write operation whilegroup members and others can only execute a file.
[29/08/2024 - 16:22.43]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ chmod 611 color

26. change the ownership of file "tmp" to root


$ $sudo
Pass:#chown hasti root tmp
#exit
$

Assignment-3

11
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

1. write a command to display content of top 3 largest file in aworking directory.


[29/08/2024 - 14:29.19]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ ls -s | head -n2
0 f1
0 f2

2. Count no. of words in lines 1 through 2 of file f1.txt.


[29/08/2024 - 14:33.17]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ sed -n '1,2p' f1 | wc -w 2

3. Display all filenames not beginning with “.”.


[29/08/2024 - 14:35.57]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ ls |grep ^[^.] f1
f2

4. delete all special characters from file f1.


[29/08/2024 - 14:36.09]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ tr -cd '[a-zA-Z0-9\n]' <f1 Hii Hello

5. Display i-node no of all files of current directory.


[29/08/2024 - 14:38.43]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ ls i
9851624184977260 f1 19984723346472289 f2 16888498602744989 f3 18295873486298472 f4

6. Display those lines of file f1 that contains exactly 20 characters in it.


[29/08/2024 - 14:40.20]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ grep -E'^.{20}' f2 unix shell
programming

7. Replace 'hello' with “HELLO” in input file finish and write those lines to output file fout.sh
[29/08/2024 - 14:44.51]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ cat f2 HELLO

8. extract all username and their home directory form /etc/passwd file.
[29/08/2024 - 14:44.58]~/desktop/unix/pract

12
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

[Surali.DESKTOP-5I719O9]$ cat /etc/passwd | cut -d ':' -f 1,6


SYSTEM:LocalService:NetworkService:
Administrators:Administrator:/home/Administrator DefaultAccount:/home/DefaultAccount Guest:
/home/GuestSurali:/home/Surali WDAGUtilityAccount:/home/WDAGUtilityAccount

9. Locate lines of file where the second and second last character of the lines are same.
[29/08/2024 - 14:50.08]~/desktop/unix/pract [Surali.DESKTOP-5I719O9]$ grep'^.\(.\).*\1.$' f2
Unix shell programming

10. Display all lines of files that contains “hello” pattern in it.
[29/08/2024 - 14:50.12]~/desktop/unix/pract

[Surali.DESKTOP-5I719O9]$ grep 'hello' `grep -l'hello' *` f1:hello f3:hello

11. Display all lines having “g*” patternin it. [29/08/2024 - 14:53.09]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ grep 'g\*' f3

12. Change modification time of file to Dec 25, 10:30AM.


[29/08/2024 - 14:54.47]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ touch -t 08261957f1

13. List all files of working directory having at least 4 characters in filename. [29/08/2024 -
14:59.57]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ find . -maxdepth 1 -type f -name '????*' -print

14. Execute a command to run a script hello.sh at tea time.


[29/08/2024 - 14:59.57]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ echo “./hello.sh” | at teatime

15. Replace multiple spaces with a single space in file f1.

[29/08/2024 - 15:23.48]~/desktop/unix/pract
[Surali.DESKTOP-7GLGQEA]$ tr -s ' ' < f1 hii hello

13
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

16. Write a unix command to evalute an expression : 4*3.14+6 [29/08/2024 -


15:24.15]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ awk 'BEGIN {print 4*3.14+6}'18.56

17. write a command to display all unique words offile f1.


[31/08/2024 - 12:09.29]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ tr ' ' '\n' <f1 | sort | uniq -u
Hello
Hii

18. Write a command to locate lines that begin and end with (.).
[31/08/2024 - 12:09.46]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ grep '^\..*\.$' f2

19. write a command to display all lines that contains 2 or more ^symbol at beginning of line.
[31/08/2024 - 12:12.24]~/desktop/unix/pract [Surali.DESKTOP-
5I719O9]$ grep -E'^\^{2,}' f1

20. Write a command to replace all occurrences of “he” with “she” and “hello” with “hi” in file
f1.
[31/08/2024 - 12:17.15]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ sed 's/he/she/' -e 's/hello/hii/' f1 sed:
s/he/she/: No such file or directory Hi

Hi
[31/08/2024 - 12:18.05]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ sed 's/he/she/' f2
Hii
HELLO
14
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

unix is subject she is very anoying this is tshe lifeunix sshell programming unix is open source Good
night bin display bin display

21. Display those lines having exactly 10 alphabates from file f1.
[31/08/2024 - 12:18.36]~/desktop/unix/pract
[Surali.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/2024 - 12:19.48]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ cp f1 f2

23. Search those files from current directory which have more than 5 links.
[31/08/2024 - 12:23.30]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ find . -links +5 – print

24. Display lines of file f1 that do not contain digitin it. [31/08/2024 -
12:23.33]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ grep -v '[0-9]' f2 hi hello

25. Replace all occurrences of “linux OS” with “unix OS” in file f1.
[31/08/2024 - 12:26.04]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ sed 's/linux os/unixos/g' f3 surali
hello good morning hyyyy
HELLO unix is subject she is very anoying this is tshe life unix
sshell programming unix is open source Good night bin display
bin display

26. Display all line of file f1 having 3rd word as'user'. [31/08/2024
- 12:36.23]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ grep '^[^ ]* [^ ]* user' f2

27. Display name of all files of working directory having pattern “The”.
[31/08/2024 - 12:27.00]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ grep -l 'The' *

15
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

28. Display lines of file f1 that begin with any capital letter. [31/08/2024 -
12:35.32]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ grep '^[A-Z]' f3 surali HELLO unix is
subject Good night

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/2024 - 12:35.55]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ sed 's/ .*/ /g' f3 Kevin hello
good byyyy HELLO unix
She this unix unix
Good bin bin

30. What does the following command do?grep f1 f2 f3 It’s return nothing

31. display only those lines of file f1 having length in between 30 to 50 characters.
[31/08/2024 - 12:39.07]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ grep'^.\{10,20\}$' f3
Surali Devani good morning
HELLO unix
is subject she is very smart this is
she life unix is opensource
Goodnight
Bin display
Bin display

32. Display binary value of 12 using bc


[31/08/2024 - 12:39.07]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ echo “obase=2; 12” | bc

33. Replace all occurrences of “hello” with “hi” and “he” with “she”.
[31/08/2024 - 12:17.15]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ sed 's/he/she/' -e's/hello/hii/' f1 sed:
s/he/she/: No such file or directory /hi HI [31/08/2024
- 12:18.05]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ sed 's/he/she/' f2 Hii

16
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

HELLO unix is subject she is very anoying this is tshe


lifeunix sshell programming unix is open source Good
night bin display bin display

34. Count number of words and lines of files whose filename begins
with x.
[31/08/2024 - 12:40.29]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ cat `ls x*` | wc -lw

35. Write equivalent sed command of “sed '1,5d' f1”. [31/08/2024


- 12:41.19]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ sed -n '1,5!p' f3 HELLOunix
is subjectshe is very anoying this is tshe lifeunix sshell
programming unix is open source Good night bin display
bin display

36. Write equivalent IRE for the following regular expression - A*


- A?
[31/08/2024 - 12:42.35]~/desktop/unix/pract [Surali.DESKTOP-
5I719O9]$ grep 'A\{1,\}' f3

Assignment-4
1. Write a shell script which works like a calculator(enter the values via command line
argument)

Shell Script:
echo -e "enter num1: \c" read num1
echo -e "enter num2: \c" read
num2 echo -e "\t\t menu \n
1. Addition
2. Multiplication
3. subtraction 4. divison
enter your choice: \c" read ch case $ch in 1|+)
ans=`expr $num1 + $num2` ;; 2|*)
ans=`expr $num1 \* $num2` ;; 3|-)
ans=`expr $num1 - $num2` ;;
4|/) ans=`expr $num1 / $num2` ;;
*) echo "invalid option" ;; esac echo
"answer: $ans"

17
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

OUTPUT:
$ sh textfile1.txt enter
num1: 1 enter
num2: 4 menu

1. Addition
2. Multiplication
3. subtraction 4. divison enter your choice: 1 answer:5
2. 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.

Shell Script:
# Find the file with the maximum size in the current directory max_file=$(find .
-type f -exec ls -lS {} + | head -n 1 | awk '{print $9}')

# Check if a file was found if


[[ -z "$max_file" ]]; then echo "No files found in
the current directory."
exit 1 fi
# Print the name of the largest file echo
"The largest file is: $max_file"
# Use wc command to get number of lines, words, and characters line_count=$(wc
-l < "$max_file") word_count=$(wc -w <
"$max_file") char_count=$(wc -m <
"$max_file")

# Print the file statistics echo "Number of lines:


$line_count" echo "Number of words:
$word_count" echo "Number of characters:
$char_count"

# Print the content of the file echo


"Content of the file:" cat
"$max_file"

OUTPUT:
The largest file is:test.txt
Number of lines:6
Number of words:25
Number of characters:120 Content of the
file:
hyy unix hello unix hiii unix hyy
hyy hyy hyy hyy hyy hyy hyy unix
18
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

php wd linux wd java ospart


java nt
surali bharodiya

3. Write a shell script to display message “good morning” or “good afternoon” or “good evening”, whenever
you login.

Shell Script:
# Get the current hour
hour=$(date +"%H")

# Determine the appropriate greeting based on the hour if


[ "$hour" -lt 12 ]; then echo "Good Morning!" elif
[ "$hour" -lt 18 ]; then echo
"Good Afternoon!" else
echo "Good Evening!"
fi
OUTPUT:
$ sh login.sh
Good Morning!

19
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

4. Write a shell script to copy one file into other, display appropriate message if Destination file already exist.

Shell Script:
#check if the correct number of arguments is provided if
[ "$#" -ne 2 ]; then echo "Usage: $0 <source_file>
<destination_file>"
exit 1 fi

# Assign arguments to variables source_file="$1"


destination_file="$2"

# Check if the source file exists if [ ! -f "$source_file" ]; then


echo "Error: Source file '$source_file' does not exist."
exit 1 fi
# Check if the destination file already exists if [ -f
"$destination_file" ]; then echo "Error: Destination file
'$destination_file' already exists."
else
# Copy the source file to the destination
cp "$source_file" "$destination_file" echo "File '$source_file' has been copied to
'$destination_file'."
fi

OUTPUT:
$ sh copy.sh file1 file3
File 'file1' has been copied to 'file3'.

5. Write a shell script to accept a string and reverse it.

20
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

Shell Script: if [ "$#" -ne 1


]; then echo
"Usage: $0 <string>"
exit 1 fi
# Accept the input string input_string="$1"
reversed_string=""

# Get the length of the input string


length=${#input_string}

# Loop through the string in reverse order for


(( i=$length-1; i>=0; i-- )); do
reversed_string+=${input_string:i:1}
done

# Print the original and reversed strings echo


"Original String: $input_string" echo
"Reversed String: $reversed_string"

OUTPUT:
$ sh reverse.sh surali
Original String: surali
Reversed String: arahd

6. Write a shell script to input a word in lower case and convert it into upper case.

Shell Script:
# Read input word read -p "Enter a word in
lowercase: " word

# Convert the word to uppercase using tr


uppercase=$(echo "$word" | tr '[:lower:]' '[:upper:]')

# Display the result


echo "Uppercase word: $uppercase"

OUTPUT:
$ sh convert.sh
Enter a word in lowercase: surali Uppercase word:
SURALI

21
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

7. Write an awk script to print the fields 2,3,4 and 6 from test.txt file which contains the pattern ‘unix’.

OUTPUT:
$ awk '/unix/ { print $2, $3, $4, $6 }' test.txt unix hello unix
unix

8. Write an awk script that displays first two-words of each line of the file f1.

OUTPUT:
$ awk '{ print $1, $2 }' test.txt
hyy unix hyy hyy
unix php linux wd
ospart java
surali bharodiya

22
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

9. Write an awk script which prints file contents bottom to top.

AWK Script:
$ awk '{
# Store each line in an array lines[NR]
= $0
}
END {
# Print lines in reverse order
for (i = NR; i > 0; i--) { print
lines[i] } }' test.txt

OUTPUT:
surali bharodiya ospart java nt linux
wd java unix php wd hyy
hyy hyy hyy hyy hyy hyy hyy hyy
unix hello unix hiii unix

23
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335

10. Write an awk script which print frequency of each word in the input file.

AWK Script:
$ awk '
{
# Loop through all words in each line

for (i = 1; i <= NF; i++) {


# Convert the word to lowercase for case-insensitive counting word
= tolower($i)
# Increment the count for each word freq[word]++
}
}
END {
# Print each word and its frequency
for (word in freq) {
print word, freq[word]
} }'
test.txt
OUTPUT:

linux 1 nt 1
wd 2 surali
1 php 1 hiii
1 java 2 hyy
9 ospart 1
bharodiya 1
hello 1 unix
4

24

You might also like