Quantcast
Channel: Admins Goodies » compression
Viewing all articles
Browse latest Browse all 10

tar: How to create a tar file with arbitrary leading directories w/o ‘cd’ing to parent dir

$
0
0

Question

Say I have a directory of files at

/home/user1/dir1

and I want to create a tar with only “dir1″ as the leading directory:

/dir1/file1
/dir1/file2

I know I can first cd to the directory

cd /home/user1/
tar czvf dir1.tar.gz dir1

But when writing scripts, jumping from directory to directory isn’t always favorable. I am wondering is there a way to do it with absolute paths without changing current directories?

I know I can always create a tar file with absolute paths INSIDE and use

--strip-components

when extracting but sometimes extra path names are extra private information that you don’t want to distribute with your tar files.

Thanks!

Asked by Yan

Answer

tar -C changes directory

tar -C /home/user1/ -cvzf dir1.tar.gz dir1

btw, handy one for keeping track of changing directories… use pushd and popd.

pushd .
cd /home/user1
tar cvfz dir1.tar.gz
popd
Answered by Phil

Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images