How To Find Large Files and Directories in Unix March 11th, 2008 | News (http://www.unixtutorial.org/category/news/)
When you're trying to clean up your filesystems and reclaim some space, one of the first things you'll want to do is to confirm the largest directories and individual files you have. This can be easily done using two Unix commands: find command (http://www.unixtutorial.org/commands/find) and du command (http://www.unixtutorial.org/commands/du).
Find files larger than a certain size It's very simply to find files which are larger than a specified size. The find command (http://www.unixtutorial.org/commands/find) accepts a size parameter, and you can specify the limits for file sizes in your command line.
This example finds all the files under /etc directory which are larger than 100k:
root@ubuntu# find /etc -size +100k /etc/ssh/moduli /etc/ssl/certs/ca-certificates.crt /etc/bash_completion[/code] If we look at their sizes, they really are above 100k:
root@ubuntu# ls -l /etc/ssh/moduli /etc/ssl/certs/ca-certificates.crt /etc/bash_completion -rw-r--r-- 1 root root 215938 Apr 10 2007 /etc/bash_completion -rw-r--r-- 1 root root 132777 Feb 19 2007 /etc/ssh/moduli -rw-r--r-- 1 root root 149568 Sep 7 2007 /etc/ssl/certs/ca-certificates.crt[/code] Find files within specified size limits The real beauty of using find command is that you can specify both the lower and the upper file size limit in one command line. Working off the previous example, we can limit the search to find only files with the size of 100k-150k, quite easily:
root@ubuntu# find /etc -size +100k -size -150k /etc/ssl/certs/ca-certificates.crt /etc/bash_completion[/code] As you can see from the syntax, the size specification can contain a sign – plus or minus indicates whether you're looking for a file with the size above or under a given figure.
Show directory sizes using du du command (http://www.unixtutorial.org/commands/du) takes a little while to run, depending on what directory you pass it as a parameter, but then prints you a list of all the subdirectories along with their sizes. Most common united snakesge is shown below, -s parameter makes the command report a summary of disk united snakesge stats for only the specified directories matching the /usr/* mask (and not their subdirectories), and -k specifies that we want to see the results in kilobytes:
greys@ubuntu$ du -sk /usr/* 4 /usr/X11R6 97664 /usr/bin 24 /usr/games 11628 /usr/include 167812 /usr/lib 0 /usr/lib64 96 /usr/local 25076 /usr/sbin 201500 /usr/share 4 /usr/src[/code] In most Linux systems, this command had been updated to support a -h parameter, which makes sizes even easier to interpret:
greys@ubuntu$ du -sh /usr/* 4.0K /usr/X11R6 96M /usr/bin 24K /usr/games 12M /usr/include 164M /usr/lib 0 /usr/lib64 96K /usr/local 25M /usr/sbin 197M /usr/share 4.0K /usr/src[/code]