Finding files that use the most disk space

  • Is it possible to list the largest files on my hard drive? I frequently use df -H to display my disk usage, but this only gives the percentage full, GBs remaining, etc.

    I do a lot of data-intensive calculations, with a large number of small files and a very small number of very large files. Since most of my disk space used is in a very small number of files, it can be difficult to track down where these large files are. Deleting a 1 kB file does not free much space, but deleting a 100 GB file does. Is there any way to sort the files on the hard drive in terms of their size?

    Thanks.

  • jippie

    jippie Correct answer

    9 years ago

    With standard available tools:

    To list the top 10 largest files from the current directory: du . | sort -nr | head -n10

    To list the largest directories from the current directory: du -s * | sort -nr | head -n10

    UPDATE These days I usually use a more readable form (as Jay Chakra explains in another answer and leave off the | head -n10, simply let it scroll off the screen. The last line has the largest file or directory (tree).

    Sometimes, eg. when you have lots of mount points in the current directory, instead of using -x or multiple --exclude=PATTERN, it is handier to mount the filesystem on an unused mount point (often /mnt) and work from there.

    Mind you that when working with large (NFS) volumes, you can cause a substantial load on the storage backend (filer) when running du over lots of (sub)directories. In that case it is better to consider setting quota on the volume.

    For your first option, can't you just list them with `ls -Sl | head`?

    No, `du` traverses the whole directory tree, whereas ls -S only checks the current directory.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM