[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日--
分类:
Linux.bash
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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标志