Published on

Use Commandlinefu To Remove Temporary Folders En Masse

Authors

Every few months I generally start to run out of space on my Ubuntu machine. This is usually caused by docker eating up space (which I cover here). Today I wiped the various docker pieces but found I still had not freed up enough space - I needed to squeeze out even more space.

I first started by using the excellent ncdu command line tool to detect what was using up space. This can easily be installed on Linux using sudo apt install ncdu. You can then run it using sudo ncdu -rx / (for your entire machine) or ncdu . on the current directory.

Based on running this I found that node_modules and target were using the most space. These are both temporary folders for NodeJS and Java respectively.

I did not want to have to go into each folder to remove these so I went to my code directory and ran the following find command which finds and deletes node_modules:

find . -type d -name "node_modules" -exec rm -fr {} \;

Similarly I ran the following command to remove the target directories:

find . -type d -name "target" -exec rm -fr {} \;

If you are worried about running these commands you can first run the commands without deleting e.g.:

find . -type d -name "target"

You can also use this to confirm that you have deleted the items in question.