Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

This question already has an answer here:

My preview post is about removing the single name file in the system but I actually need to remove big range of files.

previous post

How to remove files in different folders?

For example

They are junkImg-5.png to junkImg-60.png, ,meaning there are junkImg-30,junkImg-45...etc.

Are there any quick command to remove them in the command line?

I have tried

find /project/test/ -name 'junkImg-5*' -exec rm -f {} \ ;

but it's way too slow and I have so many images. Is there a way that linux can specify the range? Thanks a lot!

share|improve this question

marked as duplicate by Oliver Salzburg May 22 '13 at 21:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
meaning there are junkImg-30, junkImg45... - can you clarify what you mean? –  suspectus May 22 '13 at 17:37
    
I mean there are many files named like junkImg-10,junkImg-11,junkImg-12 all the wy to junkImg-60 –  FlyingCat May 22 '13 at 17:44
    
Just rm junkImg-* -f doesn't work? -f prevents the "are you sure" message. –  Nathan C May 22 '13 at 17:47
    
I need to keep junkImg-1 to junkImg-4, so rm junkImg-* won't work –  FlyingCat May 22 '13 at 17:48

3 Answers 3

up vote 5 down vote accepted

Just use brace expansion to delete the range of files:

rm /project/test/junkImg-{5..60}.png

This will remove junkImg files 5 though 60, with only one rm process. You can also first substitute the rm command with ls to make sure it is selecting the files you want, then replace it again with rm.

share|improve this answer
    
If all images are in the same directory, this is definitely the way to go. The link to the OP's previous post (How to remove files in different folders?) made me think otherwise. –  Dennis May 22 '13 at 20:04

Find uses Shell Pattern Matching for the -name predicate, so you can use the -or predicate and these patterns:

find DIRECTORY -name 'junkImg-[5-9].png' -or -name 'junkImg-[1-6]?.png' -delete

You can also exclude the first four files by using the -not predicate:

find DIRECTORY -name 'junkImg-*.png' -not -name 'junkImg-[1-4].png' -delete

To test if the commands work properly, always run them without -delete first. This will list the files instead of removing them.

share|improve this answer

You are starting a new rm command for each deletion. That is not the fastest way. You only need to do that if you have that many files that you can not list them all in one go.

TO make things faster, either:

  1. change the -exec rm with -ls and pipe it though xargs. Toss in a -print0 if you expect some of the filenames to be difficult (e.g. have a space in them).
  2. Or better, use finds build in -delete.
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.