Search for the largest directories or files on your hard drive with find

Have you ever wanted to know what is the largest folder or file on your hard drive?

The command find it's great, it allows us to do many things (we've already talked about some of them here), here I bring you another use of it.

The following command will search the entire HDD and tell us which are the 10 largest files or folders on the computer:

sudo find / -printf '%s %p\n'| sort -nr | head -10

If you want to know not just the biggest 10, but the 20 or something like that, just swap the last 10 for the desired one.

As I said before, this will count both folders and files, if you just want to take into account the folders would be to add -type d (d = directory):

sudo find / -type d -printf '%s %p\n'| sort -nr | head -10

On the contrary and want to see only the files and no folders would be -type f (f = file):

sudo find / -type f -printf '%s %p\n'| sort -nr | head -10

If you want to specify the type of file, that is, just take into account .mp4, just add an -iname "* .mp4":

sudo find / -iname "*.mp4" -printf '%s %p\n'| sort -nr | head -10

In my case the largest files I have are the virtual HDDs of my virtual servers with KVM+ Qemu, then a football video (presentation of Gareth Bale with Real Madrid) and other things.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Borisadrian said

    Just what I was looking for to know where I occupy more space in my root and thus be able to free it.

    Thank you.

  2.   Eduardo said

    Very good article, very useful. Thank you very much… By the way, Hala Madrid !! hehehe

    1.    FIXOCONN said

      I join the Madrid group here
      Some time ago I installed centos 6.5 minimal and I had this error and I solved it by editing the / etc / hostname, because the hostname that I had written in the configuration of the network card was not recognized by apache

  3.   3rn3st0 said

    If there is something I like about «Desde Linux» is that these gems always appear for the console that make our lives between zeros and ones more bearable. Thank you so much KZKG ^ Gaara!

  4.   wow said

    I would swear I had read in this blog an alternative, that since I discovered it I cannot live without it:

    ncdu

    It is an interactive command that does not come by default (you have to install it from your distro package) but it is tremendously useful. It sorts the files by size, showing you a bar or a percentage of the space they occupy on the partition. Here is a screenshot taken from the internet http://www.heitorlessa.com/wp-content/uploads/2013/04/NCDU-1.9-Disk-stats.png

  5.   vidagnu said

    It can also be done with the du command.
    This is to find the folders

    $du-Sh | sort -rh | head -n 15

    And this one to find the largest files.

    $ find. -type f -exec du -Sh {} + | sort -rh | head -n 15

    $ find. -type f -exec du -Sh {} + | sort -rh | head -n 15

  6.   hup80 said

    And what is the explanation for each option?

  7.   Luis Gago Casas said

    Very good article was of great help to me.
    Thank you so much for sharing it.

  8.   Roger Reyes said

    Can anybody help me? I need a command that searches within a directory for all the .txt files that are greater than 0 bytes and moves them to another directory, so far I have only found this:

    find. -type f -size + 1b -exec mv /home/oradev/new/*.txt / home / oradev / move \;

    but move all files regardless of their size.

  9.   jac said

    Thanks for the command!

    He had used it on other occasions, but only in "Script kiddie" mode ... because of the rush and such.

    And even though find is a very common command (-name, –exec), I hadn't been able to take a good look at the entire manual.

    I had already realized the brutal power that this magnificent tool has ... but now I see it more closely and I admire it more.

    Here you have it in Spanish:
    http://es.tldp.org/Paginas-manual/man-pages-es-extra-0.8a/man1/find.1.html

    It is a bitch that the arguments are not more intuitive ... Either you know them, because you have learned them, or to search inet or man when there are no more ... otas.

    Thanks again and thank you as always to GNU!

    One question ... just out of curiosity:

    When you put the "printf" argument to find ...
    Does find use the system printf command, or is printf implemented within find?

    I say it, because printf is a command implemented in the system forever, but that personally I have never had to make use of ... at least directly.

    A greeting!

    jack.

  10.   duven said

    could you tell me how to execute sudo find / -type f -printf '% s% p \ n' | sort -nr | head -10
    avoiding some routes?

    I have for example:
    / dev / sda2 19G 16G 2.8G 85% /
    udev 10M 0 10M 0% / dev
    tmpfs 3.2G 329M 2.9G 11% / run
    tmpfs 7.9G 153M 7.8G 2% / dev / shm
    tmpfs 5.0M 0 5.0M 0% / run / lock
    tmpfs 7.9G 0 7.9G 0% / sys / fs / cgroup
    / dev / sda1 453M 37M 389M 9% / boot
    / dev / drbd3 477M 2.3M 445M 1% / var / lib / nfs
    / dev / drbd1 1.9T 821G 1005G 45% / nfs / home
    / dev / drbd2 2.9T 960G 1.8T 36% / nfs / homearchive
    / dev / drbd0 962G 426G 488G 47% / nfs / pool

    and when running find / -type f -printf '% s% p \ n' | sort -nr | head -10
    I get files from / nfs /
    I would like to ignore that