开发者

Prevent parent directories from being tarred

开发者 https://www.devze.com 2023-01-21 23:46 出处:网络
Basically I just want to tar all the files in a directory, but not get all the parent directories in the archive.

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.ta开发者_运维技巧r.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.


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$ 


There's a much easier way to do this:

  1. cd down to the directory you wish to be top level, i.e...

    cd /var/lib/mysql

  2. Remove parent directories from your tar command

    /var/lib/mysql/DATABASE_NAME becomes just DATABASE_NAME

More details can be found in this blog writeup.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号