≡ Menu

sort command

Linux: Sort du -h (human-readable) Output By Size

I have a large number of files stored in ~/Downloads/ directory. How do I sort and print sizes in human readable format using du -h command under Ubuntu Linux LTS version 12.04 or any other Linux distributions?
[click to continue…]

Linux ls Command: Sort Files By Size

How do I sort all *.avi or *.py files in $HOME/Download/ directory by file size using Linux ls command line utility?
[click to continue…]

Linux / Unix: Sort Specific Field or Column

How do I sort output of find . -type f -iname "pattern*" -ls command by seventh column in Linux? Sample input:

7782638 15084 -rw-r--r-- 1 tom tom 15422474 Aug 1 2011 ./logserver42/2011/hsbcreports_201107.html
7782637 14504 -rw-r--r-- 1 tom tom 14830464 Jul 1 2011 ./logserver42/2011/hsbcreports_201106.html
7782643 14408 -rw-r--r-- 1 tom tom 14730488 Dec 1 2011 ./logserver42/2011/hsbcreports_201111.html
7782640 19656 -rw-r--r-- 1 tom tom 20101379 Oct 1 2011 ./logserver42/2011/hsbcreports_201109.html
7782634 17588 -rw-r--r-- 1 tom tom 17985190 Apr 1 2011 ./logserver42/2011/hsbcreports_201103.html
7782641 20796 -rw-r--r-- 1 tom tom 21263382 Nov 1 2011 ./logserver42/2011/hsbcreports_201110.html
7798789 13296 -rw-r--r-- 1 tom tom 13590695 Mar 1 2012 ./logserver42/2012/hsbcreports_201202.html
7798788 12220 -rw-r--r-- 1 tom tom 12495686 Feb 1 2012 ./logserver42/2012/hsbcreports_201201.html
5701709 1648 -rw-r--r-- 1 tom tom 1681600 Nov 1 11:14 ./logserver42/2012/hsbcreports_201210.html
7798802 8944 -rw-r--r-- 1 tom tom 9138603 Jun 1 10:13 ./logserver42/2012/hsbcreports_201205.html
5701710 648 -rw-r--r-- 1 tom tom 657811 Nov 10 11:13 ./logserver42/2012/hsbcreports_201211.html
7798805 7360 -rw-r--r-- 1 tom tom 7523891 Sep 1 10:11 ./logserver42/2012/hsbcreports_201208.html
7323999 776 -rw-r--r-- 1 tom tom 787924 Jun 12 2006 ./logserver42/2006/hsbcreports_200605.html
7324001 140 -rw-r--r-- 1 tom tom 139058 Aug 22 2006 ./logserver42/2006/hsbcreports_200607.html
7324000 44 -rw-r--r-- 1 tom tom 41447 Jun 12 2006 ./logserver42/2006/hsbcreports_200606.html
7324003 1972 -rw-r--r-- 1 tom tom 2014614 Oct 1 2006 ./logserver42/2006/hsbcreports_200609.html
7356496 3136 -rw-r--r-- 1 tom tom 3205737 Dec 1 2007 ./logserver42/2007/hsbcreports_200711.html

I need the following output:

7324000 44 -rw-r--r-- 1 tom tom 41447 Jun 12 2006 ./logserver42/2006/hsbcreports_200606.html
7324001 140 -rw-r--r-- 1 tom tom 139058 Aug 22 2006 ./logserver42/2006/hsbcreports_200607.html
5701710 648 -rw-r--r-- 1 tom tom 657811 Nov 10 11:13 ./logserver42/2012/hsbcreports_201211.html
7323999 776 -rw-r--r-- 1 tom tom 787924 Jun 12 2006 ./logserver42/2006/hsbcreports_200605.html
5701709 1648 -rw-r--r-- 1 tom tom 1681600 Nov 1 11:14 ./logserver42/2012/hsbcreports_201210.html
7324003 1972 -rw-r--r-- 1 tom tom 2014614 Oct 1 2006 ./logserver42/2006/hsbcreports_200609.html
7356496 3136 -rw-r--r-- 1 tom tom 3205737 Dec 1 2007 ./logserver42/2007/hsbcreports_200711.html
7798805 7360 -rw-r--r-- 1 tom tom 7523891 Sep 1 10:11 ./logserver42/2012/hsbcreports_201208.html
7798802 8944 -rw-r--r-- 1 tom tom 9138603 Jun 1 10:13 ./logserver42/2012/hsbcreports_201205.html
7798788 12220 -rw-r--r-- 1 tom tom 12495686 Feb 1 2012 ./logserver42/2012/hsbcreports_201201.html
7798789 13296 -rw-r--r-- 1 tom tom 13590695 Mar 1 2012 ./logserver42/2012/hsbcreports_201202.html
7782643 14408 -rw-r--r-- 1 tom tom 14730488 Dec 1 2011 ./logserver42/2011/hsbcreports_201111.html
7782637 14504 -rw-r--r-- 1 tom tom 14830464 Jul 1 2011 ./logserver42/2011/hsbcreports_201106.html
7782638 15084 -rw-r--r-- 1 tom tom 15422474 Aug 1 2011 ./logserver42/2011/hsbcreports_201107.html
7782634 17588 -rw-r--r-- 1 tom tom 17985190 Apr 1 2011 ./logserver42/2011/hsbcreports_201103.html
7782640 19656 -rw-r--r-- 1 tom tom 20101379 Oct 1 2011 ./logserver42/2011/hsbcreports_201109.html
7782641 20796 -rw-r--r-- 1 tom tom 21263382 Nov 1 2011 ./logserver42/2011/hsbcreports_201110.html

