1

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?

0

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

What if the file is roarroar.java, this will not get ignored despite roar being a substring.
!(*roar*|*gumbo*).java otherwise with find find . -maxdepth 0 -name '*.java' \! -name '*roar*.java' \! -name '*gumbo*.java' -exec printf '%s\n' {} +
@Nahuel I think it's more complicated than that, as the * need to be optional
@TomFenech, no because * matches null string, however @() is a good idea can save 2 *
@Nahuel you're right about the *, thanks, I edited.
0

Try using the find command:

find . -type f -not -name '*roar*' -and -not -name '*gumbo*' -exec <command> {} \;

For example:

find . -type f -not -name '*roar*' -and -not -name '*gumbo*' -exec javac {} \;

Comments

0

I would use a standard loop:

for file in *.java; do
  case $file in
  *roar*|*gumbo*) continue;;
  *) javac "$file";;
  esac
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.