zoukankan      html  css  js  c++  java
  • [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日--

  • 相关阅读:
    Ubuntu 16.04 swoole扩展安装注意!!!
    go mod使用指南
    基于gin框架-验证码demo
    go(基于gin开发提升效率)--Air
    go mod路径引入并代码提示
    golang在win10下安装问题(一)
    win10下beego安装注意坑(一)
    API统一管理平台-YApi
    vim编辑
    swool安装(centos7)
  • 原文地址:https://www.cnblogs.com/heyang78/p/12546221.html
Copyright © 2011-2022 走看看