This is the procedure for finding large files and sorting files by size on a FreeBSD, UNIX, Linux and Mac OS X based file system by using the default find, du, sort and tail utilities. Written 2022-08-08. Updated 2022-08-11.
Listing contents of current directory and sorting by size.
The following command will list contents, which includes directories and regular files, of the current directory. The list will be sorted by size from smallest to largest. The du command is given the -s option, which will calculate the size of each directory, and the -h option, which will add size with SI suffix to the output. The sort command is given the -h option, which will sort output by numerical values with SI suffix, such as K, M, G, T and P, known as kilobytes, megabytes, gigabytes, terabytes and pentabytes.
Consider the following content of a current directory, that has a directory and a number of documents.
$ ls dira doca.dat docb.dat $ ls dira docc.dat docd.dat
The directories and files can now be listed and sorted by size.
$ du -sh * | sort -h 3,6K doca.dat 9,3M dira 44G docb.dat
The sort order can be reversed by adding the option -r.
$ du -sh * | sort -hr 44G docb.dat 9,3M dira 3,6K doca.dat
Finding large files in current directory and sorting by size.
The following command will search current directory, and its subdirectories, and find regular files, that are larger than 200 MB in size. The list will be sorted by size from smallest to largest. The find utility is given the -type f option, that will limit the output to regular files, the -size +200M option, that will list files with a size of 200 MB or larger, and, ouput the list of files with the du utility. The du utility is given the option -h, that will add SI suffix to size. The sort utility is given the option -h, that will output by numerical values with SI suffix, such as K, M, G, T and P, known as kilobytes, megabytes, gigabytes, terabytes and pentabytes.
$ find . -type f -size +200M -exec du -h {} \; | sort -h
Note, that some files might be listed with different sizes, which depend on the utility, that is used. The du utility will show the size, which the file is occupying on the file system. This size is usually smaller, because of compression and other file system characteristics. The ls utility will show the size, which the file has, if you opened it in an editor and counted the number of letters or bytes within the file.
$ find . -type f -size +200M -exec du -h {} \; | sort -h 72M ./.cache/foo/bar.db $ ls -ld ./.cache/foo/bar.db -rw-r--r-- 1 micski micski 229552128 11 Aug 2022 ./.cache/foo/bar.db
Finding and listing the top largest files on an entire file system.
The following command will search and find regular files in a file system, sort the output by size from smallest to largest and then only list the top 5 largest files. The tail utility is given the option -n 5, which will only list the last lines of the output.
# find /usr/home/ -type f -exec du -h {} \; | sort -h | tail -n 5
The following command will only find regular files with a specific name, such as a data heavy file type as coredumps.
# find . -type f -name '*.core' -exec du -h {} \; | sort -h | tail -n 5
More about finding, listing and sorting file sizes.
find, du and sort general commands on FreeBSD Manual Pages. How To Find Largest Top 10 Files and Directories On Linux / UNIX / BSD on nixCraft. How do you sort du output by size? on StackExchange. How to find files larger than 10MB, 100MB, 1GB in Linux on Stack Overflow. How to rename batch of images or files in bash shell.