This question already has an answer here:

I am working on a script that contains a line similar to

"command /path/to/directory/*example*"

If the directory contains files that are named 1example.txt, example.tmp, example.war it will run the command on all of them. I would like for the command to be run on all of them except for the .war. Is there some way of doing this cleanly? The "/path/to/directory/*example*" is a string that is passed through.

marked as duplicate by Ipor Sircer, Jeff Schaller, Stephen Rauch, Wildcard, Rahul Nov 30 '17 at 5:16

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.

  • Using ! in the string results in an unexpected bash event – K123 Nov 30 '17 at 1:13
  • Did you set extglob? – Jeff Schaller Nov 30 '17 at 1:23
  • I can't use extglob :( – K123 Nov 30 '17 at 1:36
  • Why are you accepting a string from a user that is allowed to contain a glob? That's a very bad idea. What happens when a user passes /*/*/*/*/*/../../../../../*/*/*/*/* and your machine crashes? Why not just accept a directory name and a literal string to be accepted as a substring? – Wildcard Nov 30 '17 at 3:04

Use find:

find ~/tmp/example -type f ! -name '*.war' -exec echo {} \;
  • 1
    So long as there aren't any directories this is fine, otherwise it does a recursive search which was probably not wanted. But it doesn't filter for example either. – Wildcard Nov 30 '17 at 3:01
  • Is there a way to do it without a command? Kind of like how "*" means any character is there a not this character? Its going to be used as an input for a "sort" command – K123 Nov 30 '17 at 4:39

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