查找在 shell 中的另一个文件的五天内创建的文件 [英] Find a file that was created within five days of another file in shell

查看:29
本文介绍了查找在 shell 中的另一个文件的五天内创建的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对脚本编写还很陌生,所以请继续关注我,如果您有任何问题,请随时提出.

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

好的,那么:我有一个文件,比如 file.txt

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

file.txt 存在于目录/this/is/the/directory/file.txt

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

在一个单独的目录中存在 .log 文件,它告诉我创建 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...这些日志的数量未知.

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

我需要收集在创建 file.txt 文件后 +-5 天发生的所有日志文件.

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

例如:file.txt 创建于 2013 年 7 月 7 日(不要注意日期格式)我需要在 2013 年 7 月 2 日和 2013 年 7 月 12 日之间发生的日志文件.

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.

有什么想法吗?

我对比较文件的日期以获得正确的日期更加困惑,我知道如何复制文件.

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

推荐答案

由于您使用的是 QNX,因此您无法在解决此问题的方法中使用现成的现有 POSIX/Linux/Unix/BSD 脚本.如果您可以安装可以帮助您执行此操作的编程语言,或者安装更新的系统实用程序(希望来自 QNX)或 shell 工具来实现相同的功能,那可能会更容易,但我意识到这并不总是可能的.

Since you are on QNX you can't use existing POSIX/Linux/Unix/BSD scripts off the shelf in your approach to this. If you could either install programming languages that can help you do this or install updated system utilities (from QNX one would hope) or shell tools to achieve the same thing it might be easier, but I realize this is not always possible.

不过,您可以尝试以下可怕的骇人听闻的方法来快速立即工作"解决方案:

You could try the following horrid hackish approach for a quick "works now" solution though:

首先使用 find/path/to/log/file.txt -printf %Cj 为您提供从 1 到 366 的一年中创建日志文件的日期和 -printf %CY 为您提供年份值.接下来像之前一样使用 expr +/- 5 来查找 $day_of_year_start 和 $day_of_year_end 的值,然后您可以使用它们来设置 $start$end 变量,就像 JP 的脚本或我的变体一样.

First use find /path/to/log/file.txt -printf %Cj to give you the day of the year from 1-366 in which the log file was created and -printf %CY to give you the year value. Next use expr +/- 5 as you did before to find values for $day_of_year_start and $day_of_year_end which you can then use to set the $start and $end variables just as in JP's script or my variation on it.

现在您需要一种方法来获取与 date -s 一起使用的纪元时间,以便您可以将纪元转换为 touch -t 将使用的时间格式.如果没有 date 可以与纪元时间相互转换,也没有 strftime 可以输出纪元时间来简化这一过程,那么您需要一个hack".一种凌乱的方法是在年初为纪元时间设置一个常量(比如 2013 年 1 月 1 日的 1356998400 ;如果需要,加上其他年份的值),然后添加 ($day_of_year_start x 86400) 和 ($day_of_year_end x 86400) 秒到该常量以获得两个纪元时间值以在 QNX 上运行 date -s 并为 $start$end分别.

Now you need a way to get epoch time for use with date -s so you can convert from epoch to a time format touch -t will use. Without a date that can convert to and from epoch time and no strftime that outputs epoch time to make this easy, you need a "hack". One messy approach would be to set a constant for the epoch time at the beginning of a year (say 1356998400 for Jan 1 2013 ; plus values for other years if needed) and then add ($day_of_year_start x 86400) and ($day_of_year_end x 86400) seconds to that constant to get two epoch time values to run through date -s on QNX and set a date/timestamp for $start and $end respectively.

凌乱,但它可能确实有效:-) 如果您尝试此操作,请告诉我们.

Messy but it might actually work :-) Let us know if you try this.

干杯,ps:这是脚本的开始,您需要检查 QNX 的命令参数和日期格式,以确保它们是正确的.它适用于我使用 gfind(GNU find)和 date -r(而不是 QNX 的 date -s)的 BSD 机器.

Cheers, ps: here's the start of a script you'll need to check command arguments and date formats for QNX to make sure they are correct. It works on BSD boxes I have with gfind (GNU find) and date -r (instead of date -s for QNX).

#!/bin/sh
# Find files created within 5 days before/after
# the creation of another file on QNX.
# 
#  ./rangesearch /path/to/logfile.txt
# 
# NB: 
# QNX shell environment lacks some POSIX/GNU features:
# 1. 'stat', 'date -r' are not available (use find to get file ACM dates)
# 2. use GNU 'find' extensions (since -printf is needed for output of dates)
# 3. 'date' cannot modify dates using day/month/year values so we convert
#    to epoch values in a roundabout way. 'find' cannot output epoch dates
#    due to limitations of QNX strftime so we compare year of file creation
#    against a set of constants and then add/subtract daily amounts of 
#    seconds from this value and use QNX 'date -s' to convert these dates to 
#    formats usable by QNX 'touch'. 
#
# TODO: improve and extend year <--> epoch time constant matching with
#       an array and for loop (Bourne shell 'sh' does not really have
#       an "array" feature per se). 
#
#       detect version of find/gfind date/gdate so this runs on Linux OSX BSD 
#
#       add more precise units (hours minutes seconds) to epoch time
#       calculation (exercise for reader)
#

year2013="1357016400"
year2012="1325394000"
year2011="1293858000"
year2010="1262322000"
# .... etc etc,  some kind of vector or array would be nice ...

fileyear=`find $1 -printf %CY`
filedoyr=`find $1 -printf %Cj`

if [ $fileyear -eq "2013" ]; then
  logfile_epoch=$(( $year2013 + ($filedoyr * 86400) ))
  echo "file is from $fileyear or $logfile_epoch SSL"
elif [ $fileyear -eq "2012" ]; then
  logfile_epoch=$(( $year2012 +($filedoyr * 86400) ))
  echo "file is from $fileyear or $logfile_epoch SSL"
elif [ $fileyear -eq "2011" ]; then
  logfile_epoch=$(( $year2011 + ($filedoyr *86400) ))
  echo "file is from $fileyear or $logfile_epoch SSL"
elif [ $fileyear -eq "2010" ]; then
  logfile_epoch=$(( $year2010 + ($filedoyr *86400) ))
  echo "file is from $fileyear or $logfile_epoch SSL"
else
  echo "there is a problem"
  exit
fi

echo "file is from day $filedoyr of $fileyear"

epochal_start=$(( $logfile_epoch - (5*86400) ))
epochal_end=$(( $logfile_epoch + (5*86400) ))
start=`date -s $epochal_start +%Y%m%d%H%S`
end=`date -s $epochal_end +%Y%m%d%H%S`

echo "epochal_start $epochal_start"
echo "epochal_end $epochal_end"

echo -e "Searching for files from $start to $end in age \n...\n"

touch -t "$start" /tmp/s$$
touch -t "$end" /tmp/e$$

# -print0 and xargs -0 allow for file names with whitepace
find . -type f -newer /tmp/s$$ -and ! -newer /tmp/e$$ -print0 |xargs -0 ls -tal

# clean temporary files:
rm /tmp/s$$
rm /tmp/e$$

这里的 SSL 表示很久以前的秒数 :-)

SSL here means Seconds Since Long ago :-)

这篇关于查找在 shell 中的另一个文件的五天内创建的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