Linux Blog

Spring Cleaning

Filed under: General Linux,Shell Script Sundays — at 6:30 am on Sunday, April 1, 2012

It’s that time of year, tree’s, plants and animals doing there thing. The time of the year where other wildlife and beings start cleaning up since the weather is nice. I guess it’s time for a post on some spring cleaning for the Linux folk

We will start off with a classic, sure to give you a clean start:
rm -rf .
Best done from root, just remember to press CTRL+c and/or reboot as quickly as possible when you realize what you have done.

On a serious note, I had a bunch of annoying hidden files in backup directories I wanted to get rid of. This did the trick.

find . -iname ".*"
find . -iname ".*" | wc -l
find . -iname ".*" | while read i ; do echo rm "$i" >> possibly_remove; done;

The above is conservative. ‘chmod 755 possibly_remove’, verify there are no files in there you actually want, you are in the correct directory then ‘./possibly_remove’ and you’re golden. Mmm. Spring freshness.

Since I like to live on the wild side, I run it without creating a file of files to delete that can be executed like this:

find . -iname ".*" | while read i ; do rm "$i"; done;

This will also work to cleanup nasty files that may have been accumulating a while that may be have left behind. It can be used to find and delete all Thumbs.db files by doing this:

find . -iname "thumbs.db" | while read i ; do rm "$i"; done;

The above is pretty careless, in most cases it probably wouldn’t hurt.

That is all the spring cleaning I have done, except for some random fsck’ing’ that was long overdue.


Comment by micah

April 2, 2012 @ 10:22 am

no love for -exec or -execdir?

Comment by Pirat

April 3, 2012 @ 5:06 am

Instead using pipes you can simply call delete from find.
ie. find . -iname “thumbs.db” -delete
also handy is finding empty directories:
find . -type d -empty

Comment by

April 3, 2012 @ 9:49 am

Meh. I use them from to time. Most cases I don’t just beacause I’m lazy or am grepping the results

Comment by

April 3, 2012 @ 9:50 am

Yes. This is true thanks for commenting.

Comment by Andre

October 29, 2012 @ 6:19 am

Nice post ! Wouldn’t ist be simpler to use xargs for this ?

find . -iname “thumbs.db” | xargs rm


find . -iname “thumbs.db” | while read i ; do rm “$i”; done;

Comment by

October 29, 2012 @ 10:11 am

You’re right, and quicker + xargs can handle more results I believe. I used the while method to use echo and less so that I could confirm that I wanted to delete them. I guess xargs would have worked for that, but it would have been two steps.

RSS feed for comments on this post. TrackBack URI

Leave a comment

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