11.2. sort on the system

Sort can be used to sort the usernames from the /etc/passwd file. All we need to do is sort on field 1, which is the login name field, then pass the output using a pipe to awk, and let awk print the first field.

						$ cat passwd | sort -t: +0 | awk -F":" '{print $1}' 
adm 
bin 
daemon 
... 
...
					

Sort can also be used on the df command to print the usage column in desending order. Here’s a normal df output.

						$df 
Filesystem  1024-blocks Used   Available Capacity Mounted on 
/dev/hda5   495714      291027 179086    62%      / 
/dev/hda1   614672      558896 55776     91%      /dos
					

Now using -b option, which ignores any leading blanks when sorting, we use field 4 (+4), which is the ‘capacity’ column. Reversing the sort gives a clearer picture of the free space on ...

Get Linux and Unix Shell Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.