Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 39

UNIX UTKARSH SENGAR

T.Y .BCA (42)

1. Start recording all below performed commands and stop at 7 the question.

student@ubuntu:~$ script
output:Script started, file is typescript
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

2. Display calendar of year 2016 and paging output.

student@ubuntu:~$ cal 2016

3. Display current month in number only

student@ubuntu:~$ date +%m


output:06

4. Perform following calculations:


a. Display output of cube of 15
b. Addition of 25+36 and 98+65 in same line
c. Perform division of 17/7 with precision
d. Get output in binary of 688
e. Find the power of 12 of 8
f. Find 9/5. (output should display 2 numbers after precision)
g. Find decimal of 11001010
h. Find binary of 14

student@ubuntu:~$ bc
output:bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
a)15^3
output:3375

b)25+36;98+65
output:61
163

c)scale=2
17/7
output:2.42

1|Page
UNIX UTKARSH SENGAR
T.Y .BCA (42)

d)obase=2
688
output:1010110000

e)12^8
output:429981696

f)scale=2
9/5
output:1.80

g)ibase=2
11001010
output:202

h)ibase=2
11001010
output:202

5. Display currently logged in users.

student@ubuntu:~$ who
output:student :0 2019-06-26 00:56 (:0)
student pts/2 2019-06-26 00:57 (:0)
student pts/17 2019-06-26 01:08 (:0)

6. Display your machine’s characteristics

student@ubuntu:~$ uname
output:Linux

7. Display your terminal

student@ubuntu:~$ tty
output:/dev/pts/18

8. Check your current directory

student@ubuntu:~$ pwd
output:/home/student

9. Create a new directory with your rollno. E.g. TYBCA01

student@ubuntu:~$ mkdir TYBCA42

2|Page
UNIX UTKARSH SENGAR
T.Y .BCA (42)

10. Change directory to you currently created directory.

student@ubuntu:~$ cd TYBCA42
student@ubuntu:~/TYBCA42

11. Create 3 files inside current directory named foodfile, moviefile and placefile.(Write your
favorite food, movie, place as contents of files)

student@ubuntu:~$ cd TYBCA42
student@ubuntu:~/TYBCA42$ cat >foodfile
pizza
burger
student@ubuntu:~/TYBCA42$ cat >moviefile
avtar
robot
student@ubuntu:~/TYBCA42$ cat >placefile
goa
venice

12. Display contents of newly created files with line numbers.

student@ubuntu:~/TYBCA42$ cat foodfile moviefile placefile


pizza
burger
avtar
robot goa
venice
student@ubuntu:~/TYBCA42$ wc -l foodfile moviefile placefile
2 foodfile
1 moviefile
2 placefile
5 total

13. Copy foodfile to favfoodfile

student@ubuntu:~/TYBCA42$ cp foodfile favfoodfile

14. Rename moviefile to favmoviefile

student@ubuntu:~/TYBCA42$ mv moviefile favmoviefile

3|Page
UNIX UTKARSH SENGAR
T.Y .BCA (42)

15. Count total numbers of lines, words, and characters of favmoviefile

student@ubuntu:~/TYBCA42$ wc favmoviefile
output: 1 2 14 favmoviefile

16. Display only total number of characters of placefile.

student@ubuntu:~/TYBCA42$ wc -c placefile
output:13 placefile

17. Display only total number of lines of foodfile.

student@ubuntu:~/TYBCA42$ wc -l foodfile
output: 2 foodfile

18. Display only total number of words of favmoviefile

student@ubuntu:~/TYBCA42$ wc -w favmoviefile
output:2 favmoviefile

19. Create two new files names Stud1 and Stud2 containing names of students

student@ubuntu:~/TYBCA42$ cat >stud1


nikunj
sarabjeet
dipen
shiv
ashish
utkarsh

student@ubuntu:~/TYBCA42$ cat >stud2


swastik
dipen
utkarsh
nikunj
sushil

20. Compare two files and display its output.

4|Page
UNIX UTKARSH SENGAR
T.Y .BCA (42)

student@ubuntu:~/TYBCA42$ cmp stud1 stud2


output:stud1 stud2 differ: byte 1, line 1

21. Find out what is common between Stud1 and Stud2 files

student@ubuntu:~/TYBCA42$ cmp stud1 stud2

22. Convert one file type to other (use dos2unix and unix2dos cmds)
student@ubuntu:~/TYBCA42$ dos2unix <stud1
student@ubuntu:~/TYBCA42$ unix2dos <stud2

23. Display all contents of your current directory

student@ubuntu:~/TYBCA42$ ls
output:favfoodfile favmoviefile foodfile placefile stud1 stud2

24. Display long listing of all files.

student@ubuntu:~$ ls -l
output:total 72
drwxrwxr-x 2 student student 4096 Aug 23 2018 bat
drwxr-xr-x 6 student student 4096 Jun 26 01:06 Desktop
drwxr-xr-x 2 student student 4096 Aug 23 2018 Documents
drwxr-xr-x 2 student student 4096 Aug 23 2018 Downloads
-rw-rw-r-- 1 student student 381 Oct 8 2018 emp.lst
-rw-r--r-- 1 student student 8980 Aug 23 2018 examples.desktop
drwxr-xr-x 2 student student 4096 Aug 23 2018 Music
-rwxrwxr-x 1 student student 187 Dec 6 2018 nilu
drwxr-xr-x 2 student student 4096 Aug 23 2018 Pictures
drwxr-xr-x 2 student student 4096 Aug 23 2018 Public
-rw-rw-r-- 1 student student 73 Aug 23 2018 saif
drwxr-xr-x 2 student student 4096 Aug 23 2018 Templates
drwxrwxr-x 3 student student 4096 Jun 26 03:18 TYBCA28
-rw-rw-r-- 1 student student 4885 Jun 26 02:24 typescript
drwxr-xr-x 2 student student 4096 Aug 23 2018 Videos

