You are on page 1of 27

RAJALAKSHMI ENGINEERING COLLEGE

Rajalakshmi Nagar, Thandalam, Chennai 602 105

Department of Computer Science and Engineering

GE2155 Computer Practice Lab - II UNIX Commands Sample Q & A


Prepared by

B.BHUVANESWARAN
Senior Lecturer / CSE

GE2155 Computer Practice Lab-II UNIX Commands 1. a. Write and execute the following UNIX commands (i) Create a file. -bash-3.2$ cat > sample.txt Computer Practice Lab-II. Department of CSE. Rajalakshmi Engineering College. Chennai. (ii) Display only the first two characters of all the lines from a file. -bash-3.2$ cut -c1-2 sample.txt Co De Ra Ch (iii) Convert the lower case to uppercase. -bash-3.2$ tr 'a-z' 'A-Z' < sample.txt COMPUTER PRACTICE LAB-II. DEPARTMENT OF CSE. RAJALAKSHMI ENGINEERING COLLEGE. CHENNAI. (iv) Combine two strings. -bash-3.2$ sh sh-3.2$ a="REC" sh-3.2$ b="RIT" sh-3.2$ c=$a$b sh-3.2$ echo $c RECRIT (v) Display the file contents in descending order. sh-3.2$ sort -r sample.txt Rajalakshmi Engineering College. Department of CSE. Computer Practice Lab-II. Chennai.

BHUVANESWARAN B / SL / CSE / REC - 2

2. a. Write and execute the following UNIX commands (i) Create two files with the name of name.txt, which contain only names, and reg.txt with the content of register number respectively. -bash-3.2$ cat > name.txt ABARNA B ABHINANDAN SESHADRI R ABINASH R ABIRAMI K ABISHEK A -bash-3.2$ cat > reg.txt 211611104001 211611104002 211611104003 211611104004 211611104005 -bash-3.2$ (ii) Combine the two files in the form of register number followed by name -bash-3.2$ paste reg.txt name.txt 211611104001 ABARNA B 211611104002 ABHINANDAN SESHADRI R 211611104003 ABINASH R 211611104004 ABIRAMI K 211611104005 ABISHEK A (iii) Sort the two files in ascending order -bash-3.2$ sort name.txt ABARNA B ABHINANDAN SESHADRI R ABINASH R ABIRAMI K ABISHEK A -bash-3.2$ sort reg.txt 211611104001 211611104002 211611104003 211611104004 211611104005

BHUVANESWARAN B / SL / CSE / REC - 3

(iv) Count the number of lines in the files -bash-3.2$ cat name.txt | wc -l 5 -bash-3.2$ cat reg.txt | wc -l 5 (v) Rename the two files. -bash-3.2$ mv reg.txt regrec.txt -bash-3.2$ mv name.txt namerec.txt

BHUVANESWARAN B / SL / CSE / REC - 4

3. a. Write and execute the following UNIX commands (i) Create two files. -bash-3.2$ cat > rec.txt Rajalakshmi Engineering College. B.E. B.Tech. M.E. M.Tech. M.B.A. M.C.A. M.S. Ph.D. -bash-3.2$ cat > rit.txt Rajalakshmi Institute of Technology. B.E. B.Tech. M.B.A. M.C.A. (ii) Combine the two files. sh-3.2$ cat rec.txt rit.txt > merge.txt (iii) Search a specific word from any one of the file. sh-3.2$ grep Ph.D. rec.txt Ph.D. (iv) Search a specific file from a directory. sh-3.2$ find rit.txt rit.txt (v) Display the common and distinct line of contents from a file. sh-3.2$ diff rec.txt rit.txt 1c1 < Rajalakshmi Engineering College. --> Rajalakshmi Institute of Technology. 4,5d3 < M.E. < M.Tech. 8,9d5 < M.S. < Ph.D. BHUVANESWARAN B / SL / CSE / REC - 5

-bash-3.2$ cat rec.txt | sort > recsort.txt -bash-3.2$ cat rit.txt | sort > ritsort.txt -bash-3.2$ comm -1 -2 recsort.txt ritsort.txt B.E. B.Tech. M.B.A. M.C.A. -bash-3.2$ cat rit.txt Rajalakshmi Institute of Technology. B.E. B.Tech. M.B.A. M.C.A.

BHUVANESWARAN B / SL / CSE / REC - 6

4. a. Write and execute the following UNIX commands (i) Create two files. -bash-3.2$ cat > rec.txt Rajalakshmi Engineering College. B.E. B.Tech. M.E. M.Tech. M.B.A. M.C.A. M.S. Ph.D. -bash-3.2$ cat > rit.txt Rajalakshmi Institute of Technology. B.E. B.Tech. M.B.A. M.C.A. (ii) Display the contents of both the files. -bash-3.2$ cat rec.txt Rajalakshmi Engineering College. B.E. B.Tech. M.E. M.Tech. M.B.A. M.C.A. M.S. Ph.D. -bash-3.2$ cat rit.txt Rajalakshmi Institute of Technology. B.E. B.Tech. M.B.A. M.C.A. (iii) Count the number of characters in both the files. -bash-3.2$ cat rec.txt | wc -c 84 -bash-3.2$ cat rit.txt | wc -c 64 BHUVANESWARAN B / SL / CSE / REC - 7

(iv) Rename the two files. -bash-3.2$ mv rec.txt engg.txt -bash-3.2$ mv rit.txt inst.txt (v) Combine the two files without duplicate. -bash-3.2$ sort rec.txt rit.txt | uniq > merge.txt -bash-3.2$ cat merge.txt B.E. B.Tech. M.B.A. M.C.A. M.E. M.S. M.Tech. Ph.D. Rajalakshmi Engineering College. Rajalakshmi Institute of Technology.

BHUVANESWARAN B / SL / CSE / REC - 8

5. a. Write and execute the following UNIX commands (i) To view all the files and directories page by page -bash-3.2$ ls -l | more (ii) To view only the directories page by page. -bash-3.2$ ls -d */ | more (iii) To view only the files in a directory. -bash-3.2$ -rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r-ls -l | grep ^1 bhuvan bhuvan 1 bhuvan bhuvan 1 bhuvan bhuvan 1 bhuvan bhuvan 1 bhuvan bhuvan 1 bhuvan bhuvan 1 bhuvan bhuvan 1 bhuvan bhuvan

51 84 64 61 84 65 64 85

May May May May May May May May

9 9 9 9 9 9 9 9

16:09 16:12 16:17 15:32 16:20 15:32 16:20 15:20

degrees.txt engg.txt inst.txt namerec.txt rec.txt regrec.txt rit.txt sample.txt

(iv) Display the working directory. -bash-3.2$ pwd /home/bhuvan (v) Display the complete path of the working directory. -bash-3.2$ pwd -P /home/bhuvan

BHUVANESWARAN B / SL / CSE / REC - 9

6. a. Write and execute the following UNIX commands (i) Display the calendar. -bash-3.2$ cal 2012
2012 January Tu We Th 3 4 5 10 11 12 17 18 19 24 25 26 31 April Tu We Th 3 4 5 10 11 12 17 18 19 24 25 26 February Su Mo Tu We Th 1 2 5 6 7 8 9 12 13 14 15 16 19 20 21 22 23 26 27 28 29 May We Th 2 3 9 10 16 17 23 24 30 31 March Su Mo Tu We Th 1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 28 29

Su 1 8 15 22 29

Mo 2 9 16 23 30

Fr 6 13 20 27

Sa 7 14 21 28

Fr 3 10 17 24

Sa 4 11 18 25

Fr 2 9 16 23 30

Sa 3 10 17 24 31

Su 1 8 15 22 29

Mo 2 9 16 23 30

Fr 6 13 20 27

Sa 7 14 21 28

Su Mo Tu 1 6 7 8 13 14 15 20 21 22 27 28 29

Fr 4 11 18 25

Sa 5 12 19 26

June Su Mo Tu We Th Fr 1 3 4 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 24 25 26 27 28 29

