Directory Sorting and File Size’s in Ubuntu
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.
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.
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.
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 /.