Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent parent directories from being tarred

Tags:

shell

tar

Basically I just want to tar all the files in a directory, but not get all the parent directories in the archive.

I've tried -C, but I guess I'm not using it right.

tar -cjf archive.tar.bz2 -C /var/some/log/path ./*

This results in tar trying to add all the files in the CWD. Using the full path as last argument doesn't prevent the dirs from being added.

Seems simple enough, but can't figure it out. Somehow tar does not tar ./* as being relative to -C, although it should change to that dir.

Help appreciated.

like image 246
Joe Avatar asked Oct 27 '10 08:10

Joe


1 Answers

The parent directory (/var/some/log) is included, since /var/some/log/path/.. is included when you do ./*. Try just doing

tar -cjf archive.tar.bz2 -C /var/some/log/path .

Test run:

$ find tmp/some_files
tmp/some_files
tmp/some_files/dir1
tmp/some_files/dir1/dir1file
tmp/some_files/hello
tmp/some_files/world
tmp/some_files/dir2
tmp/some_files/dir2/dir2file
$ tar -cvjf archive.tar.bz2 -C tmp/some_files/ .
./
./dir1/
./dir1/dir1file
./hello
./world
./dir2/
./dir2/dir2file
$ cd tmp/unpacked
/tmp/unpacked$ mv /home/aioobe/archive.tar.bz2 .
/tmp/unpacked$ tar -xvjf archive.tar.bz2 
./
./dir1/
./dir1/dir1file
./hello
./world
./dir2/
./dir2/dir2file
/tmp/unpacked$ ls
archive.tar.bz2  dir1  dir2  hello  world
/tmp/unpacked$ 
like image 66
aioobe Avatar answered Sep 28 '22 18:09

aioobe