Kavitha
Kavitha

Reputation: 151

Exclude two or more directories in bash for loop

I am having a parent directory called Stem in that parent directory I am having around 20 sub-directories and 20 sub-directories names are ending with _a, and each sub-directory is having a file called violations.txt. In those 20 sub-directories, two sub-directories name are Trans_a and shift_a, I do not need to perform any script action on these two sub-directories. I need to execute my script on only 18 sub-directories.

I tried the below code but not getting the exact output.

#!/bin/bash
echo "These warning reports are based on this run directory:" ; pwd
echo " " ;
File="violatons.txt"
for d in *_a;
do
if ( "$d"=="Trans_a"||"shift_a" ); then
True
else
    Statement 
fi
done

Note : In some situations the sub-directories which we need to exclude in script may increase or decrease, it depends upon the situation.

Upvotes: 1

Views: 331

Answers (1)

thanasisp
thanasisp

Reputation: 5975

for f in !(Trans|shift)_a/violations.txt; do
    echo "$f"
    # do stuff
done

!(pattern-list) Matches anything except one of the given patterns.

Reference: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Pattern-Matching

Upvotes: 3

Related Questions