为什么 tar 创建链接

为什么 tar 创建链接
root@linux:/mnt/TEST# ls
test1.txt  test2.txt  test3.txt
root@linux:/mnt/TEST# find /mnt/TEST/ -mmin -10 -exec  tar -czvf /tmp/test.tar.gz {} +
tar: Removing leading `/' from member names
/mnt/TEST/
/mnt/TEST/test3.txt
/mnt/TEST/test2.txt
/mnt/TEST/test1.txt
tar: Removing leading `/' from hard link targets
/mnt/TEST/test3.txt
/mnt/TEST/test2.txt
/mnt/TEST/test1.txt
root@linux:/mnt/TEST# tar -tvf /tmp/test.tar.gz 
drwxr-xr-x root/root         0 2017-10-08 00:15 mnt/TEST/
-rw-r--r-- root/root         0 2017-10-08 00:15 mnt/TEST/test3.txt
-rw-r--r-- root/root         0 2017-10-08 00:15 mnt/TEST/test2.txt
-rw-r--r-- root/root         0 2017-10-08 00:15 mnt/TEST/test1.txt
hrw-r--r-- root/root         0 2017-10-08 00:15 mnt/TEST/test3.txt link to mnt/TEST/test3.txt
hrw-r--r-- root/root         0 2017-10-08 00:15 mnt/TEST/test2.txt link to mnt/TEST/test2.txt
hrw-r--r-- root/root         0 2017-10-08 00:15 mnt/TEST/test1.txt link to mnt/TEST/test1.txt

答案1

您将这些文件包含两次,GNUtar会注意到并假设第二组文件必须是硬链接,因为它们与第一组文件具有相同的 inode。

您将它们存档两次:

  1. 首先通过归档/mnt/TEST目录(满足find标准),
  2. 然后再次归档各个文件。

您应该修改find命令以仅查找常规文件:

find /mnt/TEST/ -type f -mmin -10 -exec  tar -czvf /tmp/test.tar.gz {} +

另请注意,如果该find命令找到的文件数量多于单次调用可处理的文件数量tar,它将调用tar多次,每次都会覆盖 tar 存档。

解决这个问题:

find /mnt/TEST/ -type f -mmin -10 \
    -exec sh -c 'a="/tmp/test.tar.gz"; if [ -f "$a" ]; then tar -uzvf "$a" "$@"; else tar -czvf "$a" "$@"; fi' sh {} +

这会附加如果存档存在( ),则将文件添加到存档中tar -u,否则将创建存档(tar -c)。

相关内容