Ravi Tutorials Provide Project Training

Unix Tutorial3 Ans


Unix Tutorial3 Solved


1. Write a command to display content of top three largest file in a working directory.
cat `ls -lS | grep -v '^d' | head -n 4 | tail -n 3 | tr -s ' ' | cut -d ' ' -f 9`

2. Count no of words in lines 40 thrugh 60 of file f1.txt
sed -n '4,8p' t1 | wc -w

3. Display all filenames not begining with "."
find . -maxdepth 1 -type f ! -name '.*' -print

4.Delete all special characters from file f1.
tr -cd '[a-zA-Z0-9 \n]' < t1

5.Display i-node no of all files of current directory.
find . -maxdepth 1 -type f -ls

6.Display those lines of file f1 that contains exactly 50 characters in it.
grep -E '^.{13}$' t1

7. Replace "hello" with "HELLO" in input file fin.sh and write those lines to output file fout.sh.
tr 'hello' 'HELLO' < t1 > fout.sh

8.Extract all usernames and their home directories from /etc/passwd.
cat /etc/passwd | cut -d ':' -f 1,6

9.Locate lines of file where the second and second last character of the lines are same.
grep  '^.\(.\).*\1.$' t1

10. Display all lines of files that contain "hello" pattern in it.
grep 'hello'  `grep -l 'hello' *`

11. Display all lines having "g*" pattern in it.
grep 'g\*' t1

12.Change the modification time of file to Dec 25, 10:30 AM
touch -t 12251030 t1

13.List all files of working directory having atleast 4 characters in filename.
find . -maxdepth 1 -type f -name '????*' -print

14. Execute a command to run a shellscript hello.sh at tea time.
at teatime
warning: commands will be executed using /bin/sh
at> sh hello.sh
at> <EOT>

15. Replace multiple spaces with a single space in file f1.
tr -s ' ' < t1

16. Write a unix command to evaluate an expression: 4*3.14+6
awk 'BEGIN {print 4*3.14+6}'

17.Write a command to display all unique words of file f1.
tr ' ' '\n' <t1 | sort | uniq -u

18. Write a command to locate lines that begin and end with (.).
grep '^\..*\.$' t1

19. Write a command to display all lines that contains 2 or more ^ at the begining of line.
grep -E '^\^{2,}' t1

20. Write a command to repace all occurences of 'he' with 'she' and 'hello' with 'hi' in f1.
sed -e 's/\bhello\b/hi/g' -e 's/\bhe\b/she/g' t1

21. Display those lines having exactly 10 alphabets from f1.
grep '^[A-Za-z ]\{10\}$' t1

22. Copy file f1 to f2 if f1 exists otherwise write error message to file f2.
cp t234 t2 2> t2

23. Search those files from current directory which have more than 5 links.
find . -links +5 -print

24. Display lines of file f1 that donot contain digit.
 grep -v '[0-9]' t1

25.Replace all occurrences of 'linux os' with 'unix os' in file f1.
sed 's/linux os/unix os/g' t1

26.Display all lines of f1 having third word as 'user'.
grep '^[^ ]* [^ ]* user' t1

27. Display name of all files of working directory having pattern "The".
grep -l 'The' *

28. Display lines of file f1 that begin with any capital letter.
grep '^[A-Z]' t1

29.Write a sed command to extract first word of each line. Assuming that there is no white space character at begining of line.
sed 's/ .*/ /g' t1

30. What does the following command do? grep f1 f2 f3
Here f1 is considered as pattern and f2 and f3 are file names. So it would search f1 pattern in f2 and f3 file.

31.Display those lines of file f1 having length in between 30 to 50 characters.
grep '^.\{10,20\}$' t1

32.Display binary value of 12 using bc.
bc
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'.
obase=2
12
1100

33.Replace all occurrences of 'hello' with 'hi' and 'he' with 'she'
sed -e 's/\bhello\b/hi/g' -e 's/\bhe\b/she/g' t1

34.Count number of words and lines of files whose filename begins with x.
find . -maxdepth 1 -type f -name 't*' -exec wc -wl {} \;

35.Write equivalent sed command of 'sed '1,5d' f1'.
sed -n '1,5!p' f1

36. Write equivalent IRE for the following RE
-A*
-A?
A\{1,\}
A\{1\}