Professional Documents
Culture Documents
Display Username and UID sorted by UID Using cut, sort and tr
cut command is used to extract specific part of a file. The following example cu
ts the username and UID from /etc/passwd file, and sort the output using sort co
mmand using username as a key and :
as a delimiter.
As a part of formatting the output, you can use any other character to display u
sername and UID. Using tr command you can convert to :
to any other character.
$ cut -d ':' -f 1,3 /etc/passwd | sort -t ':' -k2n - | tr ':' '\t'
root
0
daemon
1
bin
2
sys
3
sync
4
games
5
man
6
lp
7
mail
8
news
9
uucp
10
proxy
13
2. Find List of Unique Words in a file Using tr, sed, uniq
The following example lists the words which has only alphabets. tr command conve
rts all the character other than alphabets
to newline. So all the words will be listed out with number of newlines. Sed com
mand removes the empty lines and finally uniquely sort the output to avoid the d
uplicates.
For this example, let us use the following Readme.txt file.
$ cat Readme1.txt
Installtion Steps:
Run the below steps as root user:
1.Copy the output file at any temporary location.
2.Unzip the file using unzip command.
3.Copy the tar to data directory
Now, execute the following command to find list of uniq words in the above Readm
e.txt file.
$
1
1
1
1
1
2
1
1
2
1
1
1
1
1
2
1
1
4
1
2
1
1
$
steps
tar
temporary
the
to
unzip
user
using
Note: uniq with -i ignores the cases, so the count of the word
zip and Unzip)
unzip
Linux sed command plays a vital role in text manipulation operations. Please ref
er our Sed Tutorial for detailed explanation about sed command.
3. Join Two Files (Where one file is not sorted) Using sort and join
Join command joins two files based on a common field between two files. For join
to work properly, both the files should be sorted. In case if one file is sorte
d and one more is not, then the following example will help you to join.
In the example below, the file m1.txt has Employee name and Employee Id and its
not sorted. Second file m2.txt has employee name and Department name. To join th
ese two files, sort the first file and give the sorted output as one of the inpu
t stream for join. Without any option join command uses the first field in a fil
e as a common field.
$ cat m1.txt
Jincy 500
Amit 300
Saurab 100
Jobi 400
Kumar 200
$ cat m2.txt
Amit Monitoring
Jincy Marketing
Jobi Accounts
Kumar Sales
Saurab Maintenence
$ sort m1.txt | join - m2.txt
Amit 300 Monitoring
Jincy 500 Marketing
Jobi 400 Accounts
Kumar 200 Sales
Saurab 100 Maintenence
$
4. Find out which process is using up your memory using ps, awk, sort
The following command lists all the process sorted based on the used memory size
.
$ ps aux | awk '{if ($5 != 0 ) print $2,$5,$6,$11}' | sort -k2n
PID VSZ RSS COMMAND
3823 3788 484 /sbin/mingetty
3827 3788 484 /sbin/mingetty
3830 3788 484 /sbin/mingetty
3833 3788 488 /sbin/mingetty
3834
3873
2173
1835
1832
2054
2281
3788
3788
3796
3800
5904
5932
6448
484
484
568
428
596
540
360
/sbin/mingetty
/sbin/mingetty
/usr/sbin/acpid
klogd
syslogd
/usr/sbin/sdpd
gpm
The above command lists the PID, Used virutal memory size, Used resident set-siz
e and process command. The output is sorted on VSZ.
While debugging performance issues, use this command to find out which process i
s using up the memory.
Awk is an extremely useful language to manipulate structured data very quickly.
We have written tons of article on awk earlier. Start by reading this Awk Introd
uction article, and dive in deep by reading the whole awk tutorial series.
5. Find out Top 10 Largest File or Directory Using du, sort and head
du command shows summarized disk usage for each file and directory of a given lo
cation (/var/log/*). The output of a sort command is reversely sorted based on t
he size.
# du -sk /var/log/* | sort -r -n | head -10
1796
/var/log/audit
1200
/var/log/sa
612
/var/log/anaconda.log
512
/var/log/wtmp
456
/var/log/messages.4
92
/var/log/messages.2
76
/var/log/scrollkeeper.log
72
/var/log/secure
56
/var/log/cups
48
/var/log/messages.1
6. Find out Top 10 Most Used Commands.
Ever wondered what command you type a lot from your command line? If you are lik
e most people it might be pwd or ls. Check it out yourself using this one liner.
Bash maintains all the commands you execute in a hidden file called .bash_histor
y under your home directory, as we ve explained earlier in our 15 Examples to Master
Linux Command History article.
Use the following one liner to identify which command you execute a lot from you
r command line.
$ cat ~/.bash_history | tr "\|\;" "\n" | sed -e "s/^ //g" | cut -d " " -f 1 | so
rt | uniq -c | sort -n | tail -n 15
11 ssh
12 shutdown
15 cp
15 vncserver
22 cat
23 find
23 pwd
24 mv
25 ovc
47 grep
58 ps
67 vi
74 ll
117 ls
118 cd
The above example does the following operation to get the top 10 used commands
Multiple commands can be executed in a single command lines each separated b
y | or ; . To count each command separately, convert pipe character or semicolon to new
line.
Remove the spaces in the beginning (if exists) using sed command.
Cut the command field (first field).
Sort the commands and find number of occurrences of each command using uniq
command.
Sort based on the number of occurrences and print only last 15 lines.