bash 如何在不以非交互方式覆盖的情况下进行 gunzip
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24011976/
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
How to gunzip without overwriting non-interactively
提问by herder
I want to unzip .gz
files but without overwriting. When the resulting file exists, gunzip will ask for permission to overwrite, but I want gunzip not to overwrite by default and just abort. I read in a man that -f
force overwriting, but I haven't found nothing about skipping it.
我想解压缩.gz
文件但不覆盖。当生成的文件存在时,gunzip 将要求覆盖权限,但我希望 gunzip 默认不覆盖并中止。我读过一个-f
强制覆盖的人,但我没有发现跳过它。
gunzip ${file}
I need something like -n
in copying cp -n ${file}
我需要像-n
复制一样的东西cp -n ${file}
回答by dogbane
gunzip
will prompt you before overwriting a file. You can use the yes
command to automatically send an n
string to the gunzip
prompt, as shown below:
gunzip
将在覆盖文件之前提示您。可以使用该yes
命令自动向提示发送n
字符串gunzip
,如下图:
$ yes n | gunzip file*.gz
gunzip: file already exists; not overwritten
gunzip: file2 already exists; not overwritten
回答by konsolebox
Granting your files have .gz
extensions, you can check if the file exists before running gunzip
:
授予您的文件.gz
扩展名,您可以在运行之前检查文件是否存在gunzip
:
[[ -e ${file%.gz} ]] || gunzip "${file}"
[[ -e ${file%.gz}
]] removes .gz
and checks if a file having its name exists. If not (false), ||
would run gunzip "${file}"
.
[[ -e ${file%.gz}
]] 删除.gz
并检查具有其名称的文件是否存在。如果不是 (false),||
将运行gunzip "${file}"
.
回答by crizCraig
Here's a combination of the answers here and thisthat will unzip a group of gzipped files to a different destination directory:
这里是这里的答案的组合和这将解压缩一组gzip压缩的文件到不同的目的地目录:
dest="unzipped"
for f in *.gz; do
STEM=$(basename "${f}" .gz)
unzipped_name="$dest/$STEM"
echo ''
echo gunzipping $unzipped_name
if [[ -e $unzipped_name ]]; then
echo file exists
else
gunzip -c "${f}" > $unzipped_name
echo done
fi
done