5

I want to create a .tgz from the content of a directory. I also want to strip the leading "./" from the tar'ed content.
I had done this as follows:

cd /path/to/files/ && find . -type f | cut -c 3- | xargs czf /path/to/tgz/myTgz.tgz

I learned recently that using xargs may not be the best way to pull this off because xargs may invoke tar multiple times if the cmdline arg list gets too long, and I was advised to make use of tar's ability to read a list of input files from stdin. I ended up finding this article on how to do this. However, I find that the recommendation...

cd /path/to/files/ && find . -type f | cut -c 3- | tar czf foo.tgz -T -

...seems to not be portable. It runs fine on my dev PC, but on a busybox target, I get the following error from running the same command:

tar: can't open '-': No such file or directory

So, my question: is there a truly portable/global way to invoke tar to create a .tgz by feeding it input files from stdin (as opposed to cmdline arguments)?

(It is not an option available to me to install alternatives to tar such as gnutar/bsdtar/etc.)

(Secondary question: Why does the "-T -" argument to tar denote "read files from stdin"? From the tar man page, all I could find was that "-T" means:

get names to extract or create from FILE

... but I couldn't see any reference to a plain "-")

  • 3
  • 2
    Since tar is not a POSIX utility, a truly portable (as in "standard") implementation may not be found. pax, on the other hand, is a POSIX utility. – Kusalananda Jan 3 at 19:31
  • This site does one question per question, and that tertiary question is a massive distraction that is a question in its own right, most likely already long since covered. – JdeBP Jan 3 at 19:40
  • @JdeBP - removed tertiary question – StoneThrow Jan 3 at 19:43
  • @Kusalananda - that's an informative comment; thank you. However, it looks like pax isn't available in Busybox (at least in the version of Busybox I have to work with). I think I may have to resign myself to using the sub-optimal approach using xargs. – StoneThrow Jan 3 at 19:47
6

It is a common convention to interpret - to mean standard input where an input file name is expected, and to mean standard output where an output file name is expected. Because this is a common convention, the short help summary in the GNU tar man page does not mention it, but the complete manual (usually available locally through info tar) does. The POSIX command line utility syntax guidelines includes this convention, so it's pretty widespread (but it's always a choice on the part of the author of the program).

BusyBox utilities do follow this convention. But the manual does not mention tar as supporting the option -T, and neither does the version on the machine I'm posting this (1.27.2 on Ubuntu). I don't know why you're getting the error “: No such file or directory” rather than “invalid option -- 'T'”. It seems that your tar interprets -T as an option that does not take an argument, then sees - as a file name. Since in this context tar needs a file name to put in the archive, and not just some content that comes from a file, it would not make sense to use the stdin/stdout interpretation for -.

BusyBox utilities support a restricted set of functionality by design, because they're intended for embedded systems where the fancier features of GNU utilities wouldn't fit. Apparently -T is not a feature that the BusyBox designers considered useful.

I don't think BusyBox tar has any way to read file names from stdin. If you need to archive a subset of the files in a directory and you don't need any symbolic links in the archive, a workaround is to create a forest of symbolic links in a temporary directory and archive this temporary directory.

It's not clear exactly why you're using find. If you only want the files in the current directory, why not tar czf /path/to/archive.tgz -- * ? Your command does make sense if there are subdirectories and you want to archive the files in these subdirectories, but not the directories themselves (presumably to restore them in a place where the directory structure must exist but may have different permissions). In this case a leading ./ wouldn't do any harm.

4

I know that is a stupid answer and maybe not good for you but how bad is if you just...

cd /path/to/files/ && find . -type f | cut -c 3- >/tmp/temp.txt 
tar czf foo.tgz -T /tmp/temp.txt
  • Not at all a stupid answer. The original question (and the reason why I asked if this was possible to do this from stdin) was because of the problem I noted RE: xargs calling tar multiple times if the input list is too long...does this solution also bypass that problem? (it seems like it does because it's also using "-T", not explicitly putting the source files as command-line arguments) – StoneThrow Jan 3 at 20:13
  • The one problem with this script that I see is if you run two of them in the same time, in this case you will need to use something to generate random names for temp.txt.... – Luciano Andress Martini Jan 4 at 10:34
3

You can use the -u/--update option in conjunction with xargs. I just tried and to prove that it works, I gave the filename arguments to tar one-by-one using xargs -n 1:

find top_of_tree/ -type f | xargs -n 1 tar -uf archive.tar

Of course, this is just a proof of concept. In your case, it is probably more practical to allow xargs to separate the argument list into larger parts that do not exceed the argument length limit, so as to avoid having a lot of calls to tar:

find top_of_tree/ -type f | xargs tar -uf archive.tar

The prerequisite is that all implementations of tar on your platforms support at least one of the -u/--update flags.

EDIT: the -u/--update flags allow tar to create as well as append to an archive.

I just noticed that unfortunately, you cannot update compressed archives (tgz, created with the -c flag). However, you can, by all means create a non-compressed archive first and then compress the results.

Also, it should not confuse you that I'm using flags to tar with a hyphen ('-' character). It accepts both forms. In my example, -uf would be equivalent to uf in yours.

As for the - option, it is probalby not documented in the man page of tar because it is a very common Unix convention for command line tools that deal with streams as well as named files. - is usually a special argument for filters and names the standard input stream as a file.

  • Good idea, thank you, but unfortunately, looks like Busybox version of tar does not support -u/-update. – StoneThrow Jan 3 at 20:15
  • I worked a lot on embedded machines with BusyBox as well. Unfortunately, my only solution was to cross-compile 7Zip for that system. – Larry Jan 3 at 20:23
  • -u wouldn't work with a gzipped archive anyway. – Gilles Jan 3 at 20:28

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.