SOL: Use the power of the command line to reclaim disk space

This is part of my Simple One-Liners (SOL) series of useful commands. Ok, I’m just starting the series now, but I thought it would be a good idea!

Of course there are tools like the eminent BleachBit which you can and should be using regularly to reclaim space on your hard drive. But what if you want to see what is actually taking the most space on your drive? This may be especially important for a system with limited drive space. For example my ultra-portable Thinkpad X1 Carbon has a 200GB SSD drive so I have to watch the space on it more carefully.

Simple! In a Cygwin terminal running with Administrator privileges, just use the du command, combined with the sort command, and pipe its output to a file. In this example I’m using drive C which is usually the main drive.

cd /cygdrive/c
du -ak . |sort -rn 2>&1 |tee du.txt

This will output the result to a file du.txt that you can then view with the less command:

less du.txt

Let’s say you want to get fancy with the name of the file and add a date to it, for example the following date command will give you output like the following:

date +%Y.%m.%d-%T
2016.05.18-15:45:23

You can combine this with the above command:

du -ak . |sort -rn 2>&1 |tee du-`date +%Y.%m.%d-%T`.txt

which will give you a file like du-2016.05.18-15:45:23.txt

Now when you look at the output file you will see a list of files on drive C in descending order by size.

Now you can decide – look at the list. Do you really need those videos on the drive? Can some backup files be moved to your network share?


Comments

Leave a Reply