Check date of a file in unix
In my unix shell script, I have a for loop and looping thru the files in a directory, I want to check the modified date of a file开发者_Go百科 is greater than some number of days, say 10 days and do some processing. Any help in this regards is highly appreciated.
The find command has a built-in test for this:
find <yourdir> -type f -mtime +10 -exec yourproc {} \;
This will run "yourproc matching_filename" on each file in <yourdir>
older than 10 days.
The -ctime test works similarly, using the inode change time rather than the modification time.
find . -mtime +10
This could be integrated into a script in several ways. For one:
isOlder () {
[ "`find $1 -mtime +10`" == "$1" ]
return $?
}
ls | while read fname; do
if isOlder "$fname"; then
echo $fname is older
else
echo $fname is not older
fi
done
I listed the above because it sounded like you mainly wanted a test and were doing other processing. OTOH, if you really just want the older files:
$ find . -mtime +10 | while read fname; do
> echo $fname
> done
精彩评论