Join 3,572 readers in helping fund MetaFilter (Hide)


Datascrubing
July 16, 2004 8:37 AM   Subscribe

datascrubbing: I'm using rm -R */*.gif (for example) to get rid of unnecessary files in a directory. There is stuff that's nested about five directories deep that I can't get to using the above command. I'm using the Bash terminal in OS X. specifically, there is a redundant file called "popup" nested in there that I want to get rid of, among others.

Second related problem: is there a way to recursively delete (full) directories, say ones that end in .blob using wildcards? And would I use rmdir or rm -R to do this?

I'm working on a copy of the original, so its ok to experiment a little
posted by mecran01 to Computers & Internet (18 answers total)
 
Globs probably won't do what you need, at least under any normal shell.

Check out the 'find' command: it's made for stuff like this. Unfortunately, the syntax is pretty rough; and of course recursively deleting things is *very* risky, even for seasoned Unix pros.

For your first question, this command should do the trick (when executed from within the directory you want to start):
find . -name '*.gif' -exec rm {} \;

If you want it to be safer, but tedious:
find . -name '*.gif' -exec rm -i {} \;

For your second question, this should work (again, navigate to the directory from which you want to begin):
find . -type d -name '*.blob' -exec rm -r {} \;
posted by Eamon at 8:42 AM on July 16, 2004


There's many ways to do these things, here are my current preferences.

first question: experiment with find/exec

e.g. find ./ -name blahblah -exec rm -rf {} \;

Use find without the -exec just to see what it finds. the {} in the exec statement gets replaced with the file name

second question: for dir in *.blob; do rm -rf $dir;done
posted by winterdrm at 8:43 AM on July 16, 2004


Perfect! Thanks very much!

ask.metafilter: the usenet you dreamt of.
posted by mecran01 at 8:49 AM on July 16, 2004


I find the find syntax painful, especially for removing files. That's why I combine it with the xargs command, like so:

find . -name "*.removeme.*" -print | xargs rm

If you're being especially careful, then you can do:

find . -name "*.removeme.*" -print > ExamineMe

look at ExamineMe and then

cat ExamineMe | xargs rm
posted by rdr at 9:09 AM on July 16, 2004


For your first question, this command should do the trick (when executed from within the directory you want to start):
find . -name '*.gif' -exec rm {} \;


Note that with rm in a find command you always want to use /a/hard/path, never . for current directory.
posted by the fire you left me at 9:42 AM on July 16, 2004


why? (curious).

another way of doing the same thing:
rm ` find . -name "*.removeme.*" -print`
which may be specific to certain shells (afaik works with bash + csh)
posted by andrew cooke at 10:39 AM on July 16, 2004


dude

rm -rf
posted by cheaily at 11:00 AM on July 16, 2004


(in case it wasn't obvious, my "why" was about the explicit hard path)
posted by andrew cooke at 11:16 AM on July 16, 2004


I'm still waiting for an explanation on the hard path too. I've been using "find ." for years, and tutorials all over the web are littered with it.
posted by Galvatron at 11:19 AM on July 16, 2004


From the command line 'find . -name something -exec rm {} \;' is standard. When executed as a shell script, a hard path is more desirable, I think, as the 'rm' command would be executing relative to the location of the shell script when piped something from 'find .' .
posted by the fire you left me at 11:44 AM on July 16, 2004


Oh, galvatron, etc, that's because if you integrate your find command into a script, it'll do unknown bad things. Well, that's my 2 cents on it, anyways.

Otherwise... I don't see a problem, except that if your find command does rmdir it could wipe out the directory you are in. That's not good, but not a big deal, either. :-)
posted by shepd at 11:44 AM on July 16, 2004


OK, that's sensible. Thanks for the simultaneous clarifications.
posted by Galvatron at 11:58 AM on July 16, 2004


yep. ta.
posted by andrew cooke at 12:22 PM on July 16, 2004


I'd like to claim that zsh is a normal shell; Well, normal and totally wonderful.

/bin/zsh
rm **/*.gif


** is recursive; See man zshexpn for the full story.
posted by fvw at 3:25 PM on July 16, 2004


I was able to reduce 916 mb of scraped guitar tabs down to 50mb using these suggestions. I thank you.
posted by mecran01 at 4:50 PM on July 16, 2004


I find the find syntax painful, especially for removing files. That's why I combine it with the xargs command...

Be careful with xargs: it will break if any of your filenames contain spaces. In these cases, you need to combine find's '-print0' option with xargs's '-0' option. Of course, these options are nonstandard, and are (AFAIK) only available in the GNU implementations of these tools.

Well, normal and totally wonderful.

A shell cannot be both normal and wonderful.
posted by Eamon at 5:03 PM on July 16, 2004


A variant on the ways suggested of being careful: whenever I do something complex like this, I always test it by putting "echo" right before the "rm" so that I can see exactly what commands will get executed. I highly recommend this unless you're really familiar with the shell's/various commands' syntaxes. I find that I almost always make some mistake, and I've been burned before when not being careful.
posted by advil at 9:26 PM on July 16, 2004


ask.me needs automatic micropayments.
posted by mecran01 at 9:12 AM on July 17, 2004


« Older I downloaded Picasa at the urg...   |  for the lawyers: i'm three ... Newer »
This thread is closed to new comments.