问题重现:

使用tar命令压缩目录时,会报“tar: Removing leading `/' from member names”错误(去除文件名中前导的根目录“/”),但是不会影响后面的压缩文件的产生

命令:

tar -g /tmp/snapshot -czvf system_backup.tar.gz /tmp

[root@logstash tmp]# tar -g /tmp/snapshot -czvf system_backup.tar.gz /tmp
tar: /tmp: Directory is new
tar: /tmp/.ICE-unix: Directory is new
tar: /tmp/.Test-unix: Directory is new
tar: /tmp/.X11-unix: Directory is new
tar: /tmp/.XIM-unix: Directory is new
tar: /tmp/.font-unix: Directory is new
tar: Removing leading `/' from member names
/tmp/
/tmp/.ICE-unix/
/tmp/.Test-unix/
/tmp/.X11-unix/
/tmp/.XIM-unix/
/tmp/.font-unix/
/tmp/snapshot
[root@logstash tmp]# ls
snapshot  system_backup.tar.gz
[root@logstash tmp]# 
[root@logstash tmp]# tar -tf system_backup.tar.gz 
tmp/
tmp/.ICE-unix/
tmp/.Test-unix/
tmp/.X11-unix/
tmp/.XIM-unix/
tmp/.font-unix/
tmp/snapshot
[root@logstash tmp]#

原因分析:

tar 命令在压缩文件时,默认会取相对路径,不会取从根路径下来的绝对路径,所以如果待压缩

的源路径是绝对路径,便会报该错误

解决方案:

tar命令中使用-P参数即可规避该问题。

-P, --absolute-names       don't strip leading `/'s from file names;绝对路径,不从文件名中剥离“/

命令:

tar -g /tmp/snapshot -czvPf system_backup.tar.gz /tmp

#-P不能放到最后,那是因为-f表示filename,后面必须紧接要创建的文件的名称

[root@logstash tmp]# tar -g /tmp/snapshot -czvPf system_backup.tar.gz /tmp
tar: /tmp: Directory is new
tar: /tmp/.ICE-unix: Directory is new
tar: /tmp/.Test-unix: Directory is new
tar: /tmp/.X11-unix: Directory is new
tar: /tmp/.XIM-unix: Directory is new
tar: /tmp/.font-unix: Directory is new
/tmp/
/tmp/.ICE-unix/
/tmp/.Test-unix/
/tmp/.X11-unix/
/tmp/.XIM-unix/
/tmp/.font-unix/
/tmp/snapshot
[root@logstash tmp]# ls
snapshot  system_backup.tar.gz
[root@logstash tmp]# tar -tf system_backup.tar.gz 
tar: Removing leading `/' from member names
/tmp/
/tmp/.ICE-unix/
/tmp/.Test-unix/
/tmp/.X11-unix/
/tmp/.XIM-unix/
/tmp/.font-unix/
/tmp/snapshot
[root@logstash tmp]#