基于文件名字符的终端多文件计数排除

Terminal multiple file count exclusion based on character in file names

我是一个相当新手的终端用户,我想知道如何根据名称中的特定字符制作脚本 select 文件,以便将它们从检查中排除许多文件都在一个文件夹中,必须显示为一个数字。有问题的字符是 º.

只需使用文件名扩展,也称为通配:

echo *[!w]*

将显示当前目录中不包含 w 的所有文件名的列表。

*表示"zero or more of any characters"

[! ] 包含要排除的单个字符列表

获取计数:

for fname in *[!w]*
do
    (( count++ ))
done

echo "$count files without a 'w'"

我选择了'w',因为它更容易查看和测试。还有许多其他方法可以做到这一点,包括 set、使用数组和 wc 程序。

无论文件名包含什么,这都有效:

count="$(find . -mindepth 1 -not -name '*º*' -exec printf x \; | wc -c)"

测试:

$ cd -- "$(mktemp -d)"
$ touch aº
$ touch b
$ find . -mindepth 1 -not -name '*º*' -exec printf x \; | wc -c
1