Sort by file/directory size

OS : RHEL 6.6

I want to list the files/directories sorted (Ascending or Desceding) by their size.

As you can see in the below example, du command doesn't sort by size.
In Linux world, is there any other command or workaround using du command to list the files/directories sorted by their size.

[manhba@reporting-node1 Data_Extraction]$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.6 (Santiago)

[manhba@reporting-node1 Data_Extraction]$ ls -lrdth *
drwxrwxr-x 2 manhba manhba 4.0K Dec 16 12:03 WO0000000073502_SBL
drwxrwxr-x 2 manhba manhba 4.0K Dec 16 12:54 WO0000000073255_SBL
drwxrwxr-x 2 manhba manhba 4.0K Dec 17 15:50 WO0000000073265_SBL
drwxrwxr-x 2 manhba manhba 4.0K Dec 22 01:02 WO0000000074061_SBL
drwxrwxr-x 2 manhba manhba 4.0K Dec 27 05:06 WO0000000072057_SBL
drwxrwxr-x 2 manhba manhba 4.0K Jan  4 15:37 3_Jan_2018
drwxrwxr-x 2 manhba manhba 4.0K Jan  4 16:00 WO0000000075793
drwxrwxr-x 2 manhba manhba 4.0K Jan  5 06:20 WO0000000075787
drwxrwxr-x 2 manhba manhba 4.0K Jan  5 22:16 WO0000000075994_jan5
drwxrwxr-x 2 manhba manhba 4.0K Jan  9 05:09 WO0000000076411
drwxrwxr-x 2 manhba manhba 4.0K Jan 17 13:30 WO0000000077242
drwxrwxr-x 2 manhba manhba 4.0K Jan 26 05:57 WO0000000078972
drwxrwxr-x 2 manhba manhba 4.0K Feb  2 14:25 WO0000000080132_OSS
drwxrwxr-x 2 manhba manhba 4.0K Feb  3 19:58 WO0000000075775
drwxrwxr-x 2 manhba manhba 4.0K Feb  6 15:08 WO0000000080606
drwxrwxr-x 2 manhba manhba 4.0K Feb  7 11:21 WO0000000076180
drwxrwxr-x 2 manhba manhba 4.0K Feb  7 11:21 WO0000000080071
drwx------ 2 manhba manhba 4.0K Feb  7 11:24 WO0000000080703
drwxrwxr-x 2 manhba manhba 4.0K Feb  8 07:19 WO0000000080072
drwxrwxr-x 2 manhba manhba 4.0K Feb  9 15:17 WO0000000081126
drwxrwxr-x 2 manhba manhba 4.0K Feb 14 22:04 WO0000000081452
drwxrwxr-x 2 manhba manhba 4.0K Feb 16 15:37 WO0000000081648
drwxrwxr-x 3 manhba manhba 4.0K Feb 20 02:07 WO0000000080073
drwxrwxr-x 2 manhba manhba 4.0K Feb 20 14:34 WO0000000082620
[manhba@reporting-node1 Data_Extraction]$
[manhba@reporting-node1 Data_Extraction]$
[manhba@reporting-node1 Data_Extraction]$ du -sh *
1.1M    3_Jan_2018
8.0K    WO0000000072057_SBL
20K     WO0000000073255_SBL
168K    WO0000000073265_SBL
22M     WO0000000073502_SBL
8.0K    WO0000000074061_SBL
6.8M    WO0000000075775
4.1M    WO0000000075787
52K     WO0000000075793
1.8M    WO0000000075994_jan5
8.0K    WO0000000076180
27M     WO0000000076411
596K    WO0000000077242
75M     WO0000000078972
24K     WO0000000080071
170M    WO0000000080072
12G     WO0000000080073 ----------------------> Largest directory
1.1M    WO0000000080132_OSS
35M     WO0000000080606
2.5M    WO0000000080703
8.4M    WO0000000081126
760K    WO0000000081452
20K     WO0000000081648
2.5G    WO0000000082620  -------------------------> Second largest directory
[manhba@reporting-node1 Data_Extraction]$

How about

du -sb * | sort -n
1 Like

i like to add the -r option to the sort to get the largest files first.

We call this script lz to sort by sizes.

rev=""
if [ $# -gt 0 ]
  then
  if [ $1 = '-r' ]
  then
    rev="r"
#   echo $rev
    shift
  fi
fi
ls -l $* |grep -v "total " |sort +4n$rev -5 +8 |more -e

In case of a tie, the file names are alphabetical.

1 Like

for finding directories of size bigger than 10k in current directory and below that.

find . -type d -size +10k | xargs du -sh | sort -rn
1 Like

Hi jgt,
No such -r option for du command in RHEL 6.X or 7.X

That is true, however there is an -r option for the sort command, that will sort in descending order.