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 attempting to negate a find command so that I get every other file except for the file or files in the command.

find . -maxdepth 1 -regextype posix-egrep -regex '.*/.*\.svg' -exec echo {} \;

Based on my last post it appears I have to place -not ahead of -regex.

If I attempt to execute the command, it fails i.e. it returns the message it is not allowed or am I doing it incorrectly?

find . -maxdepth 1 -not -regextype posix-egrep -regex '.*/.*\.svg' -exec echo {} \;


find . -maxdepth 1 ! \( -regextype posix-egrep -regex '.*/.*\.svg' -exec echo {} \; \)
share|improve this question
try keeping the -exec echo... outside of the parentheses – golimar Apr 22 '13 at 11:27
up vote 1 down vote accepted

Unless you have requirements not mentioned in your question you are needlessly complicating matters. Just try something like this:

find . -maxdepth 1 -type f | grep -v *\.svg

find will print the file names it finds by default; you do not need to echo {}. You also don't need the regex at all; you could simply run:

find . -maxdepth 1 ! -name "*.svg"

In any case, even if you did need the regex, you are using a needlessly complex one. *\.svg would do just as well as .*/.*\.svg; since all you are interested in is the end of the file name, there is no need to specify anything about the parent directory.

That is, unless you want files whose name starts with a literal ., if so, run

find . -maxdepth 1 ! -name "\.*\.svg"

If you really need the regex (which I stress you do not for what you state in your question) you can negate it just like any other flag:

find . -maxdepth 1 -regextype posix-egrep ! -regex '.*/.*\.svg'

! means not. Its placement is actually quite logical, it is right before whatever it is you want to negate. In the example above you are negating the regex, not the regextype which is why the ! goes right before -regex.

share|improve this answer
Thanks. The reason for using regex is to match particular patterns. How woukd I negate regular expressions aside from using -not preceding -regex? – PeanutsMonkey Apr 22 '13 at 19:34
@PeanutsMonkey See my updated answer for how to negate the regex as you asked. I am still 90% certain you are using a command that is way too complicated for what you need to do. Try updating your question with the actual file names or extensions you want to match/not match and leave me a comment here so I can have a look. – terdon Apr 23 '13 at 0:56

You must log in to answer this question.

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