Sa 2 9 16 23 30

Su 1 8 15 22 29

Mo 2 9 16 23 30

July Tu We Th 3 4 5 10 11 12 17 18 19 24 25 26 31 October Tu We Th 2 3 4 9 10 11 16 17 18 23 24 25 30 31

Fr 6 13 20 27

Sa 7 14 21 28

August Su Mo Tu We Th 1 2 5 6 7 8 9 12 13 14 15 16 19 20 21 22 23 26 27 28 29 30 November Su Mo Tu We Th 1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 28 29

Fr 3 10 17 24 31

Sa 4 11 18 25

Su Mo 1 7 8 14 15 21 22 28 29

Fr 5 12 19 26

Sa 6 13 20 27

Fr 2 9 16 23 30

Sa 3 10 17 24

September 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 December 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

(ii) Execute more than one command at a time (using semi-colon ;) -bash-3.2$ whoami ; date bhuvan Wed May 9 15:37:14 IST 2012 (iii) Display the users full details. -bash-3.2$ finger bhuvan Login: bhuvan Name: (null) Directory: /home/bhuvan Shell: /bin/bash On since Wed May 9 15:17 (IST) on pts/1 from 172.16.2.222 No mail. No Plan.

BHUVANESWARAN B / SL / CSE / REC - 10

(iv) Display the user-id / Group-id. -bash-3.2$ id uid=508(bhuvan) gid=508(bhuvan) groups=508(bhuvan) (v) Execute more than one command at a time (using logical-AND &&). -bash-3.2$ whoami && date bhuvan Wed May 9 15:38:24 IST 2012

BHUVANESWARAN B / SL / CSE / REC - 11

7. a. Write and execute the following UNIX commands (i) Demonstrate pipe command -bash-3.2$ who | wc -l 26 (ii) Demonstrate tee command -bash-3.2$ who | tee result | wc -l 26 -bash-3.2$ cat result (iii) Use more than one command at a time (who and date) -bash-3.2$ who ; date bhuvan pts/1 2012-05-09 15:17 (172.16.2.222) exam04099 pts/2 2012-05-09 16:26 (172.16.9.109) exam04116 pts/3 2012-05-09 16:30 (172.16.9.9) bhuvan pts/4 2012-05-09 16:34 (172.16.2.222) exam02007 pts/23 2012-05-09 14:12 (172.16.9.200) root :0 2012-05-09 13:33 exam04048 pts/21 2012-05-09 14:58 (172.16.9.167) exam02028 pts/45 2012-05-09 14:34 (172.16.9.184) exam04046 pts/46 2012-05-09 14:54 (172.16.9.181) exam02003 pts/51 2012-05-09 15:02 (172.16.9.201) exam04040 pts/48 2012-05-09 14:59 (172.16.9.156) exam02031 pts/58 2012-05-09 15:03 (172.16.9.189) exam04040 pts/56 2012-05-09 15:03 (172.16.9.156) exam02009 pts/59 2012-05-09 15:04 (172.16.9.173) exam02015 pts/61 2012-05-09 15:04 (172.16.9.210) exam04060 pts/62 2012-05-09 15:05 (172.16.9.156) exam04111 pts/5 2012-05-09 16:34 (172.16.9.151) root pts/6 2012-05-09 17:05 (:0.0) Wed May 9 17:10:32 IST 2012 (iv) Create a file called create.txt -bash-3.2$ cat > create.txt Computer Practice Lab-II. (v) Move the file create.txt to move.txt -bash-3.2$ mv create.txt move.txt

BHUVANESWARAN B / SL / CSE / REC - 12