25. Write default file permissions

5|Page
UNIX UTKARSH SENGAR
T.Y .BCA (42)

student@ubuntu:~/TYBCA42$ chmod 644 newcity

26. Give other user execute permission for foodfile

student@ubuntu:~/TYBCA42$ chmod u+x favfoodfile

27. Give all users write permission for stud1 file (use Absolute permission)

student@ubuntu:~/TYBCA42$ chmod 222 stud1

28. Write command to display file that has recorded 1 st to 7 the questions.

student@ubuntu:~$ cat typescript


Script started on Wed 26 Jun 2019 01:58:08 AM PDT
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

29. Display current date and time

student@ubuntu:~/TYBCA42$ date
output:Wed Jun 26 03:15:27 PDT 2019

30. Display current month in digits as well as string

student@ubuntu:~/TYBCA42$ date +"%h %m"


output:Jun 06

31. Make new directory of your name inside current directory.

student@ubuntu:~/TYBCA42$ mkdir utkarsh

32. Change directory to parent directory of current directory.

student@ubuntu:~/TYBCA42/utkarsh$ cd $home
student@ubuntu:

33. Sort all 3 files of current directory and redirect its output to new files named
newcity,newstate and newcountry.

6|Page
UNIX UTKARSH SENGAR
T.Y .BCA (42)

student@ubuntu:~/TYBCA42$ sort favfoodfile >newcity;sort placefile >newcountry;sort


favmoviefile >newstate

34. Give contents of file city as input to wc command and display only word count.

student@ubuntu:~/TYBCA42$ wc -w newcity
2 newcity

35. Display total number of lines when ls command is used (use pipe)

student@ubuntu:~/TYBCA42/pattern$ ls | wc -l

36. Check your current path.

student@ubuntu:~/TYBCA42$ pwd
/home/student/TYBCA42

37. Display your own UID and GID

student@ubuntu:~/TYBCA42$ id
uid=1000(student) gid=1000(student)
groups=1000(student),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),12
8(sambashare)

38. Make a new directory named pattern in the current directory

student@ubuntu:~/TYBCA42$ mkdir pattern

39. Create 5 new files inside pattern directory (name the files as - unix1, unix02,
unix3,funix1, unix2)

student@ubuntu:~/TYBCA42/pattern$ cat >unix1


easy

40. Display all files starting with “u” in pattern directory.

student@ubuntu:~/TYBCA42/pattern$ cat >unix1

7|Page
UNIX UTKARSH SENGAR
T.Y .BCA (42)

easy
student@ubuntu:~/TYBCA42/pattern$ cat unix02
cat: unix02: No such file or directory
student@ubuntu:~/TYBCA42/pattern$ cat >unix02
it is an operating system
student@ubuntu:~/TYBCA42/pattern$ cat >unix03
commands
student@ubuntu:~/TYBCA42/pattern$ cat >funix1
hard
student@ubuntu:~/TYBCA42/pattern$ cat >unix2
it is flexible

41. Display all files starting with “unix” and having only 1 character after 1

student@ubuntu:~/TYBCA42/pattern$ ls u*
unix02 unix03 unix1 unix2

42. Display files in series (unix1-unix3). (use character class)

student@ubuntu:~/TYBCA42/pattern$ ls unix[1-3]
unix1 unix2

43. Display the output of ls-l command on terminal as well as in new file named filelist.

student@ubuntu:~/TYBCA42/pattern$ ls -l >filelist

44. Remove pattern directory

student@ubuntu:~/TYBCA42$ rmdir pattern

45. Remove sortedcity, sortedstate and sortedcountry files

student@ubuntu:~/TYBCA42$ rm newcity newstate newcountry

46. Find the output of the following commands


a) expr 10 % -4
b) expr xyz123q : ‘.*’
c) echo “Files:” ‘ls’ #ls is enclosed within back quotes
d) ls [?0-9]?[a-z]*

8|Page
UNIX UTKARSH SENGAR
T.Y .BCA (42)

e) ls –l | grep ‘^d’ | wc-l


f) expr “abcdefg” : ‘.*(..\)’
g) echo ${x}x Assume that x is having value 12

a)student@ubuntu:~/TYBCA42$ expr 10 % -4
output:2

b)student@ubuntu:~/TYBCA42$ expr xyz123q : '.*


output:7

c)student@ubuntu:~/TYBCA42$ echo "Files:" `ls`


output:student@ubuntu:~/TYBCA42$ echo "Files:" `ls`
Files: 0ab9 0unix bca f2 favfoodfile favmoviefile ff2
ff2~ ff3 ff5 file1 file3 file4 file5 file6 foodfile h1 h2 utkarsh mya newcity
newcountry newfile newstate pattern placefile stud1 stud2 X1 X1~ X1.link

d)student@ubuntu:~/TYBCA42$ ls [?0-9]?[a-z]*
0ab9 0unix

e)student@ubuntu:~/TYBCA42$ ls -l |grep '^' | wc -l


32

f)student@ubuntu:~/TYBCA42$ expr "abcdefg" : '.*(.. / )'


output:0

g)student@ubuntu:~/TYBCA42$ echo ${x}x


output:x

9|Page
UNIX UTKARSH SENGAR
T.Y .BCA (42)

1. Display line starting from 10 th line to the end of file X1

student@ubuntu:~/TYBCA42$ tail -n +10 X1


student@ubuntu:~/TYBCA42$ tail -n +10 X1
4290|jayant choddhary |executive |production |07/09/50|6000
2476|anil aggarwal |manager |computer |01/05/68|unix
6521|lalit chowdary |director |marketing |12/12/55|8200
3212|shyam saksena |d.g.m |accounts |26/09/45|6000
3564|sudhir agarwal |executive |personnel |12/03/45|7500
2345|j.b.saxena |g.m |marketing |31/12/40|8000
0110|v.k.agarwal |g.m |marketing |05/09/62|9000

