Directory Sorting and File Size’s in Ubuntu

Posted by on October 26, 2011 in Operating Systems, Ubuntu | 0 comments

If you’ve ever had a harddrive randomly fill up on you, you’ll know the importance of a recursive call to iterate through your directories looking for big files. Ubuntu has a fairly easy way to do this.

du -k | sort -nr | more

The above code will recursively iterate through the directory you are in and all directories in it, and find the largest directories and files. It will sort them largest to smallest. So if your /var/log folder is taking up 50gigs, and your /var/lib folder is taking up 40 gigs, The output will look something like below. Using this search, you can figure out what’s taking up the most space on your computer.

90771552 ./var
50381156 ./var/log
40234196 ./var/lib

Now sometimes you may have a /mnt directory that you don’t want to look in. Maybe you know that it’s on another harddrive that has enough space. I have a network storage node under /mnt that would take ages to iterate through if the regular listing were to be called. To ignore a directory you have to do a little more work. First you have to make a file that holds all the folders you want to search through. So if you’re in the highest directory “/” and you want to skip /mnt, then do the following. Before executing the second command, simply edit the file, and delete any directory you don’t want to search over.

find . -type d > mydirs.txt
du -k $(cat mydirs.txt) | sort -nr | more

Another useful command is the following, that will only display individual files that are above 100megs in size. Good directories to try are /var and /home/, but the command will do okay searching the entire disk as well if you leave it in the root directory /.

find /var -size +100000k -exec du -h {} \;
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

0 Comments

Trackbacks/Pingbacks

  1. Ubuntu disk space issues with kcore | Touch of Technology - [...] is that the normal command I run to find files that are taking up the most space, see my …

Leave a Comment

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>