n-alexander
n-alexander

Reputation: 15363

piping files from tar to a command

I need to untar a multiple-file archive. And I have to use a binary to do the actual writing.

With a single file archive, I'm happy with

tar -Oxvf singlefile.tgz | writer filename

for multiple files, tar has a --to-command option that pipes each file to a specified command. Sounds good but what about the file names?

>tar xvf test.tgz --to-command='writer' 2> /dev/null
tets1
test2
test3

tar seems to output the filenames to its stdout, and I couldn't find a way to get the filenames within the command.

Any ideas?

Upvotes: 4

Views: 3718

Answers (2)

James_pic
James_pic

Reputation: 3307

If you use the --to-command flag, various pieces of metadata about the process are made available in the environment. Running tar -xf file.tar --to-command=env gives:

TAR_VERSION=1.28
TAR_ARCHIVE=file.tar
TAR_VOLUME=1
TAR_BLOCKING_FACTOR=20
TAR_FORMAT=gnu
TAR_FILETYPE=f
TAR_MODE=0644
TAR_FILENAME=file1.txt
TAR_REALNAME=file1.txt
TAR_UNAME=james_pic
TAR_GNAME=users
TAR_ATIME=1501586107.32746318
TAR_MTIME=1492550943
TAR_CTIME=1501586107.32746318
TAR_SIZE=12345
TAR_UID=1001
TAR_GID=1000

For your purposes, you'll probably want TAR_FILENAME

Upvotes: 4

Did you read the tar man page and the GNU tar documentation ?

You might also be interested by tardy

Upvotes: 0

Related Questions