Prevent Parent Directories from Uploading

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

I tried -C, but I think I am not using it correctly.

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

This causes tar to try to add all the files to CWD. Using the full path as the last argument does not prevent the addition of dirs.

Seems simple enough, but can't figure it out. Somehow tar is not tar./* as relative -C, although it should change to this directory.

Help evaluate.

+6
shell tar
source share
2 answers

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

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

Testing:

 $ 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$ 
+11
source share

There is a much simpler way to do this:

  • cd to the directory you want to be at the top level, i.e.

    cd /var/lib/mysql

  • Remove parent directories from your tar command

    /var/lib/mysql/DATABASE_NAME becomes just DATABASE_NAME

More information can be found in this blog post .

0
source share

All Articles