pecar
pecar

Reputation: 55

bash: using the conditional AND OR in if satement

I want to select files based on begin and end of file name. I wrote the code below but still having problems with my code, it is not printing anything.

Details: My script below should loop over files places in the directory $path and check if the file name begin with (${t} or X${t} and ends with ".bz2") remov the file from repository.

t="T"
for f1 in $path/*; do
    if [[ ("$f1" == "${t}"* || "$f1" == "X${t}"*) && ("${f1: -4}" == ".bz2")]]; then 
       echo "IM here to remove files"
    fi

In my $path i have these files: XT&20220202ffff.bz2, T&2022020mmmmmm.bz2, LMMMM.bz2

Input are files in the $path output is removing the files.

Upvotes: 0

Views: 48

Answers (1)

M. Nejat Aydin
M. Nejat Aydin

Reputation: 10123

You don't need a for loop nor the [[...]] conditional construct. All you need is pattern matching:

t="T"
echo rm "$path"/{X,}"$t"*.bz2

Drop the echo if the output looks ok.

Upvotes: 1

Related Questions