Find a file that was created within five days of another file in shell

后端 未结 3 1828
南笙
南笙 2020-12-17 04:49

i\'m still pretty new to scripting so stick with me and if you have any questions please feel free to ask.

Okay, so: I have a file let\'s say file.txt

3条回答
  •  生来不讨喜
    2020-12-17 05:06

    Well something to get your started unless someone posts a one-liner!

    # Get the date in you want to start listing files from using your
    # sample file. This will assign the variable in the format of MMDDYYYY
    $ start=$(date -d "-5 days" '+%m%d%Y' < <(date -r /this/is/the/directory/file.txt))
    
    # Get the date in you want to end listing files from. using your
    # sample file. This will assign the variable in the format of MMDDYYYY
    $ end=$(date -d "+5 days" '+%m%d%Y' < <(date -r /this/is/the/directory/file.txt))
    
    # Create two temp files. touch -t will force the timestamp on
    # these files to match the content of variables
    $ touch -t "$start" /tmp/s$$
    $ touch -t "$end" /tmp/e$$
    
    # Use these temp files timestamp as a pivot mechanism by find commands
    # newer option. This will list files whose timestamp is in between our 
    # temp files timestamp which we captured from your sample file
    $ find /path/to/files -type f -newer /tmp/s$$ -and -not -newer /tmp/e$$
    

提交回复
热议问题