2. Display all files in current directory where the first character is numeric and last
character is not alphabetic.

student@ubuntu:~/TYBCA42$ ls [0-9]*[!a-z]
student@ubuntu:~/TYBCA42$ find . -name [0-9]*[!a-z] -print
output:0ab9

3. Display the contents of last modified file.

student@ubuntu:~/TYBCA42$ cat `ls -t |head -1`

hello
hello world
unix1
unix1
it is an operating system

4. Display contents of top 3 largest files in a working directory.

student@ubuntu:~/TYBCA42$ cat `ls –s |head –n 3`

5. Display all files of current directory whose 1 st character is not digit.

student@ubuntu:~/TYBCA42$ ls [!0-9]*
bca ff2 file1 file6 newcity placefile X1~
f2 ff2~ file3 foodfile newcountry stud1 X1.link
favfoodfile ff3 file4 h1 newfile stud2
favmoviefile ff5 file5 h2 newstate X1
utkarsh:
mya:
tybca
pattern:

10 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

filelist funix1 unix02 unix03 unix1 -unix1 unix2

6. Display inode number of all files of current directory.

student@ubuntu:~/TYBCA42$ ls -i
808524 0ab9 808916 ff3 808914 h1 808841 pattern
808877 0unix 808570 ff5 808920 h2 808751 placefile
808794 bca 808827 file1 808742 utkarsh 808761 stud1
808917 f2 808842 file3 808717 mya 808762 stud2
808748 favfoodfile 808810 file4 808790 newcity 808840 X1
808750 favmoviefile 808800 file5 808813 newcountry 808777 X1~
808881 ff2 808811 file6 808809 newfile 808840 X1.link
808919 ff2~ 808741 foodfile 808828 newstate

7. Forcefully delete each file from directory tybca/mya/mywork

student@ubuntu:~/TYBCA42/mya$ rm -f mywork

8. Show all hidden files from mydir/tybca

student@ubuntu:~/TYBCA42/mya/tybca$ ls -a
student@ubuntu:~/TYBCA42$ ls -a
. f2 ff3 file4 h2 newfile stud2
.. favfoodfile ff5 file5 utkarsh newstate X1
0ab9 favmoviefile file1 file6 mya pattern X1~
0unix ff2 file3 foodfile newcity placefile X1.link
Bca ff2~ .file3.swp h1 newcountry stud1

9. Sort filename by last access time from directory tybca/work

student@ubuntu:~/TYBCA42/mya/tybca$ ls -lt |sort


drwxrwxr-x 2 student student 4096 Jul 17 01:23 pattern
drwxrwxr-x 2 student student 4096 Jun 26 03:18 utkarsh
drwxrwxr-x 3 student student 4096 Jul 24 01:49 mya
-rw-r--r-- 1 student student 14 Jul 10 02:07 newcity
-rw-rw-r-- 1 student student 0 Jul 31 03:04 f2
-rw-rw-r-- 1 student student 0 Jul 31 03:05 ff3
-rw-rw-r-- 1 student student 101 Jul 3 03:09 file4
-rw-rw-r-- 1 student student 131 Jul 3 03:07 file5
-rw-rw-r-- 1 student student 135 Jul 10 02:25 file3
-rw-rw-r-- 1 student student 13 Jul 10 02:07 newcountry

11 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

-rw-rw-r-- 1 student student 13 Jun 26 02:31 placefile


-rw-rw-r-- 1 student student 14 Jun 26 02:29 favmoviefile
-rw-rw-r-- 1 student student 15 Jul 10 02:07 newstate
-rw-rw-r-- 1 student student 16 Jul 17 03:04 0ab9
-rw-rw-r-- 1 student student 19 Jul 3 03:05 newfile
-rw-rw-r-- 1 student student 24 Jul 31 03:19 ff5
-rw-rw-r-- 1 student student 28 Jul 17 03:05 0unix
-rw-rw-r-- 1 student student 28 Jul 3 02:05 bca
-rw-rw-r-- 1 student student 34 Jul 31 02:47 ff2
-rw-rw-r-- 1 student student 34 Jul 31 02:47 ff2~
-rw-rw-r-- 1 student student 35 Jun 26 02:55 stud2
-rw-rw-r-- 1 student student 39 Jul 31 03:07 h2
-rw-rw-r-- 1 student student 49 Jul 31 03:06 h1
-rw-rw-r-- 1 student student 57 Jul 31 03:22 file1
-rw-rw-r-- 1 student student 60 Jul 3 03:21 file6
-rw-rw-r-- 1 student student 901 Jul 31 01:59 X1~
-rw-rw-r-- 2 student student 901 Jul 31 01:59 X1
-rw-rw-r-- 2 student student 901 Jul 31 01:59 X1.link
-rw-rw-r-x 1 student student 14 Jun 26 02:28 foodfile
total 116

10. Find out inode number of file chap01


student@ubuntu:~/TYBCA42$ ls –i
141596 desktop 144745 example.desktop 141595 public
141596 documents 144745 music 141594 Templates
141596 Downloads 144745 pictures 141599 videos

11. Count number of characters in first five lines of file x1

student@ubuntu:~/TYBCA42$ head -n 10 X1 |cut -d \| -f 2,3,4 X1 |wc -c


616

12. To replace the word Computer with Computing of file x1

student@ubuntu:~/TYBCA42$ sed 's/computer/computing/' X1

2233|a.k.shukla |g.m |sales |12/12/52|6000


9876|jai sharma |director |production |23/03/50|7000
5678|sumit chakrobarty|d.g.m |marketing |19/04/43|unix

12 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

2365|barun sengupta |director |personnel |11/05/47|7800


5423|n.k.gupta |chairman |admin |30/08/56|5400
1006|chanchal singhvi |director |sales |03/09/38|6700
6213|karuna ganguly |g.m |accounts |05/06/63|6300
abcd|s.n. dasgupta |manager |sales |12/09/63|5600
4290|jayant choddhary |executive |production |07/09/50|6000
2476|anil aggarwal |manager |computing |01/05/68|unix
6521|lalit chowdary |director |marketing |12/12/55|8200
3212|shyam saksena |d.g.m |accounts |26/09/45|6000
3564|sudhir agarwal |executive |personnel |12/03/45|7500
2345|j.b.saxena |g.m |marketing |31/12/40|8000
0110|v.k.agarwal |g.m |marketing |05/09/62|9000

13. Display lines whose last word is UNIX of file x1

student@ubuntu:~/TYBCA42$ grep unix$ X1


5678|sumit chakrobarty|d.g.m |marketing |19/04/43|unix
2476|anil aggarwal |manager |computer |01/05/68|unix

14. To delete all special characters from the file x1

student@ubuntu:~/TYBCA42$ tr -cd [a-z\012] <X1


22akshuklagmsales12122000jaisharmadirectorproduction200000sumitchakrobartydgm
marketing10unix2barunsenguptadirectorpersonnel110002nkguptachairmanadmin00001
00chanchalsinghvidirectorsales000021karunagangulygmaccounts0000abcdsndasguptam
anagersales1200020jayantchoddharyexecutiveproduction0000002anilaggarwalmanagerc
omputer010unix21
lalitchowdarydirectormarketing1212200212shyamsaksenadgmaccounts20000sudhiragar
walexecutivepersonnel120002jbsaxenagmmarketing11200000110vkagarwalgmmarketi

15. To display lines beginning with alphabets of a file x1

student@ubuntu:~/TYBCA42$ grep "^[a-z]" X1


abcd|s.n. dasgupta |manager |sales |12/09/63|5600

16. To remove a file name as x1? (x1 followed by ? character is the filename)

student@ubuntu:~/TYBCA42$ rm X1?
student@ubuntu:~/TYBCA42$ cat X1?
cat: X1?: No such file or directory

13 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

17. To run script X1 in background so that its execution continue even after user logout from
the system

student@ubuntu:~/TYBCA42$ nohup sort h4 &

18. To display last line of a file x1

student@ubuntu:~/TYBCA42$ tail -n 1 X1
0110|v.k.agarwal |g.m |marketing |05/09/62|9000

19. To deny execute permission to a group of a file x1

student@ubuntu:~/TYBCA42$ chmod -x X1
student@ubuntu:~/TYBCA42$ ls -l X1
-rw-rw-r-- 1 student student 901 Jul 31 01:30 X1

20. To create link between file x1 and x1.link

student@ubuntu:~/TYBCA42$ chmod -x X1
student@ubuntu:~/TYBCA42$ ls -l X1
-rw-rw-r-- 1 student student 901 Jul 31 01:30 X1
student@ubuntu:~/TYBCA42$ ls -l
total 88
-rw-rw-r-- 1 student student 35 Jun 26 02:55 stud2
-rw-rw-r-- 2 student student 901 Jul 31 01:30 X1
-rw-rw-r-- 2 student student 901 Jul 31 01:30 X1.link

21. Display lines 10 to 15 from file x1

student@ubuntu:~/TYBCA42$ sed –n ’10,15p’ X1

2476|anil aggarwal |manager |computing |01/05/68|unix


6521|lalit chowdary |director |marketing |12/12/55|8200
3212|shyam saksena |d.g.m |accounts |26/09/45|6000
3564|sudhir agarwal |executive |personnel |12/03/45|7500
2345|j.b.saxena |g.m |marketing |31/12/40|8000
0110|v.k.agarwal |g.m |marketing |05/09/62|9000

22. Display last word of each line from file x1

student@ubuntu:~/TYBCA42$ cut -d "|" -f 6 X1

14 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

6000
7000
unix
7800
5400
6700
6300
5600
6000
unix
8200
6000
7500
8000
9000

23. To count number of characters in last line of x1

student@ubuntu:~/TYBCA42$ tail -n 1 X1|wc -c


60

24. To count number of words in line 40 through 60 of file f1.txt

student@ubuntu:~/TYBCA42$ sed -n '40,60p' X1 |wc -w


89

25. Display all line that start with ‘let’ from file x1.1, e and t may be in any case

student@ubuntu:~/TYBCA42$ grep "^[Let]" x1.1


Lets go
Hello world

26. To remove duplicate line from a file

student@ubuntu:~/TYBCA42$ sort f2 | uniq >f2


hello all
hello world

27. Display the process of user ‘bca01’

student@ubuntu:~/TYBCA42$ ps -u
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
student 1866 0.0 0.4 7144 4732 pts/2 Ss 01:25 0:00 bash
student 2576 0.0 0.2 5320 2372 pts/2 R+ 02:34 0:00 ps -u

15 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

28. List the user from /etc/passwd in alphabetically sorted order.

29. Count the frequency of users who are logged in from more than one terminal

student@ubuntu:~/TYBCA42$ who |wc –l


2

30. Display string in upper case of file f1.txt

student@ubuntu:~/TYBCA42$ tr '[a-z]*' '[A-Z]*' < f1.txt


HELLO
HOW ARE YOU
UNIX

31. Write a command to print first seven line of file

student@ubuntu:~/TYBCA42$ head -n 10 X1

2233|a.k.shukla |g.m |sales |12/12/52|6000


9876|jai sharma |director |production |23/03/50|7000
5678|sumit chakrobarty|d.g.m |marketing |19/04/43|unix
2365|barun sengupta |director |personnel |11/05/47|7800
5423|n.k.gupta |chairman |admin |30/08/56|5400
1006|chanchal singhvi |director |sales |03/09/38|6700
6213|karuna ganguly |g.m |accounts |05/06/63|6300
abcd|s.n. dasgupta |manager |sales |12/09/63|5600
4290|jayant choddhary |executive |production |07/09/50|6000

32. Write a command to sort a line of file and also remove repeated line

student@ubuntu:~/TYBCA42$ sort file1 |uniq


hello
hello world
it is an operating system
unix1

33. Replaces all occurrence of ‘SYBCA’ with ‘TYBCA’ in 5th line of file f1

16 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

student@ubuntu:~/TYBCA42$ sed ‘5/tybca/sybca/’ f1

34. Count all occurrences of ‘TYBCA’ in student

student@ubuntu:~/TYBCA42$ grep –o “tybca” f1 |wc –w

35. Replace ‘kernel’ with ‘kernel architecture’ using remember pattern of sed utility.

student@ubuntu:~/TYBCA42$ sed ‘/kernel/s// & architecture/’ X1

36. To run a utility x1 at 9:00 AM

student@ubuntu:~/TYBCA42$ at 9:00am

37. To count number of characters in first three line of file x1

student@ubuntu:~/TYBCA42$ head -3 X1|wc –c

38. To display number of processes

student@ubuntu:~/TYBCA42$ ps
PID TTY TIME CMD
1815 pts/1 00:00:00 bash
1883 pts/1 00:00:00 ps

39. Remove a file forcibly which do not have write permission

student@ubuntu:~/TYBCA42$ chmod 444 f1


student@ubuntu:~/TYBCA42$ rm f1

40. To concatenate two files f1 and f2 including some text to be inserted from keyboard
after content of f1

student@ubuntu:~/TYBCA42$cat f1-f3>f3
World
student@ubuntu:~/TYBCA42$ cat f3

41. To display all files having last character as digit

student@ubuntu:~/TYBCA42$ ls –l|grep”[0-9]$”

-rw-rw-r-- 1 student student 16 jul 17 03:04 0bac9


-rw-rw-r-- 1 student student 18 jul 17 03:06 0utkarsh9

17 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

42. Remove directory tree dir/dir2/dir3 using single command

student@ubuntu:~/TYBCA42$ rm –rf dir

18 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

A. Perform following commands on the file emp.lst:-

1. Write a command to print first three lines of file


utkarsh@ubuntu:~$ sed '3q' emp,lst
2233|a.k shukla |g.m. |sales |12/12/52|6000
9876|jai sharma |director |production|12/03/50|7000
5678|sumit chakrobarty |d.g.m.|marketing |19/04/43|6000

2. Write a command to substitute ‘doshi’ to ‘desai’


utkarsh@ubuntu:~$ sed 's/doshi/desai/' emp.lst
2233|a.k shukla |g.m. |sales |12/12/52|6000
9876|jai sharma |director |production|12/03/50|7000
5678|sumit chakrobarty |d.g.m.|marketing |19/04/43|6000
2365|barun sengupta |director |personnel|11/05/47|7800
5423|n.k. gupta |chairman |admin |30/08/56|5400
1006|chanchal singhvi |director |sales |03/09/38|6700
6213|karuna ganguly |g.m. |accounts |05/06/62|6300
1265|s.n. dasgupta |manager |sales |12/09/55|5600
4290|jayant chaoudhary |executive|production|07/09/50|6000
2476|anil aggarwal |manager |sales |01/09/59|5000
6521|lalit chowdary |director |marketing|26/09/45|8200
3212|shyam sakesena |d.g.m. |accounts |12/12/55|6000
3564|sudhir Agrawal |executive|personnel|06/07/47|7500
2345|j.b. saxena |g.m. |marketing|12/03/45|8000
0110|v.k agrawal |g.m |marketing|31/12/40|9000

19 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

0989|shiv desai |executive|accounts|09/12/89|8721


9654|shivani singh |d.g.m |sales |11/06/99|9891

3. Write a command to display occurrence of string ‘Sales’


utkarsh@ubuntu:~$ grep –c 'sales' emp.lst
5

4. Write a command to display line which start with ‘The'


utkarsh@ubuntu:~$ grep "^the" filex23
the kernel architecture
the way of expression should be easy to understand

5. Write a command to display line which end with ‘ia’


utkarsh@ubuntu:~$ grep ".ia$" filex23
india

6. Write a command to print lines which do not contain ‘Sales’


utkarsh@ubuntu:~$ grep -v "sales" emp.lst
9876|jai sharma |director |production|12/03/50|7000
5678|sumit chakrobarty |d.g.m.|marketing |19/04/43|6000
2365|barun sengupta |director |personnel|11/05/47|7800
5423|n.k. gupta |chairman |admin |30/08/56|5400
6213|karuna ganguly |g.m. |accounts |05/06/62|6300
4290|jayant chaoudhary |executive|production|07/09/50|6000
6521|lalit chowdary |director |marketing|26/09/45|8200
3212|shyam sakesena |d.g.m. |accounts |12/12/55|6000
3564|sudhir Agrawal |executive|personnel|06/07/47|7500

20 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

2345|j.b. saxena |g.m. |marketing|12/03/45|8000


0110|v.k agrawal |g.m |marketing|31/12/40|9000
0989|shiv doshi |executive|accounts|09/12/89|8721

7. Write a command to replace first occurrence of Surat and Bharuch in each


line
utkarsh@ubuntu:~$ sed 's/surat/bharuch/' filex23
cat filex23
the kernel architecture
surat
hello
sister alice
welcome to Gujarat surat
good morning surat.

the kernel architecture


bharuch
restwell
sister alice
welcome the green city of gujarat(bharuch).
good morning bharuch.

8. Write a command to sort on 3rd to 5th column


utkarsh@ubuntu:~$ sort -t “|”–k3,5 emp.lst

