[bash]用于查找指定格式目录的程序

功能:

在指定目录下查找符合yyyy-MM-dd(-d)nnn模式的目录名,例如2020-03-22-b888

目标目录情况:

复制代码
[root@localhost testfolder]# ll
total 0
drwxr-xr-x. 2 root root 6 Mar 21 06:53 2020
drwxr-xr-x. 2 root root 6 Mar 21 06:16 2020-01-10-b001
drwxr-xr-x. 2 root root 6 Mar 21 06:16 2020-02-20-b001
drwxr-xr-x. 2 root root 6 Mar 21 06:16 2020-03-20-b001
drwxr-xr-x. 2 root root 6 Mar 21 06:15 2020-03-21-b001
drwxr-xr-x. 2 root root 6 Mar 21 06:15 2020-03-22-b001
drwxr-xr-x. 2 root root 6 Mar 21 06:50 2020-20
drwxr-xr-x. 2 root root 6 Mar 21 06:16 archive
-rw-r--r--. 1 root root 0 Mar 21 06:18 a.txt
drwxr-xr-x. 2 root root 6 Mar 21 06:16 backup
-rw-r--r--. 1 root root 0 Mar 21 06:18 b.txt
[root@localhost testfolder]# 
复制代码

期待查找结果:

2020-02-20-b001  2020-03-21-b001  
2020-01-10-b001  2020-03-20-b001  2020-03-22-b001

代码:

复制代码
#!/bin/bash

function findDirsUnder(){
    folder=$1

    for dir in `find $folder -type d -regextype 'posix-egrep' -regex $folder+'/[0-9]{4}-[0-9]{2}-[0-9]{2}-b[0-9]{3}$'`;
    do
       echo $dir
    done
}

findDirsUnder "/root/testfolder"
复制代码

执行结果:

[root@localhost bashs]# sh dir.sh 
/root/testfolder/2020-03-21-b001
/root/testfolder/2020-03-22-b001
/root/testfolder/2020-03-20-b001
/root/testfolder/2020-02-20-b001
/root/testfolder/2020-01-10-b001
[root@localhost bashs]# 

--2020年3月22日--

 

posted @   逆火狂飙  阅读(144)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2016-03-22 【Canvas与艺术】绘制方形斜纹生化危险Biohazard标志
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示