[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 @ 2020-03-22 15:29  逆火狂飙  阅读(151)  评论(0)    收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东