I am trying to run a for loop but trying to exclude files with a specific pattern in the filename. This is the command I am using:
for file in /home/ubuntu/discovery/download/*![.en].mp4; do
I would like this filename to be included:
Blub - S03E10 - Nervous Breakdown.mp4
but this filename to be excluded
Blub - S03E10 - Nervous Breakdown.en.mp4
Can’t get it to work. What am I doing wrong?
Thanks in advance
4
Answers
Standard globbing has no grouping or group negation operators (though the extended version has these). In your particular context, I would just filter out the unwanted files inside the loop:
And that should work in any Bourne-family shell, not just Bash.
Pathname Expansion: "match not", with
extglob
enabled:Try this:
Another approach is to iterate over all files, skipping any that have undesirable name patterns. Just make sure to also handle the case where no files are found, because in that case,
file
is set equal to the glob pattern. Often you can handle that situation by verifying that the path infile
actually exists (-e "${file}"
).Another alternative could be: