通常,我会取消存档的工作,$ mkdir newFolder; $ mv *.zip newFolder; $ cd newFolder; $unzip *.zip
但是有时我会变得很懒,只是在任意文件夹中执行操作,$ unzip *.zip
因此有时会弄乱其他内容。我将在这里列出一些方法-一些存档版本肯定带有糟糕的标志,而其他的则更斯巴达式的,我对后者更感兴趣。
取消存档的某些方法,还有其他方法吗?
$ find . -anewer fileThatExistedBeforeUnarchieving -ok rm '{}' \;
缺点是它列出了*.zip
目录,因此您需要对-ok
多个*.zip
匹配使用slow ,slow,并且由于某种原因,它似乎与提取的所有内容都不匹配。
如果提取的文件数量很少,则是一对一,缓慢,麻烦且容易出错。
当我想确定$ unzip -l *.bsd
档案的内容是否实际上是一个文件夹时,有时我会用进行检查,至少在obsd的unzip-version中可以使用。
如果您指的是某些存档工具,请在适当的时候加以说明。不过请保持简单-我对方法的实现方式更感兴趣,而不是单一的工具。
您可以生成存档中的文件列表并删除它们,尽管对于诸如unzip或7z之类的存档器而言,这很麻烦,因为它们没有生成普通文件名列表的选项。即使使用tar,也假定文件名中没有换行符。
tar tf foo.tar | while read -r file; do rm -- "$file" done
unzip -l foo.zip | awk '
p && /^ --/ {p=2}
p==1 {print substr($0, 29)}
/^ --/ {++p}
' | while …
unzip -l foo.zip | tail -n +4 | head -n -2 | while … # GNU coreutils only
7z l -slt foo.zip | sed -n 's/^Path = //p' | while … # works on tar.*, zip, 7z and more
除了删除文件,您可以将它们移动到其预期的目的地。
tar tf foo.tar | while read -r file; do
if [ -d "$file" ]; then continue; fi
mkdir -p "/intended/destination/${file%/*}"
mv -- "$file" "/intended/destination/$file"
done
不必依赖外部工具,而可以(在大多数unice上)使用FUSE通过普通的文件系统命令来操作归档。
您可以使用Fuse-zip窥视一个zip,使用提取它cp
,使用列出其内容find
,等等。
mkdir /tmp/foo.d
fuse-zip foo.zip /tmp/foo.d
## Remove the files that were extracted mistakenly (GNU/BSD find)
(cd /tmp/foo.d && find . \! -type d -print0) | xargs -0 rm
## Remove the files that were extracted mistakenly (zsh)
rm /tmp/foo.d/**(:"s~/tmp/foo.d/~~"^/)
## Extract the contents where you really want them
cp -Rp /tmp/foo.d /intended/destination
fusermount -u foo.d
rmdir foo.d
AVFS会创建整个目录层次结构的视图,其中所有档案都具有一个关联的目录(名称末尾带有#的相同名称),该目录似乎包含档案内容。
mountavfs
## Remove the files that were extracted mistakenly (GNU/BSD find)
(cd ~/.avfs/"$PWD/foo.zip#" && find . \! -type d -print0) | xargs -0 rm
## Remove the files that were extracted mistakenly (zsh)
rm ~/.avfs/$PWD/foo.zip\#/**/*(:"s~$HOME/.avfs/$PWD/foo.zip#~~"^/)
## Extract the contents where you really want them
cp -Rp ~/.avfs/"$PWD/foo.zip#" /intended/destination
umountavfs
假设除了提取之外,没有其他活动在同一层次结构中,您可以通过提取的文件最近的ctime来告诉它们。如果您刚刚创建或移动了zip文件,则可以将其用作截止文件;否则,用于ls -lctr
确定合适的截止时间。如果您要确保不删除这些zip,则无需进行任何手动批准:find
完全可以排除它们。这是使用zsh或find
;的示例命令。请注意,-cmin
和主-cnewer
变量不在POSIX中,而是存在于Linux(和其他具有GNU find的系统),* BSD和OSX上。
find . \! -name '*.zip' -type f -cmin -5 -exec rm {} + # extracted <5 min ago
rm **/*~*.zip(.cm-6) # zsh, extracted ≤5 min ago
find . -type f -cnewer foo.zip -exec rm {} + # created or moved after foo.zip
使用GNU find,FreeBSD和OSX,另一种指定截止时间的方法是创建一个文件并将touch
其mtime设置为截止时间。
touch -d … cutoff
find . -type f -newercm cutoff -delete
除了删除文件,您可以将它们移动到其预期的目的地。这是使用GNU / * BSD / OSX查找的一种方法,可根据需要在目标位置创建目录。
find . \! -name . -cmin -5 -type f -exec sh -c '
for x; do
mkdir -p "$0/${x%/*}"
mv "$x" "$0/$x"
done
' /intended/destination {} +
等价于Zsh(几乎:这是重现整个目录层次结构,而不仅仅是将包含文件的目录):
autoload zmv
mkdir -p ./**/*(/cm-3:s"|.|/intended/destination|")
zmv -Q '(**/)(*)(.cm-3)' /intended/destination/'$1$2'
警告,我尚未测试此答案中的大多数命令。在删除文件之前,请务必先检查文件列表(echo
先运行,然后运行rm
)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句