TAR 可以记录符号链接及其引用的文件/目录吗?

发布于 2024-12-02 03:34:02 字数 88 浏览 0 评论 0原文

Tar 命令有一个 -h 标志来跟踪符号链接,而不是记录符号链接。是否可以同时拥有符号链接和它们在存档中所指向的内容?这可以在一次运行 TAR 命令时自动完成吗?

The Tar command has an -h flag to follow symbolic links instead of recording the symbolic links. Is it possible to have both the symbolic links and what they point to in the archive? Can this be done automatically in one run of the TAR command?

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

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

发布评论

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

评论(2

瀟灑尐姊 2024-12-09 03:34:02

确保符号链接及其指向的目录/文件都位于 tar 内容内。

例如,假设您有一个文件 work/mydir/myfile.txt 和一个指向该文件的符号链接 work/mylink(带有相对路径):

work:

drwxrwxr-x. 2 ...... ...... 4096 May 16 11:54 mydir
lrwxrwxrwx. 1 ...... ......   16 May 16 11:54 mylink -> mydir/myfile.txt

work/mydir:

-rw-rw-r--. 1 ...... ......    0 May 16 11:54 myfile.txt

当您存档时在 work 目录中,符号链接和文件都放在 tar 存档中:

$ tar cvf work.tar work/
work/
work/mylink
work/mydir/
work/mydir/myfile.txt

解压 tar 存档时,将在目标目录中创建文件和符号链接。

Make sure both the symbolic link and the directory/file it points to are inside the tar content.

For example, assume you have a file work/mydir/myfile.txt and a symbolic link work/mylink which points to that file (with relative path):

work:

drwxrwxr-x. 2 ...... ...... 4096 May 16 11:54 mydir
lrwxrwxrwx. 1 ...... ......   16 May 16 11:54 mylink -> mydir/myfile.txt

work/mydir:

-rw-rw-r--. 1 ...... ......    0 May 16 11:54 myfile.txt

When you archive the work directory, both the symbolic link and the file are put inside the tar archive:

$ tar cvf work.tar work/
work/
work/mylink
work/mydir/
work/mydir/myfile.txt

When that tar archive will be extracted, the file and the symbolic link will be created in the target directory.

豆芽 2024-12-09 03:34:02

这是一个很老的问题,但最近我在使用 tar 命令时遇到了同样的问题。
我需要保留 clickhouse 配置和元数据目录,它看起来像一个具有混合内容的目录:有些是带有元数据的目录,有些是目录的符号链接,所以我也应该保留原始内容和符号链接。这个命令对我有用。

find /etc/clickhouse-server /opt/clickhouse/metadata \( -type l -print -exec realpath \{\} \; \) -or \( ! -type l -print \) | tar cf backup-clickhouse-config.tgz --gzip -P --files-from=-

Quite old question, but recently I had the same issue with tar command.
I need to preserve clickhouse config and metadata dir, which looks like a directory with mixed content: some are dirs with metadata, some are symlinks to dirs, so I should preserve original content and symlinks as well. This command worked for me.

find /etc/clickhouse-server /opt/clickhouse/metadata \( -type l -print -exec realpath \{\} \; \) -or \( ! -type l -print \) | tar cf backup-clickhouse-config.tgz --gzip -P --files-from=-
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文