我有一个ZIP文件目录(在Windows机器上创建的)。我可以手动解压缩他们使用unzip文件名,但我怎么能解压缩所有的ZIP文件在当前文件夹通过shell?
使用Ubuntu Linux服务器。
我有一个ZIP文件目录(在Windows机器上创建的)。我可以手动解压缩他们使用unzip文件名,但我怎么能解压缩所有的ZIP文件在当前文件夹通过shell?
使用Ubuntu Linux服务器。
当前回答
解压缩*.zip,或者如果它们在子文件夹中,那么类似于
find . -name "*.zip" -exec unzip {} \;
其他回答
下面的shell脚本将当前目录中的所有zip文件提取到新的dirs中,并以zip文件的文件名命名,即:
以下文件:
myfile1.zip
myfile2.zip
将被提取到:
./myfile1/files...
./myfile2/files...
Shell脚本:
#!/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
for i in `ls *.zip`; do unzip $i; done
这是Pedro Lobito回答的一个变体,使用如何递归地遍历目录以删除具有某些扩展名的文件:
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
Use
sudo apt-get install unzip
unzip file.zip -d path_to_destination_folder
在Linux中解压缩一个文件夹
如果你所说的“当前目录”是指zip文件所在的目录,那么我将使用以下命令:
find . -name '*.zip' -execdir unzip {} \;
节选自find的手册页
-execdir command ;
-execdir command {} +
Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find. This a much more secure method for invoking commands, as it avoids race conditions during resolution of the paths to the matched files. As with the -exec option, the '+' form of -execdir will build a command line to process more than one matched file, but any given invocation of command will only list files that exist in the same subdirectory. If you use this option, you must ensure that your $PATH environment variable does not reference the current directory; otherwise, an attacker can run any commands they like by leaving an appropriately-named file in a directory in which you will run -execdir.