bash script to find old files based off date in file name

前端 未结 4 662
天命终不由人
天命终不由人 2021-02-10 13:15

I\'m developing a bash script that needs to search out files within a single directory that are \"old\" based off a variable that specifies how many days need to pass before the

4条回答
  •  温柔的废话
    2021-02-10 13:52

    Create a bash script isOld.sh like this:

    #!/bin/bash
    
    fileName=$1
    numDays=$2
    
    fileDt=$(echo $fileName | sed 's/^[^-]*-\([^.]*\)\..*$/\1/')
    d1=$(date '+%s')
    d2=$(date -d $fileDt '+%s')
    diff=$((d1-d2))
    seconds=$((numDays * 24 * 60 * 60))
    [[ diff -ge seconds ]] && echo $fileName
    

    Then give execute permission to above file by running:

    chmod +x ./isOld.sh
    

    And finally run this find command from top of your directory to print files older than 7 days as:

    find . -name "contents-*" -exec ./isOld.sh {} 7 \;
    

提交回复
热议问题