【问题标题】:navigating directories and sub-directories and moving the files using shell导航目录和子目录并使用 shell 移动文件
【发布时间】:2016-12-08 23:32:59
【问题描述】:

目录结构

MyDirectory 

  -data/

    -DATA-TT_20160714_soe_test_testbill_52940_1.lst

  -output/

    -DATA-TT_20160714_soe_test_testbill_52940_1.pdf

  -Backup/  

enter code here 
   #!/bin/bash
    for i in $( ls ); do
        echo $i
              #cd $i
              #cd $i/data/
               echo $i
            cd  $i/data/
               echo  $i/data/
            $(ls *1.lst)
      cd ../output
    $(ls *1.pdf)
      done
  1. 我需要在目录和子目录中导航 这 保留输入和输出文件。这些文件有 YYYYMMDD 格式的日期,我需要与当前日期进行比较。 如果差异大于 1 个月,我需要 压缩这些文件并将它们移动到备份目录。 “DATA-TT”部分 是恒定的。
    谁能帮助我。可能有许多目录具有相同的子目录结构。例如 MyDirectory1,MyDirectory2,MyDirectory3

【问题讨论】:

    标签: shell unix backup


    【解决方案1】:

    必须离开去开会。我会把我一直在编写的脚本留给你来帮助你:

        #!/bin/sh
    
        # in your case filename would be the variable used in the for loop
        filename=$(find DATA-TT*)
        # Part gets the date from the filename
        part=$(echo $filename | grep -Eo '[[:digit:]]{8}')
        echo "$filename -> $part"
        # limit_date is current date -30 days
        limit_date=$(date +'%Y%m%d' -d "now 30 days")
        echo $cur_date
        # this part compares and echoes, you can try it and replace echoes with ziping, moving, etc.
        if [ $part -ge $limit_date ]
        then
                echo "part is older than 1 month"
        else
                echo "part had not yet reached 1 month age"
        fi
    

    【讨论】:

      【解决方案2】:

      这个对于数据目录和输出一样

      #!/bin/bash
      list=($(find data -name "DATA-TT_*"))  
      limit=`date +%Y%m%d -d "-30 days"`
      for i in ${list[@]}; do
         filedate=$(echo $i| cut -d'_' -f 2)
         if [ "$limit" -gt "$filedate" ]; then
            mv $i backup
         fi
      done
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-18
        • 1970-01-01
        • 1970-01-01
        • 2023-04-07
        • 1970-01-01
        • 1970-01-01
        • 2017-05-27
        • 1970-01-01
        相关资源
        最近更新 更多