简体   繁体   English

Zcat for循环中的多个压缩文件

[英]Zcat multiple compressed files in a for loop

Im fairly new to bash scripting and i've been searching here in stackoverflow for an answer that will match what im looking for but cant find an exact match.. apologies if this has already been answered. 我是bash脚本的新手,我一直在这里在stackoverflow中寻找一个答案,该答案将与我在寻找的内容相匹配,但找不到确切的匹配结果。

I have multiple folders that has multiple compressed files, some are gzip extension , some doesnt have extension. 我有多个包含多个压缩文件的文件夹,有些是gzip扩展名,有些没有扩展名。

The only way Ive been able to see the content of the compressed file is by doing 我能够查看压缩文件内容的唯一方法是

zcat filename.gzip > filename

My goal is to create a for loop that will : 我的目标是创建一个for循环,该循环将:

  1. zcat all files and output it to a new file & add an identifier at the end, something like "-read" zcat所有文件并将其输出到新文件并在末尾添加标识符,例如“ -read”
  2. delete the compressed file 删除压缩文件

Thank you! 谢谢!

To decompress all files that gzip would handle using find , basename and a while loop: 要使用findbasenamewhile循环解压缩gzip将处理的所有文件:

find . -type f | while read f; do
    set -e # You should probably always script with set -e
           # This will cause the subshell to stop on errors.
    zcat < "$f" > "$f-new" || continue
    rm "$f"
done

This should also handle any weird quoting that comes up. 这也应该处理出现的任何奇怪的报价。 The while loop will run in a subshell, and set -e will set the stop on error option in it so that if there's an error with either the zcat or the rm it won't keep plowing ahead destroying everything in sight. while循环将在子外壳中运行,并且set -e将在其中设置错误停止选项,这样,如果zcatrm出现错误,它就不会继续努力破坏所有可见的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM