Shell Script — Get all files modified after

前端 未结 9 2484
礼貌的吻别
礼貌的吻别 2020-12-12 09:57

I\'d rather not do this in PHP so I\'m hoping a someone decent at shell scripting can help.

I need a script that runs through directory recursively and finds all fil

相关标签:
9条回答
  • 2020-12-12 10:12

    as simple as:

    find . -mtime -1 | xargs tar --no-recursion -czf myfile.tgz
    

    where find . -mtime -1 will select all the files in (recursively) current directory modified day before. you can use fractions, for example:

    find . -mtime -1.5 | xargs tar --no-recursion -czf myfile.tgz
    
    0 讨论(0)
  • 2020-12-12 10:18

    This script will find files having a modification date of two minutes before and after the given date (and you can change the values in the conditions as per your requirement)

    PATH_SRC="/home/celvas/Documents/Imp_Task/"
    PATH_DST="/home/celvas/Downloads/zeeshan/"
    
    cd $PATH_SRC
    TODAY=$(date  -d "$(date +%F)" +%s)
    TODAY_TIME=$(date -d "$(date +%T)" +%s)
    
    
    for f in `ls`;
    do
    #       echo "File -> $f"
            MOD_DATE=$(stat -c %y "$f")
            MOD_DATE=${MOD_DATE% *}
    #       echo MOD_DATE: $MOD_DATE
            MOD_DATE1=$(date -d "$MOD_DATE" +%s)
    #       echo MOD_DATE: $MOD_DATE
    
    DIFF_IN_DATE=$[ $MOD_DATE1 - $TODAY ]
    DIFF_IN_DATE1=$[ $MOD_DATE1 - $TODAY_TIME ]
    #echo DIFF: $DIFF_IN_DATE
    #echo DIFF1: $DIFF_IN_DATE1
    if [[ ($DIFF_IN_DATE -ge -120) && ($DIFF_IN_DATE1 -le 120) && (DIFF_IN_DATE1 -ge -120) ]]
    then
    echo File lies in Next Hour = $f
    echo MOD_DATE: $MOD_DATE
    
    #mv $PATH_SRC/$f  $PATH_DST/$f
    fi
    done
    

    For example you want files having modification date before the given date only, you may change 120 to 0 in $DIFF_IN_DATE parameter discarding the conditions of $DIFF_IN_DATE1 parameter.

    Similarly if you want files having modification date 1 hour before and after given date, just replace 120 by 3600 in if CONDITION.

    0 讨论(0)
  • 2020-12-12 10:21

    You can do this directly with tar and even better:

    tar -N '2014-02-01 18:00:00' -jcvf archive.tar.bz2 files
    

    This instructs tar to compress files newer than 1st of January 2014, 18:00:00.

    0 讨论(0)
  • 2020-12-12 10:23

    You can get a list of files last modified later than x days ago with:

    find . -mtime -x
    

    Then you just have to tar and zip files in the resulting list, e.g.:

    tar czvf mytarfile.tgz `find . -mtime -30`
    

    for all files modified during last month.

    0 讨论(0)
  • 2020-12-12 10:26

    I would simply do the following to backup all new files from 7 days ago

    tar --newer $(date -d'7 days ago' +"%d-%b") -zcf thisweek.tgz .
    

    note you can also replace '7 days ago' with anything that suits your need

    Can be : date -d'yesterday' +"%d-%b"

    Or even : date -d'first Sunday last month' +"%d-%b"

    0 讨论(0)
  • 2020-12-12 10:28

    If you have GNU find, then there are a legion of relevant options. The only snag is that the interface to them is less than stellar:

    • -mmin n (modification time in minutes)
    • -mtime n (modification time in days)
    • -newer file (modification time newer than modification time of file)
    • -daystart (adjust start time from current time to start of day)
    • Plus alternatives for access time and 'change' or 'create' time.

    The hard part is determining the number of minutes since a time.

    One option worth considering: use touch to create a file with the required modification time stamp; then use find with -newer.

    touch -t 200901031231.43 /tmp/wotsit
    find . -newer /tmp/wotsit -print
    rm -f /tmp/wotsit
    

    This looks for files newer than 2009-01-03T12:31:43. Clearly, in a script, /tmp/wotsit would be a name with the PID or other value to make it unique; and there'd be a trap to ensure it gets removed even if the user interrupts, and so on and so forth.

    0 讨论(0)
提交回复
热议问题