sh - Find a file that was created within five days of another file in shell -
i'm still pretty new scripting stick me , if have questions please feel free ask.
okay, so: have file let's file.txt
file.txt exists in directory /this/is/the/directory/file.txt
in separate directory .log files exist tell me happens when file.txt created.
fuubar.log, fuu.log, bar.log, this.log, that.log, some.log, other.log...there unknown number of these logs.
i need gather log files occurred +-5 days of file.txt file being created.
for example: file.txt created on 7 july 2013 (don't pay attention date format) need log files occurred on , between 2 july 2013 , 12 july 2013.
any ideas?
edit: i'm more confused comparing dates of files correct ones, know how copy files.
perl , stat
not available me
since on qnx can't use existing posix/linux/unix/bsd scripts off shelf in approach this. if either install programming languages can or install updated system utilities (from qnx 1 hope) or shell tools achieve same thing might easier, realize not possible.
you try following horrid hackish approach quick "works now" solution though:
first use find /path/to/log/file.txt -printf %cj
give day of year 1-366 in log file created , -printf %cy
give year value. next use expr
+/- 5 did before find values $day_of_year_start , $day_of_year_end can use set $start
, $end
variables in jp's script or variation on it.
now need way epoch time use date -s
can convert epoch time format touch -t
use. without date
can convert , epoch time , no strftime
outputs epoch time make easy, need "hack". 1 messy approach set constant epoch time @ beginning of year (say 1356998400 jan 1 2013 ; plus values other years if needed) , add ($day_of_year_start x 86400) , ($day_of_year_end x 86400) seconds constant 2 epoch time values run through date -s
on qnx , set date/timestamp $start
, $end
respectively.
messy might work :-) let know if try this.
cheers, ps: here's start of script you'll need check command arguments , date formats qnx make sure correct. works on bsd boxes have gfind
(gnu find) , date -r
(instead of date -s
qnx).
#!/bin/sh # find files created within 5 days before/after # creation of file on qnx. # # ./rangesearch /path/to/logfile.txt # # nb: # qnx shell environment lacks posix/gnu features: # 1. 'stat', 'date -r' not available (use find file acm dates) # 2. use gnu 'find' extensions (since -printf needed output of dates) # 3. 'date' cannot modify dates using day/month/year values convert # epoch values in roundabout way. 'find' cannot output epoch dates # due limitations of qnx strftime compare year of file creation # against set of constants , add/subtract daily amounts of # seconds value , use qnx 'date -s' convert these dates # formats usable qnx 'touch'. # # todo: improve , extend year <--> epoch time constant matching # array , loop (bourne shell 'sh' not have # "array" feature per se). # # detect version of find/gfind date/gdate runs on linux osx bsd # # add more precise units (hours minutes seconds) epoch time # calculation (exercise reader) # year2013="1357016400" year2012="1325394000" year2011="1293858000" year2010="1262322000" # .... etc etc, kind of vector or array nice ... fileyear=`find $1 -printf %cy` filedoyr=`find $1 -printf %cj` if [ $fileyear -eq "2013" ]; logfile_epoch=$(( $year2013 + ($filedoyr * 86400) )) echo "file $fileyear or $logfile_epoch ssl" elif [ $fileyear -eq "2012" ]; logfile_epoch=$(( $year2012 +($filedoyr * 86400) )) echo "file $fileyear or $logfile_epoch ssl" elif [ $fileyear -eq "2011" ]; logfile_epoch=$(( $year2011 + ($filedoyr *86400) )) echo "file $fileyear or $logfile_epoch ssl" elif [ $fileyear -eq "2010" ]; logfile_epoch=$(( $year2010 + ($filedoyr *86400) )) echo "file $fileyear or $logfile_epoch ssl" else echo "there problem" exit fi echo "file 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 files $start $end in age \n...\n" touch -t "$start" /tmp/s$$ touch -t "$end" /tmp/e$$ # -print0 , xargs -0 allow file names 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 here means seconds since long ago :-)
Comments
Post a Comment