0

I ran the following command to zip all files starting with db- and ending with .sql:

zip /var/www/html/db-$(date +\%F-\%T).zip /var/www/html/^db-*.sql

Yet I get:

zip warning: name not matched: /var/www/html/^db-*.sql

zip error: Nothing to do! (/var/www/html/db.zip)

As I used a caret to match all starting with db-, and than added in *.sql in the end to match all ending with .sql, I don't understand my mistake.

I tried other variations with ^ but all failed with a similar error.

3

Bash parameters are not regular expressions, they are globs. Use:

$ zip /var/www/html/db-$(date +\%F-\%T).zip /var/www/html/db-*.sql

From bash's manual, under "Pattern Matching":

* Matches any string, including the null string. When the globstar shell option is enabled, and * is used in a pathname expansion context, two adjacent *s used as a single pattern will match all files and zero or more directories and subdirectories. If followed by a /, two adjacent *s will match only directories and subdirectories.

  • Also, filename globbing patterns are anchored by default, so db-*.sql would match any filename starting with db- and ending with .sql. Regular expressions are different. – Kusalananda Aug 9 '17 at 16:09
  • You'd also want to add the -nw option to avoid zip doing its own wildcard interpretation (in addition to the shells), or quote the glob for the shell so that it's done in zip instead. (or use a more unixy command like bsdtar to create the zip file to avoid this kind of msdos-like behaviour). – Stéphane Chazelas Aug 9 '17 at 16:11
  • I think I miss why regex can be used in sed or awk or grep (AFAICR), but not on zip. – JohnDoea Aug 9 '17 at 16:21
  • That's because sed and awk and grep are regular expression tools, built to use them (the 're' in grep actually stands for 'regular expression' -- Global Regular Expression Print -- from its origins in (as I recall) an ex (or possibly ed) command). Shells such as bash and tools such as zip are not. – DopeGhoti Aug 9 '17 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.