How to find files older than N days from a given timestamp

I want to find files older than N days with a given timestamp in the format YYYYMMDDHH

I can find a file older than 2 days with the following command, but it finds files with the current time:

find /path/to/dir -mtime -2 -type f -ls

Let's say I give input. timestamp=2011093009I want to find files older than 2 days with 2011093009.

Carried out my research, but did not seem to understand this.

+5
source share
3 answers

This is mainly achieved by searching for files in a date range ...

I used perl to calculate the days from today to a given timestamp, since the GNU date is not available on my system, so -dit is not an option. The code below accepts the date in YYYYDDMM format. See below:

#!/usr/bin/perl 
use Time::Local;

my($day, $month, $year) = (localtime)[3,4,5];
$month = sprintf '%02d', $month+1;
$day   = sprintf '%02d', $day;
my($currentYear, $currentDM) = ($year+1900, "$day$month");
my $todaysDate = "$currentYear$currentDM";
#print $todaysDate;

sub to_epoch {
    my ($t) = @_;  
    my ($y, $d, $m) = ($t =~ /(\d{4})(\d{2})(\d{2})/); 
    return timelocal(0, 0, 0, $d+0, $m-1, $y-1900);
}
sub diff_days {
    my ($t1, $t2) = @_;  
    return (abs(to_epoch($t2) - to_epoch($t1))) / 86400; 
}
print diff_days($todaysDate, $ARGV[0]);

** . Perl, , /. , , Perl

korn script , .

#!/bin/ksh
daysFromToday=$(dateCalc.pl 20110111)
let daysOld=$daysFromToday+31
echo $daysFromToday "\t" $daysOld

find /path/to/dir/ -mtime +$daysFromToday -mtime -$daysOld -type f -ls

+$daysFromToday, , -$daysOld

0

$(),

( sputnick)

date=2011060109; find /home/kenjal/ -mtime $(( $(date +%Y%m%d%H) - $(date -d $date +%Y%m%d%H) ))
0
#!/usr/bin/env bash

# getFiles arrayName olderDate newerDate [ pathName ]
getFiles() {
    local i
    while IFS= read -rd '' "$1"'[(_=$(read -rd "" x; echo "${x:-0}")) < $2 && _ > $3 ? ++i : 0]'; do 
        :
    done < <(find "${4:-.}" -type f -printf '%p\0%Ts\0')
}

# main date1 date2 [ pathName ]
main() {
    local -a dates files
    local x
    for x in "${@:1:2}"; do
        dates+=( "$(date -d "$x" +%s)" ) || return 1
    done

    _=$dates let 'dates[1] > dates && (dates=dates[1], dates[1]=_)'
    getFiles files "${dates[@]}" "$3"
    declare -p files
}

main "$@"

# vim: set fenc=utf-8 ff=unix ts=4 sts=4 sw=4 ft=sh nowrap et:

Bash script find. getFiles , mtimes . script .

Bash GNU. "N ", GNU, . . .

, printf '%(%s)T' ... , GNU, , .

Edit

, ksh, , -, ksh93 printf GNU -d like string. , , , ksh93. script.

0

All Articles