Professional Documents
Culture Documents
Utkarsh Unix
Utkarsh Unix
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.
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
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)
student@ubuntu:~$ uname
output:Linux
student@ubuntu:~$ tty
output:/dev/pts/18
student@ubuntu:~$ pwd
output:/home/student
2|Page
UNIX UTKARSH SENGAR
T.Y .BCA (42)
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
3|Page
UNIX UTKARSH SENGAR
T.Y .BCA (42)
student@ubuntu:~/TYBCA42$ wc favmoviefile
output: 1 2 14 favmoviefile
student@ubuntu:~/TYBCA42$ wc -c placefile
output:13 placefile
student@ubuntu:~/TYBCA42$ wc -l foodfile
output: 2 foodfile
student@ubuntu:~/TYBCA42$ wc -w favmoviefile
output:2 favmoviefile
19. Create two new files names Stud1 and Stud2 containing names of students
4|Page
UNIX UTKARSH SENGAR
T.Y .BCA (42)
21. Find out what is common between Stud1 and Stud2 files
22. Convert one file type to other (use dos2unix and unix2dos cmds)
student@ubuntu:~/TYBCA42$ dos2unix <stud1
student@ubuntu:~/TYBCA42$ unix2dos <stud2
student@ubuntu:~/TYBCA42$ ls
output:favfoodfile favmoviefile foodfile placefile stud1 stud2
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
5|Page
UNIX UTKARSH SENGAR
T.Y .BCA (42)
27. Give all users write permission for stud1 file (use Absolute permission)
28. Write command to display file that has recorded 1 st to 7 the questions.
student@ubuntu:~/TYBCA42$ date
output:Wed Jun 26 03:15:27 PDT 2019
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)
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
student@ubuntu:~/TYBCA42$ pwd
/home/student/TYBCA42
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)
39. Create 5 new files inside pattern directory (name the files as - unix1, unix02,
unix3,funix1, unix2)
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
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
8|Page
UNIX UTKARSH SENGAR
T.Y .BCA (42)
a)student@ubuntu:~/TYBCA42$ expr 10 % -4
output:2
d)student@ubuntu:~/TYBCA42$ ls [?0-9]?[a-z]*
0ab9 0unix
9|Page
UNIX UTKARSH SENGAR
T.Y .BCA (42)
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
hello
hello world
unix1
unix1
it is an operating system
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)
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
student@ubuntu:~/TYBCA42/mya$ rm -f mywork
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
11 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)
12 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)
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$ tail -n 1 X1
0110|v.k.agarwal |g.m |marketing |05/09/62|9000
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$ 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
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
25. Display all line that start with ‘let’ from file x1.1, e and t may be in any case
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)
29. Count the frequency of users who are logged in from more than one terminal
student@ubuntu:~/TYBCA42$ head -n 10 X1
32. Write a command to sort a line of file and also remove repeated line
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)
35. Replace ‘kernel’ with ‘kernel architecture’ using remember pattern of sed utility.
student@ubuntu:~/TYBCA42$ at 9:00am
student@ubuntu:~/TYBCA42$ ps
PID TTY TIME CMD
1815 pts/1 00:00:00 bash
1883 pts/1 00:00:00 ps
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
student@ubuntu:~/TYBCA42$ ls –l|grep”[0-9]$”
17 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)
18 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)
19 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)
20 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)
21 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)
9. Write a command to print lines with line number which contain “Marketing”
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)
Welcome
world
utkarsh@ubuntu:~$ grep –cvP ‘\S’ file1
1
-----OR-----
utkarsh @ubuntu:~$ grep -n '^[ ]*$' file1 | wc–l
1
----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
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)
25 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)
sed's/hello/hi/' file1
lsay hi to the world
hi hows you
hi world
welcome
26 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)
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)
7) Write a command to display all file name containing only digits in a filename
28 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)
2309
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
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
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
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.
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)
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
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
33 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)
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
34 | P a g e
UNIX UTKARSH SENGAR
T.Y .BCA (42)
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
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