8. a. Write and execute the following UNIX commands (i) Create a file -bash-3.2$ cat > rec.txt Rajalakshmi Engineering College. B.E. B.Tech. M.E. M.Tech. M.B.A. M.C.A. M.S. Ph.D. (ii) Print the specified number of lines of a file from start to end of the file. -bash-3.2$ head -5 rec.txt Rajalakshmi Engineering College. B.E. B.Tech. M.E. M.Tech. (iii) Display the file content with line number -bash-3.2$ nl rec.txt 1 Rajalakshmi Engineering College. 2 B.E. 3 B.Tech. 4 M.E. 5 M.Tech. 6 M.B.A. 7 M.C.A. 8 M.S. 9 Ph.D. (iv) Update the file content with I/O redirection -bash-3.2$ cat >> rec.txt B.Arch. (v) Rename the file -bash-3.2$ mv rec.txt rajalakshmi.txt

BHUVANESWARAN B / SL / CSE / REC - 13

9. a. Write and execute the following UNIX commands (i) Create a directory -bash-3.2$ mkdir Exam (ii) Create a file -bash-3.2$ cat > cs1101.txt Computer Practice Lab-II. (iii) Rename the directory -bash-3.2$ mv Exam Test (iv) Rename the file -bash-3.2$ mv cs1101.txt cs1102.txt (v) View the file -bash-3.2$ cat cs1102.txt Computer Practice Lab-II.

BHUVANESWARAN B / SL / CSE / REC - 14

10. a. Write and execute the following UNIX commands (i) Create a file -bash-3.2$ cat > cs1101.txt Computer Practice Lab-II. (ii) Update the file -bash-3.2$ cat >> cs1101.txt Common to All B.E. and B.Tech. (iii) Display the file content -bash-3.2$ cat cs1101.txt Computer Practice Lab-II. Common to All B.E. and B.Tech. (iv) Display the file with line number -bash-3.2$ nl cs1101.txt 1 Computer Practice Lab-II. 2 Common to All B.E. and B.Tech. (v) Count the number of words in the file -bash-3.2$ cat cs1101.txt | wc -w 8

BHUVANESWARAN B / SL / CSE / REC - 15

11. a. Write and execute the following UNIX commands (i) Create a Directory called main -bash-3.2$ mkdir main (ii) Create a sub-directory called sub in the main directory -bash-3.2$ cd main -bash-3.2$ mkdir sub (iii) Create a file in the main directory -bash-3.2$ cat > rec.txt Rajalakshmi Engineering College. B.E. B.Tech. M.E. M.Tech. M.B.A. M.C.A. M.S. Ph.D. (iv) Copy the file to the sub directory -bash-3.2$ cp rec.txt sub (v) Delete the file in the main directory -bash-3.2$ rm rec.txt

BHUVANESWARAN B / SL / CSE / REC - 16

12. a. Write and execute the following UNIX commands (i) Display the calendar -bash-3.2$ cal 2012
2012 January Tu We Th 3 4 5 10 11 12 17 18 19 24 25 26 31 April Tu We Th 3 4 5 10 11 12 17 18 19 24 25 26 February Su Mo Tu We Th 1 2 5 6 7 8 9 12 13 14 15 16 19 20 21 22 23 26 27 28 29 May We Th 2 3 9 10 16 17 23 24 30 31 March Su Mo Tu We Th 1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 28 29

Su 1 8 15 22 29

Mo 2 9 16 23 30

Fr 6 13 20 27

Sa 7 14 21 28

Fr 3 10 17 24

Sa 4 11 18 25

Fr 2 9 16 23 30

Sa 3 10 17 24 31

Su 1 8 15 22 29

Mo 2 9 16 23 30

Fr 6 13 20 27

Sa 7 14 21 28

Su Mo Tu 1 6 7 8 13 14 15 20 21 22 27 28 29

Fr 4 11 18 25

Sa 5 12 19 26

June Su Mo Tu We Th Fr 1 3 4 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 24 25 26 27 28 29

Sa 2 9 16 23 30

Su 1 8 15 22 29

Mo 2 9 16 23 30

July Tu We Th 3 4 5 10 11 12 17 18 19 24 25 26 31 October Tu We Th 2 3 4 9 10 11 16 17 18 23 24 25 30 31

Fr 6 13 20 27

Sa 7 14 21 28

August Su Mo Tu We Th 1 2 5 6 7 8 9 12 13 14 15 16 19 20 21 22 23 26 27 28 29 30 November Su Mo Tu We Th 1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 28 29

