KEMBAR78
108 Unix | PDF | Directory (Computing) | Filename
0% found this document useful (0 votes)
10 views20 pages

108 Unix

Il documento contiene istruzioni per vari comandi in Unix/Linux. Mostra i comandi eseguiti insieme ai loro output. Alcune cose chiave dimostrate includono: - Creazione di directory e file - Visualizzazione dei contenuti della directory corrente - Copia, spostamento e cancellazione di file - Ricerca di file in base a modelli nei loro nomi - Confronto e ottenimento di contenuti comuni tra file - Modifica di file utilizzando l'editor vi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views20 pages

108 Unix

Il documento contiene istruzioni per vari comandi in Unix/Linux. Mostra i comandi eseguiti insieme ai loro output. Alcune cose chiave dimostrate includono: - Creazione di directory e file - Visualizzazione dei contenuti della directory corrente - Copia, spostamento e cancellazione di file - Ricerca di file in base a modelli nei loro nomi - Confronto e ottenimento di contenuti comuni tra file - Modifica di file utilizzando l'editor vi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

TYBCA SEM-5 Div-4 Unix Roll No–108

Tutorial–1

1. Display the content of current directory.


[06/09/2022 - 10:23.01]~/desktop/unix/demo
[Kevin.DESKTOP-5I719O9]$ create directory d1

06/09/2022 - 10:23.21 ~/desktop/unix/demo


cd d1

[06/09/2022 - 10:23.31]~/desktop/unix/demo/d1
[Kevin.DESKTOP-5I719O9]$ cat >> f1
my name is hasti

2. Show calendar of January 1980.


January 1980 Su Mo Tu We Th Fr Sa 1 2 3 4 5 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

January 1980
Su Mo Tu We Th
Fr Sa1 2 3 4 5
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/2022 - 10:25.19]


~/desktop/unix/demo[Kevin.DESKTOP-5I719O9]$ pwd
/drives/C/Users/Hasti/Desktop/unix/t1

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
Tue October 03 , 12 : 30PM
Tue September 06, 10:26 AM

5. Display the below pattern with echo command


\
\ \ \
\ \ \ \ \
[06/09/2022 - 10:26.06]~/desktop/unix/demo

\
\\\
\\\\\

6.Execute a command to know the kernel version of the operating system you are working on.
[06/09/2022 - 10:26.31]~/desktop/unix/demo
1.7.5(0.225/5/3)

7. Execute a command to know your terminal.


[06/09/2022 - 10:26.56]~/desktop/unix/demo
CYGWIN_NT-6.2-
WOW64

1
TYBCA SEM-5 Div-4 Unix Roll No–108

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


[06/09/2022 - 10:27.16]~/desktop/unix/demo
cat > hasti.exe
How are you?
good or not?

[06/09/2022 - 10:28.11]~/desktop/unix/demo
cat hasti.exe
How are you?
good or not ?

[06/09/2022 - 10:28.21]~/desktop/unix/demo
ls *.exe
hasti.exe

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


06/09/2022 - 10:28.33 ~/desktop/unix/demo
ls -ltu
total 1
-rwxr-xr-x 1 Hasti Domain U 28 Sep 6 10:28hasti.exe
drwxr-xr-x 1 Hasti Domain U 0 Sep 6 10:23d1

10. Create five files named f1 to f5.


[06/09/2022 - 10:28.51]~/desktop/unix/demo
Input redirected to f1
hi

[06/09/2022 - 10:29.31]~/desktop/unix/demo
byyyyyy

[06/09/2022 - 10:29.41]~/desktop/unix/demo
[Kevin.DESKTOP-5I719O9]$ cat >f3
hello

[06/09/2022 - 10:29.48]~/desktop/unix/demo
[Kevin.DESKTOP-5I719O9]$ cat >f4
good morning

[06/09/2022 - 10:30.06]~/desktop/unix/demo
[Kevin.DESKTOP-5I719O9]$ cat >f5
apple

[06/09/2022 - 10:30.17]~/desktop/unix/demo
ls
d1f1 f2 f3 f4 f5 hasti.exe

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


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

06/09/2022 - 10:31.32 ~/desktop/unix/demo


cat f3
hi
byy
yyy
y

2
TYBCA SEM-5 Div-4 Unix Roll No–108

12. Display all files from the current directory that have the first and last character as a number.
06/09/2022
~/desktop/unix/demo[Kevin.DESKTOP-
5I719O9]$ create 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 contain only 3 letters.
06/09/2022 - 10:32.19
~/desktop/unix/demo[Kevin.DESKTOP-
5I719O9]$ ls ???
1f0

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


