UNIX IMPORTANT COMMAND
1 Display line 1 to 5 from file x1. [2015]
sed –n ‘1,5p’ x1
2 Display last word of each line from file x1. [2015]
awk ‘{print $NF}’ x1
3 To count number of character in first 3 line of file x1. [2015,2016(2)]
sed –n ‘1,3p’ x1| echo "total characters=" `wc -c`
4 To count number of words in line 40 thought 60 of file x1. [2015,2016]
sed –n ‘40,60p’ x1| echo "total words=" `wc -w`
5 To delete all special character from file [Link]. [2015,16,17]
tr -cd '[:alnum:]\ \n' < [Link]
6 Display all lines those stats with 'let' from a file x1. And the letter l, e or t
may be in any case. [2015]
grep -i ^let x1
7 To print first 7 lines from file. [2014(2),2016,17]
sed –n ‘1,7p’ x1
Starting from 10th line to the end of file x1. [2017]
sed -n '10,$p' x1
8 To sort a line of file and also remove a repeated line. [2014,2016,17]
sort –u x1
9 Replace all occurrences of 'TYBCA' IN STUDLST. [2018]
sed 's/ TYBCA / BCA /g' x1
OR
Replace all occurrences of 'SYBCA' with 'TYBCA' in 5th line of file [Link].
[2017]
sed 5's/ SYBCA / TYBCA /g' x1
10 To run utility x1 at 9:00 AM. [2014,17]
$ at 9:00
at> echo "It works!" > [Link]
at> Ctrl+D
job 1 at Fri Nov 20 11:27:00 2024
11 to display no of processes. [2016(2),2017]
ps aux
Here's a breakdown of the command:
ps: is the process status command.
a: displays information about other users' processes as well as your own.
u: displays the processes belonging to the specified usernames.
x: includes processes that do not have a controlling terminal.
12 To display lines end with 'India'. [2014,2017
grep India$ f1
13 To display lines start with “The”. [2014]
grep ^The f1
14 To locate/find the 'Unix' ignoring case. [2014,17]
grep –i “unix” f1
15 Remove duplicate lines from file x1. [2016,2018]
Sort –u x1 or
Uniq x1
16 Display all files in current directory where the 1st character is not digit.
[2016,17, 2018]
ls |grep "^[^0-9]"
Display all files in current directory where the 1st character or any
character is digit
ls -d1 [0-9]* or ls *[[:digit:]]* or ls *[0-9]*
[ls -d abc* # list all files starting with abc---
ls -d *abc* # list all files containing --abc--
ls -d *abc # list all files ending with --abc]
16 OR count no of character in last line of file. [2018]
tail -1|echo “Total character”`wc –c`
17 Remove a file forcible which do not have write permission. [2016, 2018]
rm –f f1
18 Display all files in current directory where the 1st character is numeric
and last character is not alphabet. [2017]
ls | grep -e "^[0-9$]"
19 Display content of last modified file. [2017]
ls -lt | head -n 2
20 Display content of top 3 largest file in a working directory. [2017]
ls -Sl|head -4
21 display i-node number of all files of current directory. [2017]
ls -i
22 Link three files [2022]
ln f1 f2; ln f1 f3
23 List attributes of the current directory [2022]
ls -l
24 To print the files in the current directory and one level down to the
current directory. [2022]
cd ..;ls
25 To locate all the lines where 1st and last characters are same. [2022]
grep '^a.*a$' f1
or
grep -E "^a.*a$" f1
26 Write a command using grep or sed or awk to display line 5 to 15, 25 to
35 and last line of file f1.[2021]
sed –ne ‘5,15p’ –e ‘25,35p’ –e ‘$p’ f1
or
sed -ne '5,15p
> 25,35p
> $p' f1
27 Write a command to count number of characters in first five lines of file
x1. [2021]
sed –n ‘1,5p’ x1 |echo “Total character =”`wc –c`