导航目录和子目录并使用shell移动文件

时间:2016-08-03 09:58:04

标签: shell unix backup

目录结构

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

2 个答案:

答案 0 :(得分:0)

必须离开去开会。我会给你留下我一直在努力帮助你的剧本:

@echo off
    setlocal enableextensions disabledelayedexpansion

    rem Give a path as argument or use current active directory
    for %%a in ("%~f1.") do set "target=%%~fa"

    rem Prepare variables to handle phase order correction and 
    rem storage of intermediate/final values
    set /a "_d=0", "_a=1", "_b=2", "_f=3"
    set "lastValue="
    set "lastVersion="

    rem %%a : Search the folders and split their name using dot as delimiter
    rem       %%a = major
    rem       %%b = minor
    rem       %%c = update + phase + build
    rem %%d : split %%c using [dabf] as delimters to obtain 
    rem       %%d = update
    rem       %%e = build
    rem %%f : split %%c using numbers as delimiters to obtain
    rem       %%f = phase 
    set "num=[0-9][0-9]*"
    for /f "tokens=1-3 delims=." %%a in ('
        dir /b /ad /o-d "%target%"
        ^| findstr /r /i /x /c:"%num%\.%num%\.%num%[dabf]%num%"
    ') do for /f "tokens=1,2 delims=dDaAbBfF" %%d in ( "%%c"
    )  do for /f "delims=0123456789" %%f in ( "%%c"
    )  do (
        rem Folder name has been splitted. Now, convert each of the segments
        rem to a numerical value, adding 1E6 to get a proper padding as all 
        rem values will be handled as strings to avoid limits in batch 
        rem arithmetic for values over 2^31
        rem Note that %%f is not being iterated here as the values to use 
        rem for phase padding have been previously defined
        for %%v in (
            %%a %%b %%d %%e
        ) do if not defined _%%v set /a "_%%v=1000000 + %%v"

        rem Obtain the properly padded version of the folder segments
        setlocal enabledelayedexpansion
        for /f "tokens=1,2" %%i in (
            "!_%%a!!_%%b!!_%%d!!_%%f!!_%%e! !lastValue!"
        ) do (
            endlocal
            rem Determine if we have found a newer version
            if "%%i" gtr "%%j" (
                set "lastValue=%%i"
                set "lastVersion=%%a.%%b.%%c"
            )
        )
    )

    if defined lastVersion (
        echo Latest version found is : %lastVersion%
    ) else (
        echo No matching folders
    )

答案 1 :(得分:0)

这个数据目录与输出相同

#!/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