[click to continue…]

du Command Output In Human Readable Format ( GB / MB / TB )

I need to get a list of sizes in human readable du output format. How do I get the output for du command in GB under Linux or UNIX operating systems?
[click to continue…]

How To Use awk In Bash Scripting

How do I use awk pattern scanning and processing language under bash scripts? Can you provide a few examples?
[click to continue…]

Linux / UNIX Shell: Sort Date

Q. How do I sort date type of data stored in a text file which is in following sample format:

Code Date TIME
HOS05 23/12/2008 10AM
HOS06 15/12/2008 2PM
HOS62 29/12/2008 10AM
HOS64 23/12/2008 2PM
HOS70 26/12/2008 10AM
ZFT01 06/12/2008 10AM
HOS73 11/12/2008 2PM
MHOS051 05/12/2008 10AM
MHOS041 20/12/2008 2PM
MHOS042 27/12/2008 2PM
MHOS053 11/12/2008 10AM
MHOS043 30/12/2008 2PM

How do I sort data date and time wise?
[click to continue…]

Shell: How To Remove Duplicate Text Lines

Q. I need to sort data from a log file but there are too many duplicate lines. How do I remove all duplicate lines from a text file under GNU/Linux?
[click to continue…]

Linux / UNIX Shell: Sort IP Address

Q. I’d like to sort a list of IP address stored in a text file. How do I sort by last notation or entire address under Linux or UNIX operating systems?
[click to continue…]

Diagnosing a dos Attack Under Linux

Q. How do I find out if I Linux box is under DoS attack or not form a shell prompt?
[click to continue…]

Linux / UNIX find the smallest directories or files in current directory

Q. How do I list or find the smallest directories or files in the current directory under Linux or UNIX like operating system?

A. There is no direct command exists for this task. However by using shell pipes and combination of other commands one can produced the desired result.

Task: Display list of smallest files

You need to use ls command and pass the option -l (long format) -S (sort) -r (in reverse order), enter:
$ ls -lSr
$ ls -lSr
$ ls -lSr | head
$ ls -lSr | head -5

Output:

-rw-r--r--  1 root root         0 May 29 07:08 Muttrc.local
-rw-r--r--  1 root root         0 Jan 12  2000 motd
-rw-r--r--  1 root root         0 Jan 12  2000 exports
-rw-r--r--  1 root root         0 Nov 28  2006 environment
-rw-rw-r--  1 root disk         0 Aug  7  2006 dumpdates
-rw-r--r--  1 root root         0 Jul 10 08:50 cron.deny
-rw-------  1 root root         1 Aug 23  2006 at.deny
lrwxrwxrwx  1 root root         7 Jul 10 08:50 rc -> rc.d/rc
lrwxrwxrwx  1 root root        10 Jul 10 08:50 rc6.d -> rc.d/rc6.d
lrwxrwxrwx  1 root root        10 Jul 10 08:50 rc5.d -> rc.d/rc5.d
lrwxrwxrwx  1 root root        10 Jul 10 08:50 rc4.d -> rc.d/rc4.d
lrwxrwxrwx  1 root root        10 Jul 10 08:50 rc3.d -> rc.d/rc3.d
lrwxrwxrwx  1 root root        10 Jul 10 08:50 rc2.d -> rc.d/rc2.d

Task: Display list of smallest directories

You need to use du command to display sorted (-S option) output. Use pipe to send du command output to sort command for sorting purpouse:
$ du -S . | sort -n
$ du -S . | sort -n | head -10

Output:

du -S . | sort -n | head -10
4       ./lighttpd/ssl
4       ./monit.d
8       ./acpi
8       ./acpi/actions
8       ./alchemist
8       ./alchemist/namespace
8       ./alternatives
8       ./desktop-profiles
8       ./dev.d
8       ./dev.d/default

Read the man page of ls, sort and du for more options:
man ls
man du
man sort