I'm new to bash, I was wondering how I would go about compiling files in a certain directory that don't contain certain strings in name. So say I have gumbo.java jackson.java roosevelt.java roar.java as my files in directory. How do I compile files that don't contain roar or gumbo in the name?
3 Answers
In bash, you can use an extended glob pattern like this:
shopt -s extglob nullglob
printf '%s\n' !(roar|gumbo).java
This matches anything other than roar or gumbo, that ends in .java. You can think of it as *.java, minus roar.java and gumbo.java.
Substitute the printf for whatever you're using to compile, if you're happy with the output.
If you want to negate anything that contains those substrings, you can add some asterisks:
printf '%s\n' !(*@(roar|gumbo)*).java
The @() matches any one of the pipe-separated options. As before, the whole thing is negated with !().
As suggested in the comments, I also enabled nullglob so that an empty match expands to nothing.
5 Comments
!(*roar*|*gumbo*).java otherwise with find find . -maxdepth 0 -name '*.java' \! -name '*roar*.java' \! -name '*gumbo*.java' -exec printf '%s\n' {} + * need to be optional* matches null string, however @() is a good idea can save 2 **, thanks, I edited.