List files by size in reverse numerical order in Linux or Unix.

The root drive on my Solaris server was full and I needed to find out what file was the largest. The du command can list the size of the files in a directory, you can then use the sort command to display the results in numerical order (by size).

Below is an example of the commands used to list the files by size, from the largest to the smallest, in the / directory.

du -akd / | sort -nr | more

The -a option of the du command means to report the file size. Whthout this option du will just display the amount of space used in each directory.

-k means to display the file in kilobytes, the default is 512 bytes block. You can also substitute it with -h which means human redable format, the output will be in kilobytes, megabytes and gigabytes.

-d option keeps the du command from crossing the partition boundries.

The -n option with the sort command, filters the results in numerical order and the -r means reverse numerical order.

The | symbol separates the different commands. In this case the three commands are du, sort and more.

The more command simply means to display the result one screen at a time, otherwise it will scroll off the top.

If you wanted to capture the results in a file for later viewing, then use the redirect > symbol as in the below example.

$ du -akd / | sort -nr > /export/home/results

About Andrew Lin

Hi, I have always wanted to creat a blog site but never had the time. I have been working in Information Technology for over 15 years. I specialize mainly in networks and server technologies and dabble a little with the programming aspects. Andrew Lin

View all posts by Andrew Lin →