5423|n.k. gupta |chairman |admin |30/08/56|5400


3212|shyam sakesena |d.g.m. |accounts |12/12/55|6000
5678|sumit chakrobarty |d.g.m.|marketing |19/04/43|6000
9654|shivani singh |d.g.m |sales |11/06/99|9891
6521|lalit chowdary |director |marketing|26/09/45|8200

21 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

2365|barun sengupta |director |personnel|11/05/47|7800


9876|jai sharma |director |production|12/03/50|7000
1006|chanchal singhvi |director |sales |03/09/38|6700
0989|shiv doshi |executive|accounts|09/12/89|8721
3564|sudhir Agrawal |executive|personnel|06/07/47|7500
4290|jayant chaoudhary |executive|production|07/09/50|6000
6213|karuna ganguly |g.m. |accounts |05/06/62|6300
2345|j.b. saxena |g.m. |marketing|12/03/45|8000
0110|v.k agrawal |g.m |marketing|31/12/40|9000
2233|a.k shukla |g.m. |sales |12/12/52|6000
2476|anil aggarwal |manager |sales |01/09/59|5000
1265|s.n. dasgupta |manager |sales |12/09/55|5600

9. Write a command to print lines with line number which contain “Marketing”

utkarsh@ubuntu:~$ grep -n "marketing" emp.lst

3:5678|sumit chakrobarty |d.g.m.|marketing |19/04/43|6000


11:6521|lalit chowdary |director |marketing|26/09/45|8200
14:2345|j.b. saxena |g.m. |marketing|12/03/45|8000
15:0110|v.k agrawal |g.m |marketing|31/12/40|9000

B. Write command using grep or egrep for the following

1. Display the lines which are not starting with 2 at the beginning
utkarsh@ubuntu:~$grep "^[^2]" emp.lst
9876|jai sharma |director |production|12/03/50|7000
5678|sumit chakrobarty |d.g.m.|marketing |19/04/43|6000
5423|n.k. gupta |chairman |admin |30/08/56|5400
1006|chanchal singhvi |director |sales |03/09/38|6700
6213|karuna ganguly |g.m. |accounts |05/06/62|6300
1265|s.n. dasgupta |manager |sales |12/09/55|5600
4290|jayant chaoudhary |executive|production|07/09/50|6000

22 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

6521|lalit chowdary |director |marketing|26/09/45|8200


3212|shyam sakesena |d.g.m. |accounts |12/12/55|6000
3564|sudhir Agrawal |executive|personnel|06/07/47|7500
0110|v.k agrawal |g.m |marketing|31/12/40|9000
0989|shiv doshi |executive|accounts|09/12/89|8721
9654|shivani singh |d.g.m |sales |11/06/99|9891

2. Display lines having exactly 50 character of file X1


utkarsh@ubuntu:~$ grep "^.\{50\}$" filex23
use tty to display and change our terminal setting
the way of expression should be easy to understand

3. Count number of blank lines in file X1


cat file1
Hello

Welcome
world
utkarsh@ubuntu:~$ grep –cvP ‘\S’ file1
1
-----OR-----
utkarsh @ubuntu:~$ grep -n '^[ ]*$' file1 | wc–l
1

4. Display lines having at least one * character in file X1


utkarsh@ubuntu:~$ egrep "\*+" filex23
*display all the filename
Hey**
23 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