[ 09/09/2022 - 10:32.40]~/desktop/unix/Kevin

Kevin

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


[06/09/2022 - 10:33.10]~/desktop/unix/demo

16 -abc

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


06/09/2022 - 10:34.09
mv ex1 as1

Copy those files that must contain the 3rd character in the file name as a digit to the directory 'xtemp'.

06/09/2022
~/desktop/unix/demo
mkdir
xtemp

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

[06/09/2022 - 10:35.21]~/desktop/unix/demo
cd xtemp

06/09/2022
~/desktop/unix/demo/xtemp[Kevin.DESKTOP-
5I719O9]$ ls
1f0 as1

18. Create directory named 'helix', 'apache'.


[12/08/2022 - 12:17.57]
/drives/d/tybca316/unix/d1/xtemp[Kevin.DESKTOP-
5I719O9]$ mkdir helix apache

3
TYBCA SEM-5 Div-4 Unix Roll No–108
19. Copy the "helix" directory to the "tmp" directory.
[06/09/2022 - 10:36.07]~/desktop/unix/demo/xtemp
[Kevin.DESKTOP-5I719O9]$ cp -R helix tmp

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


apache
[Kevin.DESKTOP-5I719O9]$ cp -R helix tmp

[06/09/2022 - 10:37.00]~/desktop/unix/demo/xtemp
cd apache

[06/09/2022 - 10:37.20]~/desktop/unix/demo/xtemp
ls
1f0 as1

21. Remove file '-abc'.


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

4
TYBCA SEM-5 Div-4 Unix Roll No–108

Tutorial–2
1. List the contents of the current directory with file names as numbers.
[28/08/2022 - 14:42.25]~/desktop/unix3
[Kevin.DESKTOP-5I719O9]$ ls | grep -E '^[0-
9]+$'123

2. Display the filename containing only alphabets as names.


[28/08/2022 - 14:56.18]~/desktop/unix3
ls | grep -E '^[a-z A-
abc

3. Remove all files containing a digit as the 2nd letter in their names.
[28/08/2022 - 14:59.40]~/desktop/unix3
Remove files that start with any character followed by a digit.

rm: d1: is a directory

[28/08/2022 - 15:02.22]~/desktop/unix3
ls
M abcd1

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


[28/08/2022 - 15:32.31]~/desktop/unix3
cat
>asd[0-9]*hello everyone

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

[28/08/2022 - 15:06.33]~/desktop/unix3
c- the moon

[28/08/2022 - 15:09.26]~/desktop/unix3
Kevin.DESKTOP-5I719O9 cat > c1

[28/08/2022 - 15:10.06]~/desktop/unix3
cat > c2

[28/08/2022 - 15:10.12]~/desktop/unix3
[Kevin.DESKTOP-5I719O9]$ cat > c3

[28/08/2022 - 15:10.18]~/desktop/unix3
cat c1 c2 c3

[28/08/2022 - 15:17.21]~/desktop/unix3
the moon

6. display the date in the following format.


Today's date is : Sat Jul 30 15:25:31 IST 2011

[28/08/2022 - 15:25.01]~/desktop/unix3
Today's date is
Today's date is Sun Aug 28 15:28:23 IST 2022

5
TYBCA SEM-5 Div-4 Unix Roll No–108

7. Compare 2 files named sc1 and sc2 and store the common content in the file result.
~/desktop/unix3
abc def ghi

[29/08/2022 - 15:39.13]~/desktop/unix3
abc def jkl

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

[29/08/2022 - 15:42.17]~/desktop/unix3
abc def

8. Find how many 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 more than 5 characters in their names (the file names having the last two characters as digits).
[28/08/2022 - 15:45.41]~/desktop/unix3
abc12i am abc

[28/08/2022 - 15:48.48]~/desktop/unix3
I am psp

[28/08/2022 - 15:49.44]~/desktop/unix3
ls ???[0-9][0-9]
abc12 psp01

10. create the directory named 'maxx' and copy all files having only capital letters.
[29/08/2022 - 14:14.09]~/desktop/unix3
cp `ls [A-Z]` max

11.create a file named emp*[28/08/2022 - 21:41.34]


~/desktop/unix3[Kevin.DESKTOP-5I719O9]$ cat > emp*

28/08/2022 - 21:43.16 ~/desktop/unix3[Kevin.DESKTOP-5I719O9]$ ls


M abc12 c- c2 d1 psp01 sc1
abc asd[0-9]* c1 c3 emp* result sc2

