tar: how to bundle the “extract to directory XXX”

2019-09-02 09:38发布

问题:

I know 'tar xf -C DIR' will untar the files in DIR, but is there an option to bundle the "-C DIR" information within the tarball when I create the tar file, so that when I use 'tar xf' the files will be automatically extracted into DIR?

Thanks.

回答1:

Create DIR, then symlink the files into DIR (no copying involved), then use GNU tar with the dereference (-h) option:

mkdir DIR
ln -s /path/to/f1 /path2/f2 DIR/
tar chf archive.tar DIR
rm -rf DIR

This will archive the actual files pointed by the symlinks (and not the symlinks you created temporarily)

Downside is you can't put actual symlinks in the archive, of course (they too will be dereferenced)



标签: shell tar