----OR----
utkarsh@ubuntu:~$grep ‘[/*]’ filex23
*display all the filename
Hey**

5. Display lines from file X1 that containing string “UNIX” or “unix” or “Unix”
utkarsh@ubuntu:~$ egrep 'Unix|unix|Unix' filex23
unix consist of shell and kernel
UNIX OPERATING SYSTEM

6. Display the directory listing


utkarsh@ubuntu:~$ ls -l | grep "^d"
drwxr-xr-x 2 utkarsh utkarsh 4096 Aug 26 07:19 Desktop
drwxr-xr-x 2 utkarsh utkarsh 4096 Aug 26 07:19 Documents
drwxr-xr-x 2 utkarsh utkarsh 4096 Aug 26 07:19 Downloads
drwxrwxr-x 2 utkarsh utkarsh 4096 Aug 28 01:53 kutkarsh
drwxr-xr-x 2 utkarsh utkarsh 4096 Aug 26 07:19 Music
drwxr-xr-x 2 utkarsh utkarsh 4096 Aug 26 07:19 Pictures
drwxr-xr-x 2 utkarsh utkarsh 4096 Aug 26 07:19 Public
drwxr-xr-x 2 utkarsh utkarsh 4096 Aug 26 07:19 Templates
drwxr-xr-x 2 utkarsh utkarsh 4096 Aug 26 07:19 Videos

7. Write a command to print lines with line number which contain ‘Accounts’
utkarsh@ubuntu:~$ grep -n "accounts" emp.lst
7:6213|karuna ganguly |g.m. |accounts |05/06/62|6300
12:3212|shyam sakesena |d.g.m. |accounts |12/12/55|6000
16:0989|shiv doshi |executive|accounts|09/12/89|8721

24 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

8. Write a command to print lines which do not contain ‘Account’


utkarsh@ubuntu:~$ grep -v "accounts" emp.lst
2233|a.k shukla |g.m. |sales |12/12/52|6000
9876|jai sharma |director |production|12/03/50|7000
5678|sumit chakrobarty |d.g.m.|marketing |19/04/43|6000
2365|barun sengupta |director |personnel|11/05/47|7800
5423|n.k. gupta |chairman |admin |30/08/56|5400
1006|chanchal singhvi |director |sales |03/09/38|6700
1265|s.n. dasgupta |manager |sales |12/09/55|5600
4290|jayant chaoudhary |executive|production|07/09/50|6000
2476|anil aggarwal |manager |sales |01/09/59|5000
6521|lalit chowdary |director |marketing|26/09/45|8200
3564|sudhir Agrawal |executive|personnel|06/07/47|7500
2345|j.b. saxena |g.m. |marketing|12/03/45|8000
0110|v.k agrawal |g.m |marketing|31/12/40|9000

9. Write a command to print line 10 to 15


utkarsh@ubuntu:~$ grep -n "." emp.lst | grep " ^1[0-5]"
10:2476|anil aggarwal |manager |sales |01/09/59|5000
11:6521|lalit chowdary |director |marketing|26/09/45|8200
12:3212|shyam sakesena |d.g.m. |accounts |12/12/55|6000
13:3564|sudhir Agrawal |executive|personnel|06/07/47|7500
14:2345|j.b. saxena |g.m. |marketing|12/03/45|8000
15:0110|v.k agrawal |g.m |marketing|31/12/40|9000

10. Write a command to substitute ‘hello’ with ‘hi’


Cat file1

25 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

lsay hello to the world


hello hows you
hello world
welcome

sed's/hello/hi/' file1
lsay hi to the world
hi hows you
hi world
welcome

11. Write a command to display line which starts with ‘The’

utkarsh@ubuntu:~$ grep "^The" filex23


The kernel architecture
The way of expression should be easy to understand

C. Write Commands for the following using sed

1)Display two lines starting from 7th line of file X1


utkarsh@ubuntu:~ $ sed -n "7,8p" emp.lst
6213|karuna ganguly |g.m. |accounts |05/06/62|6300
1265|s.n. dasgupta |manager |sales |12/09/55|5600

2) Display all blank lines between line 20 and 30 of file X1


utkarsh@ubuntu:~ $sed -n '20,30{/^$/p;}' emp.lst

3) Display lines beginning either with alphabet or digit from file x1

26 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

utkarsh@ubuntu:~$ sed -n '/^[0-9a-zA-Z]/p' filex23


1.hello world
10.user program
kernal system calls
45.library function
adminstrative file from
miscellaneous
special files
recorded data
erase text

4) Display the lines that do not contain “Unix”


Cat file1
Hello
Hey world
Unix consists of shell and kernel
Unix operating system
utkarsh@ubuntu:~$ sed -n '/unix/!p' file1
Hello
Hey world

5) Display the lines which are not starting with 2 at the beginning
utkarsh@ubuntu:~$ sed -n '/^2/!p' emp.lst
9876|jai sharma |director |production|12/03/50|7000
5678|sumit chakrobarty |d.g.m.|marketing |19/04/43|6000
5423|n.k. gupta |chairman |admin |30/08/56|5400
1006|chanchal singhvi |director |sales |03/09/38|6700
6213|karuna ganguly |g.m. |accounts |05/06/62|6300

27 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

1265|s.n. dasgupta |manager |sales |12/09/55|5600


4290|jayant chaoudhary |executive|production|07/09/50|6000
6521|lalit chowdary |director |marketing|26/09/45|8200
3212|shyam sakesena |d.g.m. |accounts |12/12/55|6000
3564|sudhir Agrawal |executive|personnel|06/07/47|7500
0110|v.k agrawal |g.m |marketing|31/12/40|9000
0989|shiv doshi |executive|accounts|09/12/89|8721
9654|shivani singh |d.g.m |sales |11/06/99|9891

6) Write a command to replace ‘UNIXOS’ on line 5th to 10th


Cat file23
Hey world
Hello
Bba
UNIXOS – UNIX OPERATING SYSTEM
Bca
UNIXOS
utkarsh@ubuntu:~$ sed '5,10s/UNIXOS/OPERATING SYSTEM/' file23
Hey world
Hello
Bba
OPERATING SYSTEM – UNIX OPERATING SYSTEM
Bca
OPERATING SYSTEM

7) Write a command to display all file name containing only digits in a filename

utkarsh@ubuntu:~$ ls | sed -n '/^[0-9]*$/p'


1904

28 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

2309

8) To list file names consist of only 4 digits.


utkarsh@ubuntu:~$ ls | sed -n '/^[0-9][0-9][0-9][0-9]$/p'
1904
2309

9) To display lines beginning with alphabets of a file x1


utkarsh@ubuntu:~$ cat> file23
good morning
1:heyya
1904 utkarsh
how are you
unix!
utkarsh@ubuntu:~$ sed -n '/^[a-zA-Z]/p' file23
good morning
how are you
unix!

10) To count number of words in line 40 through 60 of file f1.txt


utkarsh@ubuntu:~$ sed -n '10,15p' emp.lst | wc-c
348

11) Display all line before string Unix from file X1


utkarsh@ubuntu:~$ sed -n '/unix$/p' filex23

D. Create 2 files F1 and F2 using vi editor with words


and digits in the contents of the file .F
29 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

1) Write a command which converts all the lowercase letters to uppercase from
F1 file
utkarsh@ubuntu:~$ cat f1
hows you
hello world
12345678
$5%6
unix

utkarsh@ubuntu:~$ tr '[a-z]''[A-Z]' < f1


HOWS YOU
HELLO WORLD
12345678
$5%6
UNIX

2) Write a command that extracts all numeric digits from F1 and redirect the
output to ‘digit.txt’ file
utkarsh@ubuntu:~$ cat f1
hows you
hello world
12345678
$5%6
unix

utkarsh@ubuntu:~$ sed -n '/^[0-9]*$/p' f1> digit.txt


utkarsh@ubuntu:~$ cat digit.txt

30 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

12345678

3) Merge the contents of F1 and F2 and store the sorted output in file F3
utkarsh@ubuntu:~$ cat f1 f2 | sort> f3
utkarsh@ubuntu:~$ cat f3
12345678
183987
$5%6
8273ksjflkd
good going
hello world
heyya
unix
lets do unis$'
hows you

4) Change the word ‘the’ to ‘The’ from line 1 to 10 only.


Cat file23
the gateway of india
Bba
Bca
Welcome
the temple
Msc
the students
utkarsh@ubuntu:~$ sed '1,10s/the/The/' file23
The gateway of india
Bba

31 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

Bca
Welcome
The temple
Msc
The students

5) Find the lines from F1 and F2 files which are same in both files.

utkarsh@ubuntu:~$ comm -12 f1 f2

comm: file 1 is not in sorted order

comm: file 2 is not in sorted order

E.Write commands using awk for the following:

1) Write awk script to display the login ids, their home directories and login
shells from the “/etc/passwd” file
utkarsh@ubuntu:~$ awk -F "."'/user/{print $1,$6,$7}' /etc/passwd
hplip:x:113:7:HPLIP system user,,,:/var/run/hplip:/bin/false

2) Switch the first two fields in each line of a text and put the result in a new
file.
utkarsh@ubuntu:~$ awk -F "|" '/[a-zA-Z]/{print $2,$1}' Emp.lst > f1
utkarsh@ubuntu:~$ cat f1
a.k shukla 2233
jai sharma 9876
sumit chakrobarty 5678
barun sengupta 2365
n.k. gupta 5423
chanchal singhvi 1006
karuna ganguly 6213
s.n. dasgupta 1265

32 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

jayant chaoudhary 4290


anil aggarwal 2476
lalit chowdary 6521
shyam sakesena 3212
sudhir Agrawal 3564
j.b. saxena 2345
v.k agrawal 0110
shiv doshi 0989
shivani singh 9654

3) To only print lines wherein the first field had a numeric value of less than 20
utkarsh@ubuntu:~$ awk -F "|" '$1<2000 {print $0}' Emp.lst
1006|chanchal singhvi |director |sales |03/09/38|6700
1265|s.n. dasgupta |manager |sales |12/09/55|5600
0110|v.k agrawal |g.m |marketing|31/12/40|9000
0989|shiv doshi |executive|accounts|09/12/89|8721

4) Print even no’s of words in each line


utkarsh@ubuntu:~$ grep "^2" Emp.lst
2233|a.k shukla |g.m. |sales |12/12/52|6000
2365|barun sengupta |director |personnel|11/05/47|7800
2476|anil aggarwal |manager |sales |01/09/59|5000
2345|j.b. saxena |g.m. |marketing|12/03/45|8000

5) Count occurrences of pattern ‘operating system’ in file f11


utkarsh@ubuntu:~$ awk -F"|" '/operating system/{kount++}
END{print kount}' p9
2

6) Display those words whose length greater than 10 characters and consist of
alphabet only
utkarsh@ubuntu:~$ awk '/manager/{count++} END {print count}' Emp.lst
2

7) Print odd numbers of words in each line.


utkarsh@ubuntu:~$ awk '{x=NR %2;if(x==1)print}' f1

33 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

2233 a.k shukla


5678 sumit chakrobarty
5423 n.k. gupta
6213 karuna ganguly
4290 jayant chaoudhary
6521 lalit chowdary
3564 sudhir Agrawal
0110 v.k agrawal
9654 shivani singh

8) Print lines which end with 5,6,7 from file f1


utkarsh@ubuntu:~$ awk '/[5-7]$/{print}' p1
hope you are fine5
123467
5

9) Write a script to print 1 to 10 nos


utkarsh@ubuntu:~$ awk 'BEGIN{for(i=1;i<=10;i++)print i}'
1
2
3
4
5
6
7
8
9
10

10) Write a command to print those lines where field2 is computer field 3 >
15000 from sales file
utkarsh@ubuntu:~$ awk -F"|" '$6 > 15000 || $4 == "computer" {
printf " %d %-20s\n",$6,$4}' Emp.lst
20000 computer
15600 computer

11) Print lines no 18 to 30 from file f1.txt


utkarsh@ubuntu:~$ awk -F "|" 'NR==3,NR==8 {print NR,$0}' Emp.lst

34 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

3 5678|sumit chakrobarty |d.g.m.|marketing |19/04/43|6000


4 2365|barun sengupta |director |personnel|11/05/47|7800
5 5423|n.k. gupta |chairman |admin |30/08/56|5400
6 1006|chanchal singhvi |director |sales |03/09/38|6700
7 6213|karuna ganguly |g.m. |accounts |05/06/62|6300
8 1265|s.n. dasgupta |manager |sales |12/09/55|5600

12) Count the total no. of lines in a file.


utkarsh@ubuntu:~$ awk 'END {print NR}' Emp.lst
17

14) Print the fields 2,3,4, and 6 from test.txt which contains the pattern ‘comp’
krishna@ubuntu:~$ awk '/[fFoOrR]/{print}' test.txt
for submission
FOR admission
For registration
fOr login

15) Prints all lines containing string “for” in test.txt file


krishna@ubuntu:~$ awk -F "|" '$4 == "comp"
> {printf "%-20s %12s %-10s %d\n", $2,$3,$4,$6}' Emp.lst
a.k shukla g.m. sales 6000
jai sharma director production 7000
sumit chakrobarty d.g.m. marketing 6000
barun sengupta director personnel 7800
n.k. gupta chairman admin 5400
chanchal singhvi director sales 6700
karuna ganguly g.m. accounts 6300
s.n. dasgupta manager sales 5600
jayant chaoudhary executive production 6000
anil aggarwal manager sales 5000
lalit chowdary director marketing 8200
shyam sakesena d.g.m. accounts 6000
sudhir Agrawal executive comp 20000
j.b. saxena g.m. marketing 8000
v.k agrawal g.m marketing 9000
shiv doshi executive computer 15600
shivani singh d.g.m comp 9891

35 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

PRACTICAL 01

36 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

PRACTICAL 02

37 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

PRACTICAL 03

38 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)

PRACTICAL 04

39 | P a g e

You might also like