12. make a list of employees in the following order ( use vi


editor
1 abcd programmer
2 xyz manager

employee ID | employee name | position


awer
employee
3| rtyy | director
4| jbyg | programmer

6
TYBCA SEM-5 Div-4 Unix Roll No–108

13. Display only the last accessed file from the current directory.
[29/08/2022 - 13:58.49]~/desktop/unix3
$ ls -t
head -n1vbe

14. Create a file named emp_list containing empid and date of joining.
[29/08/2022 - 14:09.27]~/desktop/unix3
[Kevin.DESKTOP-5I719O9]$ cut -d"|" -f1,4 emp > emp_list

[29/08/2022 - 14:10.01]~/desktop/unix3
[Kevin.DESKTOP-5I719O9]$ cat emp_list
employee ID
1
2
3
4

15. Copy the content of file "emp*" into emp_master1, emp_master2.


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

[29/08/2022 - 14:10.27]~/desktop/unix3
cp /emp* emp_master2

16. rename file emp_master2 to backup_emp

[Kevin.DESKTOP-5I719O9]$ mv emp_master2 backup_emp

17.remove file "emp*"


29/08/2022
rm /emp*

18. Display the path of the directory where all your mail is stored.
[29/08/2022 - 14:15.02]~/desktop/unix3
[Kevin.DESKTOP-5I719O9]$ echo $MAIL

7
TYBCA SEM-5 Div-4 Unix Roll No–108

19.create the following

structure export
color
|-- dir1
|-- file1.lst
|-- m1
| |-- f1
| | `-- emp.lst
| `-- f2
|
-- menu1.lst
-- m2
| |-- d1
| `-- file123
-- m3
-- abc.php
|-- dir1
-- tmp
-- xyz.java
$ mkdir export

$ mkdir color dir1 m1 m2 m3 dir2

$ cat > fil1.txtnjgghvfd


mjnuubyb hhfubdy
,nygyrf7h
h8er7ge

$ cd m1

$ mkdir f1 f2

$ cd f1

$ cat > emp.txtmbbdfj


huberu

heg67g
the universe

$ cd..

$ cd..

$ cat > menu.txt123


123 abdft j7tji rtfni
$
c
d
m
2

8
TYBCA SEM-5 Div-4 Unix Roll No–108

$ cd m2

$ cat > file123


mnbgudsj
I love you
nysegjke
setr7hie

$ cat > file123


mnbgudsj
hdyrjeg ufuh
nysegjke
setr7hie

$ cd ..

$ cat > sps.php


we will see
nhsdguhifh
hdyusgfun

$ cd dir2

$ cat > tmp


bugsugufds
bhfaygdka hfdugc
jgdyfu

$ cat > xyz.java

bdjsbck sdbhbkj
dbbc

$ ls
DFD UNIX asd[0-9]* c2 d1 emp export m2 psp01 sc2 xyz.java
HJKU abc c- c3dir1 emp* fil1.txt m3 sps.php
M abc12 c1 color dir2 emp_listm1 maxx sc1 vbe

20. Copy file emp.lst to tmp directory (in m3 dir) using relative path.
29/08/2022 - 15:37:46
cp m1/f1/emp.txt
m3/f1

21. Protect the directory 'export' against unauthorized access.


[29/08/2022 - 15:52.35]~/desktop/unix3
[Kevin.DESKTOP-5I719O9]$ chmod 730
export

22. allow only read access to file emp.lst.


[29/08/2022 - 15:53.31]~/desktop/unix3
[Kevin.DESKTOP-5I719O9]$ chmod 555
No text provided for translation.

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

Copy the content of file emp.lst to file123, make a backup of file 'file123' and rename it to file_bkp.
[29/08/2022 - 15:55.45]~/desktop/unix3/m2
cp file123
backup_file

25. Display the content of the 'export' directory in a manner similar to question 19.
[29/08/2022 - 15:55.57]~/desktop/unix3/m2
The command lists directories in the current path.
tmp
$ dir2/:
$ m1/:
F1
F2
$ m2/: File_bkp
$m3/:
F1

26.display last modified file.


[29/08/2022 - 16:01.11]~/desktop/unix3
ls -1t |
head -n1m2

27. make an archive file of all the files having .lst


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

28. Move all .lst files to tmp directory


[29/08/2022 - 16:20.17]~/desktop/unix3
'*.lst' mv
/dir2/tmp

29. Provide the permissions to the file "color" in such a way that only the owner can perform read and write operations while
group members and others can only execute a file.
[29/08/2022 - 16:22.43]~/desktop/unix3
chmod 611
color

30. Change the ownership of file 'tmp' to root.


$ sudoPass:
#chown hasti root
tmp#exit
$

10
TYBCA SEM-5 Div-4 Unix Roll No–108

Tutorial–3
1. Write a command to display the content of the top 3 largest files in a.
workingdirectory.
[29/08/2022 - 14:29.19] ~/desktop/unix/pract
ls -s | head -
n2
0 f1
0 f2

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


[29/08/2022 - 14:33.17]~/desktop/unix/pract
number of words in the first two lines of file f1
2

3. Display all filenames not beginning with '.'


[29/08/2022 - 14:35.57]~/desktop/unix/pract
List files and directories that do not start with a dot
f1
f2

4. Delete all special characters from file f1.


[29/08/2022 - 14:36.09]~/desktop/unix/pract

9 ]' <f1hii
hello

5. Display the i-node number of all files in the current directory.


[29/08/2022 - 14:38.43]~/desktop/unix/pract
ls: cannot access 'i': No such file or directory
9851624184977260 f119984723346472289 f2 16888498602744989 f3 18295873486298472 f4

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


[29/08/2022 - 14:40.20]~/desktop/unix/pract
[Kevin.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/2022 - 14:44.51]~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ cat f2
HELLO

8. Extract all usernames and their home directories from the /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
/home/HastiWDAGUtilityAccount

9. Locate lines of file where the second and second last character of the lines are the same.
[29/08/2022 - 14:50.08]~/desktop/unix/pract
grep
Match any single character followed by the same character later in the string.

Unix shell programming

11
TYBCA SEM-5 Div-4 Unix Roll No–108

10. Display all lines of files that contain the 'hello' pattern in it.
[29/08/2022 - 14:50.12]~/desktop/unix/pract
grep 'hello' `grep -l
hello
hello

11. Display all lines having 'g*' pattern


in it.[29/08/2022 - 14:53.09]
~/desktop/unix/pract[Kevin.DESKTOP-
5I719O9]$ grep 'g*' f3

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


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

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

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

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


[29/08/2022 - 15:23.48]~/desktop/unix/pract
hii hello

16. Write a Unix command to evaluate an expression: 4*3.14+6


[29/08/2022 - 15:24.15]~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ awk 'BEGIN {print
18.56

17. write a command to display all unique words of


file f1.[31/08/2022 - 12:09.29]~/desktop/unix/pract
tr ' ' '\n' <f1 | sort | uniq -u
hello
hi

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

19. Write a command to display all lines that contain 2 or more ^


symbol at the beginning of line.
[31/08/2022 - 12:12.24]~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ grep -E
'^\{2,}' f1

sed -i 's/he/she/g; s/hello/hi/g' f1


[31/08/2022 - 12:17.15]~/desktop/unix/pract
The command replaces 'he' with 'she' and 'hello' with 'hii' in the file 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

's/he/she/' f2Hii
HELLO unix is subject she is very annoying this is the life
Unix shell programming Unix is open source Good night
bin
display
bin
display

21. Display those lines having exactly 10 alphabets from file f1.
[31/08/2022 - 12:18.36]~/desktop/unix/pract
grep '^[A-Za-z]
{10}$' f2

22.Copy file content of f1 to file f2 if f1 exists 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 the current directory which have more than 5 links.
[31/08/2022 - 12:23.30]~/desktop/unix/pract
find . -links +5 -
print

24. Display lines of file f1 that do not contain a digit


in it.[31/08/2022 - 12:23.33]~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ grep -v '[0-9]' f2
hi hello

25. Replace all occurrences of 'linux OS' with 'unix OS' in file f1.
[31/08/2022 - 12:26.04]~/desktop/unix/pract
sed 's/linux os/unix
os/g' f3kevin

hello
good
morning
byyyy
HELLO unix is subject she is very annoying this is the life
unix shell programming unix is open source Good night
bin
display
bin
display

26. Display all lines of file f1 having the 3rd word as


user.[31/08/2022 - 12:36.23]~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ grep '^[^ ]* [^ ]* user'
f2

27. Display the names of all files in the working directory that have the 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

[Kevin.DESKTOP-5I719O9]$ grep '^[A-Z]' f3


kevin HELLOunix is subject Good night

29.Write a sed command to extract the first word of each line. Assuming that there is no whitespace character at the beginning of the line.
[31/08/2022 - 12:35.55]~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ sed 's/ .*/ /g' f3
kevin
hello goodbye
HELLOunix
she
this unix unix Good
bin bin

30. What does the following command do? grep f1 f2 f3

31. Display only those lines of file f1 having a length 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 a subject she is very.
annoying this is the life
unix is open
Good
night
bin
display
bin
display

32. Display binary value of 12 using bc

33.Replace all occurrences of “hi” with “hi” and “she” 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
~/desktop/unix/pract[KEVIN.DESKTOP-
5I719O9]$ sed 's/he/she/' f2Hii
HELLO unix is subject she is very annoying this is the life
unix shell programming unix is open source Good night
bin
display
bin
display

14
TYBCA SEM-5 Div-4 Unix Roll No–108

34. Count the number of words and lines in files whose filename begins with x.
31/08/2022 - 12:40.29 ~/desktop/unix/pract
find . -maxdepth 1 -type f -name 't*' -exec wc -wl {} \;

35. Write equivalent sed command of 'sed '1,5d''


f1".[31/08/2022 - 12:41.19]~/desktop/unix/pract
[Kevin.DESKTOP-5I719O9]$ sed -n '1,5!p' f3
HELLOunix is subject
she is very annoying this is the life
Unix shell programming Unix is open source Good night
bin
display
bin
display

36. Write equivalent IRE for the following regular expression


- A*
- A?
31/08/2022
~/desktop/unix/pract[Kevin.DESKTOP-
5I719O9]$ grep 'A{1,}' f3

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 the rev() function, applying proper validation.
vi reverse.sh
Enter a String:
a
if test -z "$str"
then
String is Null
else
len=${#str}
reverse string is:
while[$len-greater-than-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:k e v i n

5. Write a shell script to find a file with maximum size in the current directory, also print the number of words, characters, and
lines along with the content of the file

fname=`ls -lS | tr -s " " | grep '^- ' | cut -d ' ' -f 9 | head -n 1`
File with max size is : $fname
this is Contents of $fname :
*contents start*
============================================
echo
cat $fname
============================================
*contents end*
echo
echo
Total number of lines in $fname = `wc -l < $fname`
Total number 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`
File with max size is : $fname
this is Contents of $fname :
*contents start*
============================================
echo

16
TYBCA SEM-5 Div-4 Unix Roll No–108
cat $fname
============================================
*contents end*
echo
echo
Total number of lines in $fname = `wc -l < $fname`
echo "Total number of words in $fname = `wc -w < $fname`"
Total number of characters in $fname = `wc -c < $fname`
============================================
*contents end*
Total number of lines in script.sh = 16
Total number of words in script.sh = 94
Total number of characters in script.sh = 561

6. Write a shell script that functions as a calculator (input the values via command line arguments)
a=$1
b=$2
number one is:$a
number two is:$b
Enter Choice :
1. Addition
2. Subtraction
3. Multiplication
4. Division
read ch

case $ chin1) res = `echo $ a + $ b | bc`


;;
2) res=`echo $a - $b | bc`
;;
3)res=`echo $a * $b | bc`
;;
4) res=`echo "scale=2;$a/$b" | bc`
;;
case
echo "Result :$res"
~
10/09/2022 - 23:03.32 ~/desktop/unix/kevin
Running script calc.sh with parameters 10 and 20.
number one is: 10
number two is: 20
Enter Choice :
Addition
2. Subtraction
Multiplication
4. Division

10-20
-10

17
TYBCA SEM-5 Div-4 Unix Roll No–108
7 Write a shell script that checks if an entered user is valid or not.

echo "Enter User Name";


read uname
set who|grep uname
if [ $# -eq 0 ]
then
echo$uname"is Not Valid User!";
else
echo $uname "is Valid User";
fi
~
~
[10/09/2022 - 23:03.32]~/desktop/unix/kevin
Enter User Name
Kevin

Q-8) Write an awk script to print fields 2, 3, 4, and 6 from the test.txt file that contains the pattern 'unix'.

awk -F" " '/Unix/{print $2,$3,$4,$6}' test.txt


temporarily occurred and
we strive for labor
Unix Duisautedolor
pain Unix eunulla
they are in which
Unixipsumdoloramet,
Unix necessity of labor
Unix Duisautedolor
pain
they are in who

Q-9) Write an awk script that displays the first two words of each line of the file f1

kevin@kevin-VirtualBox:~/kevin/demo$ awk '{print $1,$2}' test.txt


Loremipsum
Unixtempor
Unixquis
consequence. Unix
pain
Unixproident,
LoremUnix
temporary
who is Unix
consequence. Unix
Unixcillum
Unixproident,

18
TYBCA SEM-5 Div-4 Unix 108
Q-10) Write an awk script which prints file contents from bottom to top
OR
Write an awk script which prints the frequency of each word in the input file.
Note: Suggestions are invited
‘{
line[NR]=$0
}
END{
for(i=NR;i>=1;i--)
print line[i]
}’ f6
swap
parth
universe
akshit

19
TYBCA SEM-5 Div-4 Unix Roll No–108

20

You might also like