解压未知名称文件的 Bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18989930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash script of unzipping unknown name files
提问by forensick
I have a folder that after an rsync will have a zip in it. I want to unzip it to its own folder(if the zip is L155.zip, to unzip its content to L155 folder). The problem is that I dont know it's name beforehand(although i know it will be "letter-number-number-number"), so I have to unzip an uknown file to its unknown folder and this to be done automatically.
我有一个文件夹,在 rsync 之后里面会有一个 zip 文件。我想将它解压缩到它自己的文件夹(如果 zip 是 L155.zip,将其内容解压缩到 L155 文件夹)。问题是我事先不知道它的名字(虽然我知道它会是“字母-数字-数字-数字”),所以我必须将一个未知的文件解压缩到它的未知文件夹,这要自动完成。
The command “unzip *”(or unzip *.zip) works in terminal, but not in a script. These are the commands that have worked through terminal one by one, but dont work in a script.
命令“unzip *”(或unzip *.zip)适用于终端,但不适用于脚本。这些是通过终端一一运行的命令,但在脚本中不起作用。
#!/bin/bash
unzip * #also tried .zip and /path/to/file/* when script is on different folder
i=$(ls | head -1)
y=${i:0:4}
mkdir $y
unzip * -d $y
First I unzip the file, then I read the name of the first extracted file through ls and save it in a variable.I take the first 4 chars and make a directory with it and then again unzip the files to that specific folder.
首先我解压缩文件,然后我通过 ls 读取第一个提取文件的名称并将其保存在一个变量中。我取前 4 个字符并用它创建一个目录,然后再次将文件解压缩到该特定文件夹。
The whole procedure after first unzip is done, is because the files inside .zip, all start with a name that the zip already has, so if L155.ZIP is the zip, the files inside with be L155***.txt.
第一次解压后的整个过程,是因为.zip里面的文件,都是以zip已有的名字开头的,所以如果L155.ZIP是zip,里面的文件就是L155* **.txt。
The zip file is at /path/to/file/NAME.zip
.
When I run the script I get errors like the following:
zip 文件位于/path/to/file/NAME.zip
. 当我运行脚本时,我收到如下错误:
unzip: cannot find or open /path/to/file/*.ZIP
unzip: cannot find or open /path/to/file//*.ZIP.zip
unzip: cannot find or open /path/to/file//*.ZIP.ZIP. No zipfiles found.
mkdir: cannot create directory 'data': File exists data
unzip: cannot find or open data, data.zip or data.ZIP.
采纳答案by dangenet
I would try the following.
我会尝试以下。
for i in *.[Zz][Ii][Pp]; do
DIRECTORY=$(basename "$i" .zip)
DIRECTORY=$(basename "$DIRECTORY" .ZIP)
unzip "$i" -d "$DIRECTORY"
done
As noted, the basename program removes the indicated suffix .zip
from the filename provided.
如前所述,basename 程序会.zip
从提供的文件名中删除指定的后缀。
I have edited it to be case-insensitive. Both .zip
and .ZIP
will be recognized.
我已将其编辑为不区分大小写。双方.zip
并.ZIP
会被认可。
回答by Kyle Kanos
Original answer
原答案
Supposing that foo.zip
contains a folder foo
, you could simply run
假设foo.zip
包含一个文件夹foo
,您可以简单地运行
#!/bin/bash
unzip \*.zip \*
And then run it as bash auto-unzip.sh
.
然后将其作为bash auto-unzip.sh
.
If you want to have these files extracted into a different folder, then I would modify the above as
如果您想将这些文件提取到不同的文件夹中,那么我会将上面的内容修改为
#!/bin/bash
cp *.zip /home/user
cd /home/user
unzip \*.zip \*
rm *.zip
This, of course, you would run from the folder where all the zip files are stored.
当然,您可以从存储所有 zip 文件的文件夹运行。
Another answer
另一个答案
Another "simple" fix is to get dtrx(also available in the Ubuntu repos, possibly for other distros). This will extract each of your *.zip
files into its own folder. So if you want the data in a different folder, I'd follow the second example and change it thusly:
另一个“简单”修复是获取dtrx(也可在 Ubuntu 存储库中获得,可能适用于其他发行版)。这会将您的每个*.zip
文件提取到其自己的文件夹中。因此,如果您希望将数据放在不同的文件夹中,我会按照第二个示例进行更改:
#!/bin/bash
cp *.zip /home/user
cd /home/user
dtrx *.zip
rm *.zip
回答by rici
If the folder has only file file with the extension .zip
, you can extract the name without an extension with the basename
tool:
如果文件夹只有带有扩展名的文件文件.zip
,您可以使用该basename
工具提取没有扩展名的名称:
BASE=$(basename *.zip .zip)
This will produce an error message if there is more than one file matching *.zip
.
如果有多个文件匹配,这将产生一条错误消息*.zip
。
Just to be clear about the issue here, the assumption is that the zip file does not contain a folder structure. If it did, there would be no problem; you could simply extract it into the subfolders with unzip
. The following is only needed if your zipfile contains loose files, and you want to extract them into a subfolder.
为了清楚说明这里的问题,假设 zip 文件不包含文件夹结构。如果有,就没有问题;你可以简单地将它提取到子文件夹中unzip
。仅当您的 zipfile 包含松散文件并且您想将它们解压缩到子文件夹中时才需要以下内容。
With that caveat, the following should work:
有了这个警告,以下应该工作:
#!/bin/bash
DIR=${1:-.}
BASE=$(basename "$DIR/"*.zip .zip 2>/dev/null) ||
{ echo More than one zipfile >> /dev/stderr; exit 1; }
if [[ $BASE = "*" ]]; then
echo No zipfile found >> /dev/stderr
exit 1
fi
mkdir -p "$DIR/$BASE" ||
{ echo Could not create $DIR/$BASE >> /dev/stderr; exit 1; }
unzip "$DIR/$BASE.zip" -d "$DIR/$BASE"
Put it in a file (anywhere), call it something like unzipper.sh
, and chmod a+x
it. Then you can call it like this:
把它放在一个文件中(任何地方),称之为unzipper.sh
, 和chmod a+x
它。然后你可以这样称呼它:
/path/to/unzipper.sh /path/to/data_directory
回答by iamauser
for zfile in $(find . -maxdepth 1 -type f -name "*.zip")
do
fn=$(echo ${zfile:2:4}) # this will give you the filename without .zip extension
mkdir -p "$fn"
unzip "$zfile" -d "$fn"
done
回答by user3019558
simple one liner I use all the time
我一直使用的简单的一个衬垫
$ for file in `ls *.zip`; do unzip $file -d `echo $file | cut -d . -f 1`; done