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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:36:33  来源:igfitidea点击:

How to gunzip without overwriting non-interactively

bashgunzip

提问by herder

I want to unzip .gzfiles 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 -fforce overwriting, but I haven't found nothing about skipping it.

我想解压缩.gz文件但不覆盖。当生成的文件存在时,gunzip 将要求覆盖权限,但我希望 gunzip 默认不覆盖并中止。我读过一个-f强制覆盖的人,但我没有发现跳过它。

gunzip ${file} 

I need something like -nin copying cp -n ${file}

我需要像-n复制一样的东西cp -n ${file}

回答by dogbane

gunzipwill prompt you before overwriting a file. You can use the yescommand to automatically send an nstring to the gunzipprompt, 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 .gzextensions, you can check if the file exists before running gunzip:

授予您的文件.gz扩展名,您可以在运行之前检查文件是否存在gunzip

[[ -e ${file%.gz} ]] || gunzip "${file}"

[[ -e ${file%.gz}]] removes .gzand 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