Fr 3 10 17 24 31

Sa 4 11 18 25

Su Mo 1 7 8 14 15 21 22 28 29

Fr 5 12 19 26

Sa 6 13 20 27

Fr 2 9 16 23 30

Sa 3 10 17 24

September 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 December 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

(ii) Display the date and time -bash-3.2$ date Wed May 9 16:56:28 IST 2012 (iii) Display the present working directory -bash-3.2$ pwd /home/bhuvan (iv) Display your user name -bash-3.2$ whoami Bhuvan

BHUVANESWARAN B / SL / CSE / REC - 17

(v) Create a file using cat command -bash-3.2$ cat > rec.txt Rajalakshmi Engineering College. B.E. B.Tech. M.E. M.Tech. M.B.A. M.C.A. M.S. Ph.D.

BHUVANESWARAN B / SL / CSE / REC - 18

13. a. Write and execute the following UNIX commands (i) Use finger command -bash-3.2$ finger bhuvan Login: bhuvan Name: (null) Directory: /home/bhuvan Shell: /bin/bash On since Wed May 9 15:17 (IST) on pts/1 from 172.16.2.222 15 minutes 59 seconds idle On since Wed May 9 16:34 (IST) on pts/4 from 172.16.2.222 No mail. No Plan. (ii) Use grep command -bash-3.2$ cat > rec.txt Rajalakshmi Engineering College. B.E. B.Tech. M.E. M.Tech. M.B.A. M.C.A. M.S. Ph.D. -bash-3.2$ grep M. rec.txt M.E. M.Tech. M.B.A. M.C.A. M.S. (iii) Use diff command -bash-3.2$ diff rec.txt rit.txt 1c1 < Rajalakshmi Engineering College. --> Rajalakshmi Institute of Technology. 4,5d3 < M.E. < M.Tech. 8,9d5 < M.S. < Ph.D.

BHUVANESWARAN B / SL / CSE / REC - 19

(iv) Use uniq command -bash-3.2$ cat > namelist.txt ARUN A ARUN A ARUN S BABU S BABU R BABU R CHITHRA S -bash-3.2$ uniq namelist.txt ARUN A ARUN S BABU S BABU R CHITHRA S (v) Use cmp command -bash-3.2$ cmp rec.txt rit.txt rec.txt rit.txt differ: byte 13, line 1

BHUVANESWARAN B / SL / CSE / REC - 20

14. a. Write and execute the following UNIX commands (i) Create a directory -bash-3.2$ mkdir Exam (ii) View all directories from a user -bash-3.2$ ls -d */ CS1101/ CS1102/ Test/ (iii) Rename a directory -bash-3.2$ mv Exam Test (iv) View all the directories starting with a specific character. -bash-3.2$ ls -d C*/ CS1101/ CS1102/ (v) View only the hidden directories -bash-3.2$ ls -d .*/ ./ ../

BHUVANESWARAN B / SL / CSE / REC - 21

15. a. Write and execute the following UNIX commands (i) Create a file -bash-3.2$ cat > exam.txt Anna University Chennai Practicals-May/June-2012 (ii) View only files from a directory -bash-3.2$ -rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r-ls -l | grep ^1 bhuvan bhuvan 1 bhuvan bhuvan 1 bhuvan bhuvan 1 bhuvan bhuvan 1 bhuvan bhuvan 1 bhuvan bhuvan 1 bhuvan bhuvan 1 bhuvan bhuvan

51 84 64 61 84 65 64 85

May May May May May May May May

9 9 9 9 9 9 9 9

16:09 16:12 16:17 15:32 16:20 15:32 16:20 15:20

degrees.txt engg.txt inst.txt namerec.txt rec.txt regrec.txt rit.txt sample.txt

(iii) Rename a file -bash-3.2$ mv exam.txt test.txt (iv) View all files starting with a specific character -bash-3.2$ ls r* rec.txt regrec.txt

rit.txt

(v) View all the files which has a specific extension -bash-3.2$ ls *.txt degrees.txt inst.txt engg.txt namerec.txt

rec.txt regrec.txt

rit.txt sample.txt

BHUVANESWARAN B / SL / CSE / REC - 22

16. a. Write and execute the following UNIX commands (i) Create a directory with the name of Exam -bash-3.2$ mkdir Exam (ii) Change the Exam directory as the working directory -bash-3.2$ cd Exam (iii) Create a file called exam.txt in the Exam directory -bash-3.2$ cat > exam.txt Anna University Chennai Practicals-May/June-2012 (iv) View the content of the exam.txt file -bash-3.2$ cat exam.txt Anna University Chennai Practicals-May/June-2012 (v) Rename the file exam.txt to test.txt -bash-3.2$ mv exam.txt test.txt

BHUVANESWARAN B / SL / CSE / REC - 23

17. a. Write and execute the following UNIX commands (i) To check the working directory -bash-3.2$ pwd /home/bhuvan (ii) Change the password -bash-3.2$ passwd Changing password for user bhuvan. Changing password for bhuvan (current) UNIX password: New UNIX password: Retype new UNIX password: passwd: all authentication tokens updated successfully. (iii) To display todays date -bash-3.2$ date +%D 05/09/12 (iv) To display the calendar of this month -bash-3.2$ cal May 2012 Su Mo Tu We Th 1 2 3 6 7 8 9 10 13 14 15 16 17 20 21 22 23 24 27 28 29 30 31 5 2012 Fr 4 11 18 25 Sa 5 12 19 26

(v) To display a message University Exam using a special character -bash-3.2$ banner "University Exam"

BHUVANESWARAN B / SL / CSE / REC - 24

18. a. Write and execute the following UNIX commands (i) Create two directories called CS1101 and CS1102 -bash-3.2$ mkdir CS1101 -bash-3.2$ mkdir CS1102 (ii) Create a file called cs1101.txt in the CS1101 directory -bash-3.2$ cd CS1101 -bash-3.2$ cat > cs1101.txt Computer Practice Lab-II. (iii) Copy the file cs1101.txt from CS1101 to CS1102 -bash-3.2$ cp cs1101.txt ../CS1102 (iv) Update the file content of cs1101.txt in the CS1102 directory -bash-3.2$ cd .. -bash-3.2$ cd CS1102 -bash-3.2$ cat >> cs1101.txt Common to All B.E. and B.Tech. -bash-3.2$ cat cs1101.txt Computer Practice Lab-II. Common to All B.E. and B.Tech. (v) Rename the file in the CS1102 with cs1102.txt -bash-3.2$ mv cs1101.txt cs1102.txt

BHUVANESWARAN B / SL / CSE / REC - 25

19. a. Write and execute the following UNIX commands (i) Create a file -bash-3.2$ cat > sample.txt Computer Practice Lab-II. Department of CSE. Rajalakshmi Engineering College. Chennai. (ii) Rename a file -bash-3.2$ mv sample.txt test.txt (iii) Change the mode of the file to read only -bash-3.2$ chmod ugo+r-w test.txt (iv) View the content of a file -bash-3.2$ cat test.txt Computer Practice Lab-II. Department of CSE. Rajalakshmi Engineering College. Chennai. (v) Count the number of words in a file -bash-3.2$ cat test.txt | wc -l 4

BHUVANESWARAN B / SL / CSE / REC - 26

20. a. Write and execute the following UNIX commands (i) Count the specific number of lines from a file start from the beginning of the file -bash-3.2$ head -5 rec.txt | wc -l 5 (ii) To destroy a specific file from a user directory -bash-3.2$ rm rec.txt (iii) Send a greeting message to other user -bash-3.2$ write exam10001 Happy Birthday to you... [exam10001@sys ~]$ Message from bhuvan@sys.server.com on pts/2 at 09:00 ... Happy Birthday to you... EOF (iv) Execute a shell program -bash-3.2$ sh filename.sh (v) To display current time -bash-3.2$ date +%T 16:05:08

BHUVANESWARAN B / SL / CSE / REC - 27

You might also like