unix sysadmin archives
Donation will make us pay more time on the project:

Saturday, 11 June 2011

How to Find Large Files and Directories

One of the most repetitive tasks in an SA's work shift is finding large files and directories.
Here are some tips you can use to find the components that are taking up space in a UNIX file system.
This one liners works on most of the UNIX flavors but not all.

Finding Large Directories

To find large directories, use the du command and sort the output.

#du -k /var|sort -n | tail -10

This outputs the 10 largest directories in /var, sorted in ascending size order.

Solaris has this d option to avoid crossing file system boundaries, that is, to see the directory usage in / but not in the other mounted files systems (/var, /opt, and so on).

#du -kod /var|sort -n | tail -10

Finding Large Files

Use the find command to find large files,

#find / -xdev -type f -size +100000 -ls -exec du -sk {} \;

Now here is a sample that sorts the output.

#find / -size +100000 -type f -ls | sort -n

This finds all plain files in a file system larger than 100,000 512-byte blocks (approximately 50 Mbytes) and sorts the output.

For Solaris you can add a keydef argument. It restricts the sort key field definition. Here is an example that finds all plain files in a /var file system larger than 1,000 512-byte blocks and sort on field 7 which is the file size. It numerically ignores leading blanks.

#find /var -size +1000 -type f -ls | sort -k 7,7 -n

And here is a similar script for HPUX

#find . -type f -print|xargs ls -l|sort -r -n -k 5,5

There you go! Hope I can add some more in the future. Enjoy!

1 comment: