Unix Command Practice Assignment – 2


Assignment – 2 1)Create a file containing some text Display the first line of the file containing the string “Good Day” Ans: [cmsa1@localhost ~]$ grep "good day" a11.txt 2)List all files in the present directory whose 2nd character is a digit. Ans: [cmsa1@localhost ~]$ ls ?[0-9]* 3)Compute prime factors of 28 Ans: [cmsa1@localhost ~]$ factor 28 4) Display all lines in a file which contain the word ‘Best’ from a file a11.txt. Your command should report all occurrences of the word like Best,BeSt,BESt etc. Ans: [cmsa1@localhost ~]$ grep -i "best" a11.txt 5)A file contains some records with record containing Name of city, Name of state,Name of country. Sort the file with ‘Name of stete’ as sort key. Ans: [cmsa1@localhost ~]$ sort -t "|" -k 3 state.txt Here state.txt is the file and 3rd field is name of state. 6)Create a regular file. Write command to remove all permissions from the file. Ans: [cmsa1@localhost ~]$ touch f1 [cmsa1@localhost ~]$ chmod 000 f1 Here f1 is the regular file whose all permissions are removed. 7)Combine the contents of the file t1 and t2 into another file t1t2. Ans: [cmsa1@localhost ~]$ cat t1 t2 > s1s2 8)Convert all uppercase letters in a file f1 to lowercase letters. Ans: [cmsa1@localhost ~]$ cat f1.txt | tr "[a-z]" "[A-Z]" 9)Merge and sort the contents of three text files (say a,b,c) and display the sorted ouput on the screen. Ans: [cmsa1@localhost ~]$ cat a.txt b.txt c.txt | sort 10)List all lines in a file, which do not end with a semicolon. Ans: [cmsa1@localhost ~]$ grep -v ";"$ s123.txt Here s123.txt is the file. 11)Display the list of last 15 files present in the current directory. Ans: [cmsa1@localhost ~]$ find . -maxdepth 1 -type f|tail -15 12)Redirect the sorted output of names of currently logged in users to a file. Ans: [cmsa1@localhost ~]$ who|cut -d " " -f 1|sort|uniq > users1.txt 13) A file contains a word ‘district’ in some lines. Redirect those lindes to a file bbb. Ans: [cmsa1@localhost ~]$ grep -w "district" s1234.txt > bbb 14) Redirect the output of the command pwd and date in succession to a file hhh. Ans: [cmsa1@localhost ~]$ (pwd;date) > res1.txt [cmsa1@localhost ~]$ cat res1.txt /home/cmsa1 Thu Sep 12 13:57:07 IST 2016 15) There are 2 text files. Write a command to display the total no of words in both the files. Ans: [cmsa1@localhost ~]$ cat s1.txt s2.txt | wc –w 16)Move the files p1 and p2 to the directory Dest. Ans: [cmsa1@localhost ~]$ mv p1.txt p9.txt dest 17) List the content of the 3 files t1 , t2 and t3 in a single command. Ans: [cmsa1@localhost ~]$ cat t1.txt t2.txt t3.txt 18) Output the contents of the fields 1 and 3 from a text file student where delimeter between fields is ‘:’ Ans: [cmsa1@localhost ~]$ cut -d ':' -f 1,3 s5.txt 19) Find out number of files in a directory. Ans: [cmsa1@localhost ~]$ find . -maxdepth 1 -type f | wc –l 20)Redirect the number of lines of a file to a file sss. Ans: [cmsa1@localhost ~]$ cat s1.txt | wc -l > sss 21) List all filenames starting with a or b or s. Ans: [cmsa1@localhost ~]$ ls [abs]* 22)Report no of lines containing a given number say 60, in all files in the current directory. Ans: [cmsa1@localhost ~]$ grep –w “60” ./* | wc -1 23)List first 10 lines of a given file. Ans: [cmsa1@localhost ~]$ head -10 mycat.c 24) Create 3 empty files f1 , f2, f3. Ans: [cmsa1@localhost ~]$ touch f1 f2 f3 25) Create a file with timestamp 15 th august 2012 at 12:40 hours. Ans: [cmsa1@localhost ~]$ touch -t 1208151240 abc1 Here abc1 is the name of the file.

Share:

0 comments