我需要使用此命令创建一个zip文件:
zip /dir/to/file/newZip /data/to/zip/data.txt
这有效,但是创建的zip文件创建了将目录模仿为原始文件的目录结构。我不需要很多额外的文件夹。
我在手册页或Google搜索中没有粗略地找到答案。
您可以使用-j
。
-j
--junk-paths
Store just the name of a saved file (junk the path), and do not
store directory names. By default, zip will store the full path
(relative to the current directory).
使用-j
选项:
-j Store just the name of a saved file (junk the path), and do not
store directory names. By default, zip will store the full path
(relative to the current path).
使用-j
不能与-r
选项一起使用。因此,解决方法可以是:
cd path/to/parent/dir/;
zip -r complete/path/to/name.zip ./* ;
cd -;
或嵌入式版本
cd path/to/parent/dir/ && zip -r complete/path/to/name.zip ./* && cd -
如果您不希望/dev/null
输出出现在屏幕上,则可以将输出定向到cd -
有点相关-我正在寻找一种解决方案,以对目录执行相同的操作。不幸的是-j
选项不适用于此:(
关于如何完成它,这是一个很好的解决方案:https://superuser.com/questions/119649/avoid-unwanted-path-in-zip-file
或者,您可以创建指向文件的临时符号链接:
ln -s /data/to/zip/data.txt data.txt
zip /dir/to/file/newZip !$
rm !$
这也适用于目录。
只需使用-jrm
选项即可删除文件和目录结构
zip -jrm /path/to/file.zip /path/to/file