Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am looking for a way to do something that can find all files less than size except filenames with an extension or more. May be more than one.

find . -type f -size -20M -name /! -name *.mp3 -delete

From some of the things I found online I thought that this would be what I was looking for but it is not working. Pretty much I am looking for this

find . -type f -size -/+ size (except) *.ext -delete

What do you think?

share|improve this question
up vote 0 down vote accepted

! is the find not operator. So to exclude files ending in .ext from the results, include the term ! -name \*.ext.

For numeric arguments such as file size, a prefix of + means greater than and a prefix of - means less than. So to find a file larger than 12 kilobytes (really kibibytes) and smaller than 500 Megabytes (really Mebibytes), include the terms -size +12k and -size -500M.

Your second example would then look something like this.

find . -type f -size +12k -size -500M ! -name \*.ext -delete
share|improve this answer
    
Thank you so much. I use it to clean up a folder automatically for me. so if I wanted to not delete multiple extensions would it be find . -type f -size +12k -size -500M ! -name *.ext *.ext -delete – natethegreat141990 Apr 16 '14 at 0:30
    
A file name with one or more extensions contains at least one dot, so it would match the pattern *.*. A file name with two or more extensions contains at least two dots, so it would match the pattern *.*.*. If you want to make sure that each extension contains at least one character, you could replace those patterns with *.?* and *.?*.?*, respectively. It really depends on the file names you're dealing with. You want a pattern no more complicated than necessary to distinguish one set of files from the other. And of course test it before using it to delete anything. – garyjohn Apr 16 '14 at 1:18
    
sorry there is a space between those. I was meaning *.ext and *.ext? – natethegreat141990 Apr 16 '14 at 19:29
    
If you want to delete only those files whose extension is not .ext and is not .ext?, then you could use find ... ! -name \*.ext ! -name \*.ext\\? -delete. Note that if that extension really contains a question mark, the question mark will have to be escaped so that it is seen by find as a literal question mark and not as the symbol for "any single character". – garyjohn Apr 16 '14 at 21:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .