macOS FAQ: From the command line, how do I show the largest files under a directory on macOS (and Linux/Unix systems).
Solution: Use the Unix
du (disk usage) command, and sort its output.
A du/sort command to show the largest files under a directory on Mac OS X
The Unix/Linux command that worked for me on my MacOS system is this:
$ du -a * | sort -r -n | head -10
du is the disk usage command, and the
-a flag says, “Display an entry for each file in a file hierarchy.” Then I use the
sort command to sort the
du output numerically and in reverse. After that,
head -10 shows only the first ten lines of output. In the Music folder on my Mac the command and output look like this:
/Users/Al/Music> du -a * | sort -r -n | head -10 122833752 iTunes 119838520 iTunes/iTunes Music 30359456 iTunes/iTunes Music/Movies 15656496 iTunes/iTunes Music/Home Videos 10431024 iTunes/iTunes Music/Podcasts 9745248 Amazon MP3 8120392 iTunes/iTunes Music/Compilations 8049464 iTunes/iTunes Music/Unknown Artist 7976536 Unknown Artist 6101880 iTunes/iTunes Music/Unknown Artist/Unknown Album
Variations of that `du` command
As mentioned, that command shows how to display the largest 10 files and folders under the current OS X directory. To show the first 20 files/directories you’d use this command:
du -a * | sort -r -n | head -20
To show files 21-30 you can add in the
tail command, like this:
du -a * | sort -r -n | head -30 | tail -10
In that command,
head -30 prints the first 30 results, but then the
tail command shows only the last 10 files, which displays files 21-30 in the end.
Note: I was pointed towards this solution by this cyberciti.biz page. Their solution was very close, but just needed a minor change or two to work on MacOS.
In summary, if you wanted to see how to show the largest files under a directory on MacOS, Unix, and Linux systems, I hope this is helpful.