Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

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

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

file.txt exists in a directory /this/is/the/directory/file.txt

In a separate directory .log files exist that tell me what happens when file.txt was created.

fuubar.log, fuu.log, bar.log, this.log, that.log, some.log, other.log...there is an unknown number of these logs.

I need to gather all the log files that occurred +-5 days of the file.txt file being created.

For example: file.txt was created on 7 July 2013 (don't pay any attention to date format) I need the log files that occurred on and between 2 July 2013 and 12 July 2013.

Any ideas?

EDIT: I'm more confused about comparing the dates of the files to get the correct ones, i know how to copy files.

Perl and stat are not available to me

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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$$

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...