我在目录中有一个文件列表,我想制作一个 bash 脚本,该脚本使用正则表达式来检查其中的每个文件名是否具有以下语法:
xxxx_xxxx_xx_xx
Run Code Online (Sandbox Code Playgroud)
其中 x 是数字。
编辑:我只需要正则表达式
怎么样
#/usr/bin/env bash
for f in *
do
[[ $f =~ [0-9]{4}_[0-9]{4}_[0-9]{2}_[0-9]{2} ]] ||
echo "File $f does not match"
done
Run Code Online (Sandbox Code Playgroud)
正则表达式检查任何数字 ( [0-9])。花括号中的数字是重复次数,因此[0-9]{4}将匹配任何 4 位数字。
我建议您不要为此使用 bash,而是使用 find 。它可能会更快,而且肯定更便携(并非所有 shell 都可以处理正则表达式):
find -regextype posix-egrep -not -regex '\./[0-9]{4}_[0-9]{4}_[0-9]{2}_[0-9]{2}'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1824 次 |
| 最近记录: |