只需添加一些引号即可转义通配符:
unzip "*.zip"
下面的 shell 脚本将当前目录中的所有 zip 文件提取到具有 zip 文件文件名的新目录中,即:
以下文件:
myfile1.zip
myfile2.zip
将被提取到:
./myfile1/files...
./myfile2/files...
外壳脚本:
#!/bin/sh
for zip in *.zip
do
dirname=`echo $zip | sed 's/\.zip$//'`
if mkdir "$dirname"
then
if cd "$dirname"
then
unzip ../"$zip"
cd ..
# rm -f $zip # Uncomment to delete the original zip file
else
echo "Could not unpack $zip - cd failed"
fi
else
echo "Could not unpack $zip - mkdir failed"
fi
done
用法:
cd /dir/with/zips
wget -O - https://www.toptal.com/developers/hastebin/suvefuxuxo.bash | bash
"
解压缩 *.zip,或者如果它们在子文件夹中,则类似于
find . -name "*.zip" -exec unzip {} \;
gzip'ed
个文件,使用 gunzip -rfk .
在各个文件夹内递归解压缩
解压缩所有 .zip
文件并将内容存储在与 .zip
文件同名的新文件夹中:
find . -name '*.zip' -exec sh -c 'unzip -d "${1%.*}" "$1"' _ {} \;
这是 @phatmanace's answer 的扩展和地址 @RishabhAgrahari's comment:
这将提取当前目录中的所有 zip 文件,如果我希望将 zip 文件(存在于子文件夹中)提取到相应的子文件夹中怎么办?
gzip'ed
个文件,使用 gunzip -rfk .
在各个文件夹内递归解压缩
find: illegal option -- n
- 其他人?
find . -name '*.zip' -exec sh -c 'unzip -d "${1%.*}" "$1"' _ {} \;
您忘记了目录列表。
for i in *.zip; do
newdir="${i:0:-4}" && mkdir "$newdir"
unzip "$i" -d "$newdir"
done
这会将所有 zip 存档解压缩到以 zip 存档文件名命名的新文件夹中。
a.zip
b.zip
c.zip
将分别解压缩到 a
b
c
个文件夹中。
aunpack -e *.zip
,安装了 atool
。优点是它可以智能地处理错误,并且总是解压缩到子目录中,除非 zip 仅包含一个文件。因此,不会有大量文件污染当前目录的危险,就像没有目录结构的 zip 上的 unzip
一样。
aunpack -e -D *.zip
如果您希望每个 zip 获得自己的输出目录,而不管其中的文件数量如何(类似于 Windows 中 ExtractAll 的默认行为)
在任何 POSIX shell 中,这将解压缩到每个 zip 文件的不同目录中:
for file in *.zip
do
directory="${file%.zip}"
unzip "$file" -d "$directory"
done
for file in *.zip; do unzip "$file" -d "${file%.zip}"; done
如果“当前目录”是指 zip 文件所在的目录,那么我将使用以下命令:
find . -name '*.zip' -execdir unzip {} \;
摘自 find 的 man page
-execdir command ;
-execdir command {} +
与 -exec 类似,但指定的命令是从包含匹配文件的子目录运行的,通常不是您开始查找的目录。这是一种更安全的调用命令的方法,因为它在解析匹配文件的路径期间避免了竞争条件。与 -exec 选项一样,-execdir 的“+”形式将构建一个命令行来处理多个匹配的文件,但任何给定的命令调用都只会列出存在于同一子目录中的文件。如果你使用这个选项,你必须确保你的 $PATH 环境变量不引用当前目录;否则,攻击者可以通过在您将运行 -execdir 的目录中保留一个适当命名的文件来运行他们喜欢的任何命令。
用这个:
for file in `ls *.Zip`; do
unzip ${file} -d ${unzip_dir_loc}
done
for file in 'ls *.zip'; do unzip "${file}" -d "${file:0:-4}"; done
for file in *.zip; do ...
更安全吗?
这是一个不使用 ls
的单行程序,它使用文件的 zip 名称创建文件夹。它适用于当前目录中的任何 zip。
for z in *.zip; do unzip "$z" -d "${z%".zip"}"; done
您可以将其添加到您的 .bashrc
alias unzip_all='for z in *.zip; do unzip "$z" -d "${z%".zip"}"; done'
灵感来自:
Method #2: Unzipping Multiple Files from Linux Command Line Using Shell For Loop (Long Version)
在 https://www.cyberciti.biz/faq/linux-unix-shell-unzipping-many-zip-files/
如果文件是 gzip'd
。然后只需使用:
gunzip -rfk .
从根目录递归提取各个目录中的文件,保留原始文件(或删除 -k
以删除它们)
这是使用 How to loop through a directory recursively to delete files with certain extensions 教导的 Pedro Lobito answer 的变体:
shopt -s globstar
root_directory="."
for zip_file_name in **/*.{zip,sublime\-package}; do
directory_name=`echo $zip_file_name | sed 's/\.\(zip\|sublime\-package\)$//'`
printf "Unpacking zip file \`$root_directory/$zip_file_name\`...\n"
if [ -f "$root_directory/$zip_file_name" ]; then
mkdir -p "$root_directory/$directory_name"
unzip -o -q "$root_directory/$zip_file_name" -d "$directory_name"
# Some files have the executable flag and were not being deleted because of it.
# chmod -x "$root_directory/$zip_file_name"
# rm -f "$root_directory/$zip_file_name"
fi
done
利用
sudo apt-get install unzip
unzip file.zip -d path_to_destination_folder
在linux中解压文件夹
for i in `ls *.zip`; do unzip $i; done
Filename not matched
时,只需将模式用单引号括起来:unzip '*.zip'