13

This is my current directory structure on my MacOSX-Machine:

kuli at fumpenwuppich in /volume/workspace on master [!?$]
$ ls -l
total 0
drwxr-xr-x   4 kuli  staff  136 Nov 28 16:02 conf
drwxr-xr-x   3 kuli  staff  102 Nov 28 16:23 html
drwxr-xr-x   3 kuli  staff  102 Nov 28 13:06 legacy
drwxr-xr-x  11 kuli  staff  374 Nov 28 15:41 vagrant.nginx

I want to create a tar.bz2-file as a backup, but without the sub directory 'legacy'.

I've tried multiple things:

tar cfjv --exclude 'legacy' ~/Dropbox/backup/kuli.20151128.tar.bz2 .
tar cfjv --exclude='legacy' ~/Dropbox/backup/kuli.20151128.tar.bz2 .
tar cfjv ~/Dropbox/backup/kuli.20151128.tar.bz2 --exclude legacy .
tar cfjv ~/Dropbox/backup/kuli.20151128.tar.bz2 --exclude=legacy .
tar cfjv ~/Dropbox/backup/kuli.20151128.tar.bz2 --exclude='legacy' .
tar cfjv ~/Dropbox/backup/kuli.20151128.tar.bz2 --exclude './legacy' .
tar cfjv ~/Dropbox/backup/kuli.20151128.tar.bz2 --exclude='./legacy' .
tar cfjv ~/Dropbox/backup/kuli.20151128.tar.bz2 --exclude 'workspace/legacy' .

and so on. I've also tried to put the exlude-switch at the end of the command.

But nothing worked. All commands ignore the exclude command at all.

How to achieve this?

2 Answers2

17

According to man page options go before files and directories list:

tar {-c} [options] [files | directories]

Additionally -f and --exclude options require parameters: -f file and --exclude pattern.

So, in your case, this should work:

tar cjv --exclude 'legacy' -f ~/Dropbox/backup/kuli.20151128.tar.bz2 .

baf
  • 4,531
  • 1
  • 21
  • 24
  • Sorry @baf, you are misreading the 'man' page. The notation "[files | directories]" indicates the list of files that are to be included in the archive; this has nothing to do with "-f". The "f" is an option for naming the target file; as an option, it goes with the placeholder notation "[options]". There is no indication about the ordering. I am testing on Darwin 19.6.0, which is macOS Catalina 10.15.7, and I cannot reproduce the error in the question; the functionality of "--exclude" does not depend on order. However, I do have a problem getting "--exclude" to work with "--files-from". – IAM_AL_X Apr 27 '21 at 19:28
  • @IAM_AL_X I am not misreading. I refer to `file`, which is obligatory parameter for `-f` option, not `files | directories`. Another thing is you are using modern macOS version, which uses different tar version. This response will not help you. – baf Apr 30 '21 at 16:47
17

Curiously, I could not get this to work on Mac OS X (version 10.11.5) until I rearranged the order of parameters to something like

tar --exclude '/subdir1/' --exclude '/subdir2/' -zcvf "mybackup.tar.gz" ./myfolder

KenGCL
  • 171
  • 1
  • 2
  • not an expert but I believe it makes sense. The f in -zcvf is the same f @baf is using in his answer. If you try tar -zcvf --exclude './a/b' foo.tar.gz ./a/ your option is where you f parameter should be. tar -zcv --exclude './a/b' -f foo.tar.gz ./a also works. – igarciaoliver Jul 11 '17 at 03:44
  • Reinforcing what @igarciaoliver said, the "f" is an option and must be followed by its parameter. The "-f" introduces the name of the target file; there can be no "--" type option between the switch "f" and the parameter it introduces. Switches can be bundled, as in "cvf" or "-cvf". Apparently the "f" need not be the last switch in a bundle of switches, but I personally would never use "cfjv" as in the question ("f" is followed by two more switches, "j" and "v"), I would always make "f" the last switch in the bundle, as in "cjvf"; this connects the "f" with the parameter it is introducing. – IAM_AL_X Apr 27 '21 at 19:38