linux - 如何根据条件查找文件

标签 linux bash filenames

我有这样的行:

elastislide.css
handheld1119.css
job-listing1119.css
jquery-ui-1.8.23.custom1119.css
print1119.css
royalslider.min1119.css
staticlayout1119.css
style-imagegallery.css
style.min1119.css

我想查找 .css 文件类型的最后 4 个字符。例如,我想找到 1119 并将其存储在一个变量中,最后 4 个字符每个月都会变化。

请帮忙。

最佳答案

如果没有任何上下文表明您希望如何使用此信息,则很难发布有用的代码,但以下循环会遍历匹配项。

find . -type f -name '*[0-9][0-9][0-9][0-9].css' -print |
while read -r file; do
    base=${file%.css}
    prefix=${base%[0-9][0-9][0-9][0-9]}
    printf "file: %s suffix: %s\n" "$file" "${base#$prefix}"
done

这演示了 ${var#prefix}${var%suffix} 变量替换。

如果文件名包含换行符,find | while 构造不安全。对于生产脚本,您应该使用更健壮的东西;但对于快速或专门的脚本来说,这应该足够了。

关于linux - 如何根据条件查找文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32042854/

相关文章:

python - 使用 argparse 传递参数后如何将 python 脚本作为批处理作业运行?

linux - 在 SLURM sbatch 脚本中使用 Bash 变量

bash - 带引号的 Shell 函数命令

path - 路径组件的命名标准是什么?

python - for 文本文件循环

php - 将 wordpress 站点从 Nginx 移动到专用服务器 Windows

c++ - 如何以编程方式阻止Linux操作系统用户空间中的USB存储设备

python - 使用 Python 提取文件名中包含无效字符的文件

linux - Rpm 安装错误,libstdc++-4.4.4-13.el6

c - 如何从 C 获取 Linux 中当前文件 (pwd) 的路径?