Find large files/directories in Linux

Using a mix of a couple utilities to find the largest directories/files in Linux

du -aBM 2>/dev/null | sort -nr | head -n 50 | more

du arguments:

  • -a for “all” files and directories. Leave it off for just directories
  • -BM to output the sizes in megabyte (M) block sizes (B)
  • 2>/dev/null – exclude “permission denied” error messages (thanks @Oli)

sort arguments:

  • -n for “numeric”
  • -r for “reverse” (biggest to smallest)

head arguments:

  • -n 50 for the just top 50 results.
  • Leave off more if using a smaller number

Note: Prefix with sudo to include directories that your account does not have permission to access.

Example showing top 10 biggest files and directories in /var (including grand total).

cd /var
sudo du -aBM 2>/dev/null | sort -nr | head -n 10
7555M   .
6794M   ./lib
5902M   ./lib/mysql
3987M   ./lib/mysql/my_database_dir
1825M   ./lib/mysql/my_database_dir/a_big_table.ibd
997M    ./lib/mysql/my_database_dir/another_big_table.ibd
657M    ./log
629M    ./log/apache2
587M    ./log/apache2/ssl_access.log
273M    ./cache

To display the biggest top-20 directories (recursively) in the current folder, use the following one-liner:

du -ah . | sort -rh | head -20

reference: https://www.cyberciti.biz/faq/linux-find-largest-file-in-directory-recursively-using-find-du/