Last Updated: February 25, 2016
·
782
· nikhgupta

Take control of your disk space. Find files larger than a given file size - fully explained tip :)

I, often, would love to find out what mysterious little things can squeeze the wind pipes of my seemingly 1TB hard drive to a meagre 80GB available disk space (oh, yes, that happens to me more than often)

So, I use this simple bash function to quickly list out the path to files which are larger than 1GB of size.

find . -type f -size +1G

This is a standard find feature. I can improve upon the above to create a quick bash function which will let me provide the required minimum file size, I want, while making such a search (note that, I would prefer to know how much size an individual file is taking and hence, I run du on the returned files):

# find all files in a given directory (or current directort) which are larger
# than a specified size (default: 100MB)
# size should be specified in MBs
function fileswithsize() {
   size=${1:-100} && dir="${2:-.}"
   find "${dir}" -type f -size "+${size}M" \
     -exec du -sh "{}" \;
}

The above function will let me find all files that are larger than a given size in a given directory, and display their size, as well.

Now, I am able to run these commands on my system:

# files larger than 100 MB in current directory
fileswithsize
# files larger than 400MB in current directory
fileswithsize 400M
# files larger than 1 GB inside your home directory
fileswithsize 1G ~

Wait, but thats not it!
We can definitely extend this further to do something like this:

# files sorted by size in a human-readable display
function filesbysize() {
   size=${1:-100} && dir="${2:-.}"
   find "${dir}" -type f -size "+${size}M" \
     -exec du -sh "{}" \; | sort -h
}

The above will list all files larger than the given size, and sort them accordingly, while maintaining the human-readable form for the file sizes.

Alas! The above won't work with Mac OSX, since its sort command does not take -h as an option. In which case, we can be a bit more smarter.. Atleast, I think so! :(

# reimplement our function to support MacOSX, as well.
function filesbysize() {
   size=${1:-100} && dir="${2:-.}"
   find "${dir}" -type f -size "+${size}M" -size -1G \
     -exec du -sh "{}" \; | sort -n
   find "${dir}" -type f -size +1G -size -1T \
     -exec du -sh "{}" \; | sort -n
   find "${dir}" -type f -size +1T -size -1P \
     -exec du -sh "{}" \; | sort -n
}

The above works since:

  • find is pretty fast at finding files in those constraints.. O'course, calling it three times is counter to the idea of performance, but personally, I don't mind it that much.
  • splitting the filesizes in this way enable sort -n to list the files in a sorted manner.

If, the final method is not what you would use, I hope I will soon get an email from someone with an improved code.

1 Response
Add your response

I don't think this part will work:

files larger than 1 GB inside your home directory

fileswithsize 1G ~

Because you already specified the measuring unit ( M ) in the function, as shown:

find "${dir}" -type f -size "+${size}M"

over 1 year ago ·