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日--

  • 相关阅读:
    iOS多线程开发小demo5 线程间的通信
    iOS多线程开发小demo4,线程的同步问题
    iOS多线程开发小demo3,线程的状态
    iOS多线程开发小demo2,NSThread篇
    iOS多线程开发小demo
    iOS开发多线程基础知识
    sublime text 3 3083 注册码
    Canvas现实画板功能
    CSS3动画进度条
    移动端使用HTML5表单增强体验
  • 原文地址:https://www.cnblogs.com/heyang78/p/12546221.html
Copyright © 2011-2022 走看看