MOVING!!!

Hey there, after several years of playing with Blogger and trying to get thing sorted out how I wanted, I finally decided to host my own domain so I could manage things easier. So this blog (which, admittedly, hasn't been updated much) is getting moved to my all-new site: DavisTobias.com/Linux. Also, to make it easier to transfer RSS feeds, this is the link to the new RSS feed. I'll leave this site and it's posts up, so I don't contribute to dead links on the internet, but I'm shutting off comments and won't post any more here.

April 19, 2009

Recursively deleting things

Hey there, this site has moved, so comments are disabled. Thankfully, you can go to the page, carefully linked for your satisfaction. Click here to go there.

I wanted to delete the incessant "Thumbs.db" that Windows leaves everywhere, and found three possible solutions:
rm `find /path -name '*.tmp'`
find /path -name \*.tmp | xargs rm
find /path -name "*.tmp" -exec rm {} \;

The first two probably work when there are no spaces in the folder names. The last one works even when there are spaces. I will explain all three.

First: rm `find /path -name '*.tmp'`

rm This is the "remove" command. You would say rm file to remove "file"
`find ....` Quotation marks are used to wrap things that may be interpreted as two commands. E.g., if you say rm find blah the computer will look for two files, "find" and "blah". The quotation mark found usually to the left of the 1 key is done to let the computer know you want to pass a command. Try running the find /home/user/path -name '*.tmp' by itself. Let's pull it apart:
find This is the command which is self descriptive
/path This is the path where it should start. It will look recursively through all farther out folders.
-name '*.tmp' The -name is a flag, letting the computer know that the next thing is the file it should look for.
'*.tmp' Why does this have quote marks? It doesn't really need it in this example, but if you were looking for a file with spaces in it you would need it.

Second: find /path -name \*.tmp | xargs rm

find /path -name \*.tmp This command is discussed above, the difference to note is the \*.tmp which I don't know why it is there. Sorry. Anybody?
| This is the "pipe" symbol. It takes whatever the left side makes, and passes it to the right side. In this case, the left side finds the file *.tmp and the pipe passes the location of that file to the right side.
xargs rm The xargs command is basically a way to pass commands, the important thing is that it is passing a command to do rm which will remove the file found on the left side of the pipe.

Third: find /path -name "*.tmp" -exec rm {} \;

find /path Find in the directory "/path"
-name "*.tmp" Look for the file with an extension of "tmp". If you want it to look for "TMP" as well, use -iname which is case insensetive
-exec rm {} \; The "-exec" is a flag which tells the computer to execute something, in this case the remove command, after it finds a file. Whatever command you pass has to be followed by a semicolon ; So it would be -exec blahblah ; complete with the space before the semicolon. The backslash character makes sure that the computer knows it is done doing whatever command it was supposed to do, in this case remove the file.
rm {} The rm command removes (deletes) a file. The brackets are a recursive thing, to the computer they mean "Whatever you just found".