我需要使用该命令创建一个zip
zip /dir/to/file/newZip /data/to/zip/data.txt
Run Code Online (Sandbox Code Playgroud)
这样可行,但创建的zip文件会创建一个目录结构,模仿原始文件的目录.这是我不需要的许多额外文件夹.
我粗略地浏览了手册页或谷歌搜索,但没有找到答案.
Lar*_*off 333
你可以用-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).
Run Code Online (Sandbox Code Playgroud)
Dan*_* D. 32
使用-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).
Run Code Online (Sandbox Code Playgroud)
Vik*_*iya 29
使用-j
不会与-r
选项一起使用.
所以它的解决方法可以是:
cd path/to/parent/dir/;
zip -r complete/path/to/name.zip ./* ;
cd -;
Run Code Online (Sandbox Code Playgroud)
或者是在线版本
cd path/to/parent/dir/ && zip -r complete/path/to/name.zip ./* && cd -
Run Code Online (Sandbox Code Playgroud)
/dev/null
如果您不希望cd -
输出显示在屏幕上,可以将输出定向到
fla*_*aky 18
有点相关 - 我正在寻找一个解决方案,为目录做同样的事情.不幸的是-j选项对此不起作用:(
这是一个很好的解决方案,如何完成它:https: //superuser.com/questions/119649/avoid-unwanted-path-in-zip-file
unzip
文件到处乱放压缩目录时,将父目录保留在存档中将有助于避免在稍后解压缩存档文件时弄乱当前目录
\n因此,为了避免保留所有路径,并且由于您不能一起使用 -j 和 -r (您会收到错误),您可以这样做:
\ncd path/to/parent/dir/;\nzip -r ../my.zip "../$(basename "$PWD")"\ncd -;\n
Run Code Online (Sandbox Code Playgroud)\n这"../$(basename "$PWD")"
是保留父目录的魔力。
现在unzip my.zip
将给出一个包含所有文件的文件夹:
parent-directory\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file1\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file2\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dir1\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file3\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file4\n
Run Code Online (Sandbox Code Playgroud)\n不要将解压缩的文件散布在当前目录中:
\nfile1\nfile2\ndir1\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file3\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file4\n
Run Code Online (Sandbox Code Playgroud)\n
或者,您可以创建一个指向文件的临时符号链接:
ln -s /data/to/zip/data.txt data.txt
zip /dir/to/file/newZip !$
rm !$
Run Code Online (Sandbox Code Playgroud)
这也适用于目录。
归档时间: |
|
查看次数: |
130733 次 |
最近记录: |