1

In a shell pipeline, I compose a tar archive from a selection of files under a directory, taking the list of files from stdin, and write the archive to stdout. When I run tar like this:

tar cCTf directory - -

everything is fine. But when I try to run it like this:

tar cTCf - directory -

tar does not find the files, and I get the following error message:

tar: The following options were used after any non-optional
arguments in archive create or update mode.  These options
are positional and affect only arguments that follow them.
Please, rearrange them properly.
tar: -C ‘directory' has no effect

What is wrong with the second command?

I am running GNU tar 1.34.

1
  • Are you able to install bsdtar? apt install bsdtar then bsdtar cTCf - directory - > output.tar works on my Ubuntu test machine.
    – Jim L.
    Commented Aug 30, 2022 at 19:50

2 Answers 2

2

This error...

tar: The following options were used after any non-optional arguments in archive create or update mode. These options are positional and affect only arguments that follow them. Please, rearrange them properly.

tar: -C ‘directory' has no effect

...is telling you that that the C option is position-dependent. It effect only options that come after it. So when you write cTCf, with -C after -T, it is effectively a no-op. It won't effect the way tar interprets filenames read by -T, and there's nothing after it to which it can apply.

This might be easier to understand when you consider that you can have multiple -T and -C options on the command line, e.g:

tar cCTCTf /path/to/dir1 filelist1 /path/to/dir2 filelist2 -

Each -C argument effects the following -T argument (but not any preceding ones).

-1

If you're not wedded to GNU tar, bsdtar does what you want, with your desired syntax:

$ find . -ls
 13107306    4 drwx--x---   3 user   grp       4096 Aug 30 12:54 .
 13107938    4 drwx--x---   2 user   grp       4096 Aug 30 12:38 ./directory
 13111189    4 -rw-------   1 user   grp         29 Aug 30 12:37 ./directory/a
 13113837    4 -rw-------   1 user   grp         29 Aug 30 12:37 ./directory/b
 13114062    4 -rw-------   1 user   grp         29 Aug 30 12:37 ./directory/c
$ printf "%s\n" a b c | bsdtar cTCf - directory - > output.tar
$ tar tvf output.tar 
-rw------- user/grp        29 2022-08-30 12:37 a
-rw------- user/grp        29 2022-08-30 12:37 b
-rw------- user/grp        29 2022-08-30 12:37 c

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .