防止父目录被焦油化

发布于 2024-09-29 02:50:57 字数 286 浏览 2 评论 0原文

基本上我只想压缩目录中的所有文件,但不想获取存档中的所有父目录。

我已经尝试过-C,但我想我没有正确使用它。

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

这会导致 tar 尝试添加 CWD 中的所有文件。使用完整路径作为最后一个参数不会阻止添加目录。

看起来很简单,但无法弄清楚。不知何故, tar 不会将 ./* 压缩为相对于 -C 的目录,尽管它应该更改为该目录。

帮助表示赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如此安好 2024-10-06 02:50:57

包含父目录 (/var/some/log),因为执行 ./ 时包含 /var/some/log/path/.. *。尝试只进行

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

测试运行:

$ 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$ 

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$ 
魔法唧唧 2024-10-06 02:50:57

有一种更简单的方法可以做到这一点:

  1. cd 到您希望位于顶层的目录,即..

    cd /var/lib/mysql

  2. 从 tar 命令中删除父目录

    /var/lib/mysql/DATABASE_NAME 变为 DATABASE_NAME

更多详细信息可以在 此博客文章

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文