Home > OS >  How to exclude -sources pattern in bash with space in file name
How to exclude -sources pattern in bash with space in file name

Time:05-26

I want to use bash command line to match the file name "file 9.3.0.zip".

Here is the test script for explaining the folder structure:

mkdir -p /tmp/test
touch "/tmp/test/file 9.3.0.zip"
touch "/tmp/test/file 9.3.0-sources.zip"

cd /tmp/test

If the folder does not contain any "-sources.zip", then I can use "echo file*.zip" to match "file 9.3.0.zip".

But now it also contains "-sources.zip" in it, I have tried the following pattern but still doesn't work:

echo file**!(-sources).zip

Here is the output:

file 9.3.0-sources.zip file 9.3.0.zip

How to write the pattern in bash command?

I will use this match only one file pattern in other bash command, for example:

unzip file*.zip -d /tmp/other

Thanks.

CodePudding user response:

I think the issue is that the * in file*!(-sources).zip can match something that means the remainder of the filename is not -sources.zip.

This seems to work, but I haven't tested it thoroughly:

file!(*-sources).zip

This also seems to work (but I don't recommend it!):

!(!(file*.zip)|file*-sources.zip)
  • Related