Answers:
您可以使用find
获取文件列表并执行zip -j myfiles
打包操作而忽略路径:
find . -name "d[014]" -exec zip -j myfiles {} +
$ tree
.
├── d0
├── f0
├── f1
│ └── d1
└── f2
└── f3
├── d2
├── d3
└── d4
$ find . -name "d[014]" -exec zip -j myfiles {} +
adding: d1 (stored 0%)
adding: d4 (stored 0%)
adding: d0 (stored 0%)
$ unzip -l myfiles.zip
Archive: myfiles.zip
Length Date Time Name
--------- ---------- ----- ----
0 2017-10-09 10:47 d1
0 2017-10-09 10:48 d4
0 2017-10-09 10:47 d0
--------- -------
0 3 files
但是,这仅适用于文件,目录将被忽略zip -j
。为了使该功能也适用于目录,请说我们要打包d0
,d1
而f3
在上例中是整个目录,该find
行变得稍微复杂一点:
$ find . \( -name "d[01]" -o -name "f3" \) -exec sh -c 'p=$(pwd); for i in $0 $@; do cd ${i%/*}; zip -ur "$p"/myfiles ${i##*/}; cd "$p"; done' {} +
zip warning: /home/dessert/myfiles.zip not found or empty
adding: d1 (stored 0%)
adding: f3/ (stored 0%)
adding: f3/d3 (stored 0%)
adding: f3/d2 (stored 0%)
adding: f3/d4 (stored 0%)
adding: d0 (stored 0%)
$ unzip -l myfiles.zip
Archive: myfiles.zip
Length Date Time Name
--------- ---------- ----- ----
0 2017-10-11 10:18 d1
0 2017-10-11 10:19 f3/
0 2017-10-11 10:19 f3/d3
0 2017-10-11 10:19 f3/d2
0 2017-10-11 10:19 f3/d4
0 2017-10-11 10:17 d0
--------- -------
0 6 files
zip
文件中-j
。