Bash:循环搜索与扩展名不匹配的文件 [英] Bash: loop through files that DO NOT match extension

查看:73
本文介绍了Bash:循环搜索与扩展名不匹配的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个bash脚本,该脚本需要循环目录中与特定扩展名不匹配的文件.到目前为止,我发现以下代码将循环所有与给定扩展名匹配的文件:

I'm writing a bash script that needs to loop files inside a directory that do not match a specific extension. So far, I've found that the following code loops all files that matches the given extension:

for f in *.txt ; do
    echo $f;
done

实例头如何循环遍历与指定扩展名不匹配的文件?

How could insthead loop through files that do not match the specified extension?

推荐答案

您可以使用==运算符进行模式匹配.

You can pattern-match with the == operator.

for f in *; do
    [[ $f == *.txt ]] && continue
    # [[ $f != *.txt ]] || continue
    ...
done

如果此命令可能在空目录中运行,请在循环之前使用shopt -s nullglob,或将[ -e "$f" ] || continue放在循环旁边. (前者是可取的,因为它避免了不断检查文件是否存在.)

If this might run in an empty directory, either use shopt -s nullglob prior to the loop, or put [ -e "$f" ] || continue in side the loop. (The former is preferable, as it avoids constantly checking if a file exists.)

这篇关于Bash:循环搜索与扩展名不匹配的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