I needed to find all folders, whose names have a digit in them, that don't contain a file ending with .counts.txt. By combining several suggestions I found online, I was able to come up with the following command (for bash), which does what I want.

find . maxdepth 1 -type d -name "*[0-9]*" ! -exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' \; -print

However, I don't understand some of its features:

  1. What does the exclamation mark do here? Does it somehow negate the condition statement that follows?
  2. Why is it necessary to open a new shell using sh rather than directly piping to ls?

1 Answer 1

  1. Yes the ! negates the next condition, that is the -exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' \;. As this is an -exec condition that is negated we have to remeber that it is true if the command it executes returns with status 0 (and negated the condition is true if the status is not 0).
  2. The arguments after -exec are not passed to a shell but instead are executed directly so there is no concept of a pipe available. If you would put a pipe character there the outer shell (which started find itself) would use that and all the output from find would go to egrep.

See man find for the docs on the ! and -exec args for find.

You must log in to answer this question.

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