简体   繁体   English

在 bash 中循环几个文件扩展名

[英]looping over a few file extensions in bash

I have a list of files in a folder and I want to just work on a few of them.我在一个文件夹中有一个文件列表,我只想处理其中的几个。 The folder contains files with file.qc, file.qc.gz file.qc.stat file.qc.count and so on.该文件夹包含有file.qc、file.qc.gz、file.qc.stat、file.qc.count等文件。

I want to write a loop in bash that will open only the file.qc and file.qc.gz, while ignoring other file extensions (such as qc.stats or qc.count)我想在 bash 中编写一个循环,它只会打开 file.qc 和 file.qc.gz,而忽略其他文件扩展名(例如 qc.stats 或 qc.count)

Just specify multiple globs in your loop:只需在循环中指定多个 glob:

#!/bin/bash

# Gracefully cases where there are no matches
shopt -s nullglob

for f in *.qc *.qc.gz
do
  echo "Found: $f"
done

You can also write this shorter as *.qc{,.gz} , which expands to the same thing.你也可以把它写成*.qc{,.gz} ,它扩展成同样的东西。

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

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