Find files in a date range?
Dave M
DaveM at Mich.Com
Mon Jul 30 14:57:13 UTC 2007
At 03:41 PM 7/29/2007 , Alex wrote:
>Dave M said the following on 07/27/2007 08:19 PM:
>>Is there a way to use 'find' to traverse a directory tree and print the
>>full paths to files in a date range? I can do:
>><<snip>>
>>-------------------------------
>>Dave M
>>
>>
>Here's slightly impoved version. Better instructions. Run it with no
>parameters.
Thank you for your help. That example showed me the missing magic. I guess
I missed the part in the man pages regarding using "-mtime +n" and "-mtime
-n" to search a range of dates.
For the BASH fans on the list, here is the bash script that I came up with:
- begin
-----------------------------------------------------------------------------
#!/bin/bash
#
# Copyright 2007, Dave Markus (Davem at Mich Dot Com)
#
# - Traverse a directory tree and
# List all files modified within a given date range
# $1 Directory tree to search
# $2 Search Specification
# $2 Start Date
# $3 End Date
# $4-$9 test parameters (for find, see "man find")
#
# If you fix bugs, or make improvements, I would
# appreciate seeing a copy. I am sure there is much room for improvement :)
# Be sure to put "DateRangeFind" in your subject line so I get your message.
#
# This script is free software, and you are welcome to redistribute it
and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License,
# or (at your option) any later version. http://gplv3.fsf.org/
#
# Release:
# 2007.07.30 alpha - Dave Markus, first release
# Tested with:
# - Ubuntu 6.06 (dapper)
#
#Convert date to time-stamp
Date2stamp ()
{ date --utc --date "$1" +%s
}
#Find the number of days between dates
DaysDiff ()
{ Sec=$((($1)-($2)))
if ((Sec < 0)); then
echo $((Sec/-86400))
else
echo $((Sec/86400));
fi
}
#Test for arguments
if [ ! "$1" ] || [ ! $2 ] || [ ! $3 ]; then
echo ""
echo " Script to use \"find\" to list all files within a date range"
echo ""
echo " Useage:"
echo " $ ./DateRangeFind.sh /Dir2Search yyyymmdd yyyymmdd [\"test
arguments\"]"
echo " run \"\$ man find\" or \"\$ find --help\" for details of
\"test arguments\""
echo ""
exit
fi
dir2search="$1"
today=$(date +%s)
param2=$(Date2stamp $2)
param3=$(Date2stamp $3)
#Abort on invalid date argument
if (($param2 > $today)) || (($param3 > $today)); then
echo "DateRangeFind Error: Unable to find files with dates in the future!"
exit 255
fi
#Combine test arguments
if [ ! "$4" ]; then
Tests=""
else
Tests="$4 $5 $6 $7 $8 $9"
fi
# Sort start and end dates
if (($param2 > $param3)); then
end=$param2
start=$param3
else
start=$param2
end=$param3
fi
#Calculate offsets from today
startdays=$(($(DaysDiff $today $start) +1))
enddays=$(($(DaysDiff $today $end) -1))
#Build range argument for find
if (( $startdays > 0 )) && (($enddays > 0 )); then
range="-mtime -$startdays -mtime +$enddays"
else
range="-mtime -$startdays"
fi
#Find the matching files
find -L "$dir2search" $Tests -type f -daystart $range -printf "%h/%f\n"
exit $?
- end
-----------------------------------------------------------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: DateRangeFind.sh
Type: application/octet-stream
Size: 2403 bytes
Desc: not available
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20070730/e1ec6a7a/attachment.obj>
-------------- next part --------------
-------------------------------
Dave M
Davem (at) Mich (dot) Com
Ann Arbor, Mich. USA
Protect your digital rights: http://www.eff.org
"The 'Analog Holes' they want to stop up are our eyes, ears and mouths."
More information about the ubuntu-users
mailing list