tar exclude files *.zip

5,677

You will need to quote the exclusion pattern, mydir/files/*.zip, otherwise the shell will expand it (globbing), possibly/probably matching a number of files, and the meaning of you command line will be very different from what you intended.

This means changing you original tar command line into

$ tar cfjv backup.tar.bz2 --exclude "mydir/files/*.zip" mydir

I initially thought there was something up with the cfjv switches. These are ancient command line switches and tar handles them very differently from more "modern" command line switches (prefixed by -). That's why you do not get an archive called jv even though that's what's following the f switch.

The manual for tar on Mac OS X mentions this under the "COMPATIBILITY" section:

The bundled-arguments format is supported for compatibility with historic implementations. It consists of an initial word (with no leading - character) in which each character indicates an option. Arguments follow as separate words. The order of the arguments must match the order of the corresponding characters in the bundled command word. For example,

      tar tbf 32 file.tar

specifies three flags t, b, and f. The b and f flags both require arguments, so there must be two additional items on the command line. The 32 is the argument to the b flag, and file.tar is the argument to the f flag.

Share:
5,677

Related videos on Youtube

jiffy.of.eternity
Author by

jiffy.of.eternity

Updated on September 18, 2022

Comments

  • jiffy.of.eternity
    jiffy.of.eternity over 1 year

    Why don't *.zip patterns work in tar :

    tar cfjv backup.tar.bz2 --exclude mydir/files/*.zip mydir
    

    Is there another syntax?

    • Kusalananda
      Kusalananda almost 9 years
      What is the output of that command? Don't you have to have the f flag last before the archive name, i.e. cjvf backup.tar.z2? Otherwise it would probably create an archive called jv.
    • jiffy.of.eternity
      jiffy.of.eternity almost 9 years
      @Kusalananda It works with quotes: --exclude "mydir/files/*.zip" , thanks!
    • Kusalananda
      Kusalananda almost 9 years
      Good! Yes, I saw later in the manual for tar that those very old-style command line switches are handled differently.
    • Kusalananda
      Kusalananda almost 9 years
      Done, with additional explanation.