There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
Linux and Unix
Tag Cloud
acer audio backup bios boot bsod compaq computer connection crash dell driver drivers error excel firefox freeze hard disk hard drive hardware hijackthis internet laptop linksys macro malware network outlook outlook 2003 outlook 2007 password problem recovery redirect server slow sound toshiba trojan usb video virus vista vpn windows windows 7 windows vista windows xp wireless youtube
Search
Search for:
Tech Support Guy Forums > Operating Systems > Linux and Unix >
comparing current date to modified date

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

Closed Thread
 
Thread Tools
cgjoker's Avatar
Senior Member with 205 posts.
 
Join Date: Aug 2003
23-Sep-2005, 02:24 PM #1
comparing current date to modified date
Got another...

How can I compare a modification date of a file, plus say 10 days, and if it equals the current date then move the file?

So...

for file in *
do
curdate=`date +%b +%d`
moddate=`ls -l|grep {files modification date}
if ${moddate} <= 1${curdate} + 10`
then
mv ${file} ./directory/.
fi
done
AGCurry's Avatar
Senior Member with 431 posts.
 
Join Date: Jun 2005
Location: Kansas City area
Experience: advanced but learning
23-Sep-2005, 04:36 PM #2
Everything you need is provided in the "find" command.

Do "man find" to learn more.
cgjoker's Avatar
Senior Member with 205 posts.
 
Join Date: Aug 2003
26-Sep-2005, 09:27 AM #3
Thanks but im not looking for instructions on how to use the find command, im trying to find out how to compare a modified date of a file with a current date. Im not looking for files with a specific modified date.

I would imagine the script would look something like this...

flag=14
cd olddirectory
for file in `ls -l *`
do
if -mtime {$flag} <= `date`
then
mv $file /directory/.
fi
AGCurry's Avatar
Senior Member with 431 posts.
 
Join Date: Jun 2005
Location: Kansas City area
Experience: advanced but learning
26-Sep-2005, 12:52 PM #4
In the past I've written my own C programs to do things like this, as I don't believe you can do exactly what you're describing with pure shell scripts.

However, alternatively you can use the touch command to set the mtime of a specific file, and then use test with the -nt or -ot option:

cd olddirectory
touch -m -t 200509011200 ref_file # Sept 1, 2005, noon
for file in *
do
if [ $file -ot ref_file ]
then
mv $file /directory/.
fi
done
cgjoker's Avatar
Senior Member with 205 posts.
 
Join Date: Aug 2003
27-Sep-2005, 08:45 AM #5
I see how this would work, but unfortunately it doesn't really apply to what im trying to do.

I will not have the novelty of specifying a particular day because this would run every day. I suppose I can substitute the static date with a unix 'date' command. Is there a way I can say,
if [ $file -ot ref_file "by x number of days"]
do
......

?

thanks for your help with this.
c.
Squashman's Avatar
Trusted Advisor with 15,901 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: IIAHYAYCESA,YAADA!
27-Sep-2005, 08:55 AM #6
Been following this thread and I am still confused on what exactly you are trying to do. Do you want to just back up all files every day that are 10 days old and I assume you will just set this up as a cron job.
cgjoker's Avatar
Senior Member with 205 posts.
 
Join Date: Aug 2003
27-Sep-2005, 10:05 AM #7
the idea is that ill have a file with a numeric flag as part of the filename, that will always be changing. based on this flag i will mv the file if the file is older than that specific flag value.

so one day the filename may be:

10file.dat

if the file is older than 10 days, move the file.

the next day the filename may be:

7file.dat

if the file is older than 7 days, move the file.

this script would run daily.

so what i was thinking was i could basically say, if a file modification date, plus the flag is less than or equal to the current date then move the file.

to get the flag I was thinking i can just do like a ls into a file then substring (sed) the first 2 characters to get the value of the flag. the problem may be though if there is more than one file... so:

10file1.dat
12filenew.dat
2filewhatever.dat
cgjoker's Avatar
Senior Member with 205 posts.
 
Join Date: Aug 2003
27-Sep-2005, 10:24 AM #8
this is something like the script as id imagine it.. obviously not to code.

date= date command to get this syntax for current date(200509011200)
ls * >> filename.dat
for (each line of file) filename.dat
do
flag= (get 1st 2 characters of filename)
rm line
touch -m -t ${date} ref_file
for file in *
do
if [ $file + $flag -ot ref_file ]
then
mv $ file ./directory/$file
fi
done
done
cgjoker's Avatar
Senior Member with 205 posts.
 
Join Date: Aug 2003
27-Sep-2005, 10:50 AM #9
i think this may do what i want as well.

ls * >> filename.dat
for (each line in) filename.dat
do
flag= (get 1st 2 char's.)
rm line
for file in `find . -type f -mtime +$flag -name "$flag*.*"`
do
mv "$file" /directory/"$file".dat
done
done
AGCurry's Avatar
Senior Member with 431 posts.
 
Join Date: Jun 2005
Location: Kansas City area
Experience: advanced but learning
27-Sep-2005, 03:25 PM #10
Okay. This solution requires the presence of the "stat" command.

other_directory=/whatever/place
now=$(date '+%s') # Current Unix time in seconds

for file in *
do
daysage=$(echo $file | tr -d [:alpha:][:punct:]) #extract number in name
mtime=$(stat -c'%Y' $file) # file's mtime
let seconds_age=$now-$mtime # file age in seconds
let max_seconds=$daysage\*86400 # convert file's max age to seconds
if [ $seconds_age -gt $max_seconds ]
then
mv $file $other_directory
fi
done

I wish indentation was preserved here!
Squashman's Avatar
Trusted Advisor with 15,901 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: IIAHYAYCESA,YAADA!
27-Sep-2005, 06:44 PM #11
Quote:
Originally Posted by AGCurry
I wish indentation was preserved here!
Wrap it in the code tags. [ code ] [ / code ]

Quote:
Originally Posted by AGCurry
Okay. This solution requires the presence of the "stat" command.
I was thinking the same thing when I first read this thread a couple of days ago or using statftime.
cgjoker's Avatar
Senior Member with 205 posts.
 
Join Date: Aug 2003
28-Sep-2005, 10:36 AM #12
excuse my ignorance, but when I attempt to run the code I get an error:
./file.sh[42]: seconds_age=%s-: 0403-057 Syntax error
./file.sh[41]: stat: not found.

Im using Korne shell, and if I man stat it tells me the entry is not found.

Im also guessing the
now=$(date '+%s')
it doesn't like.

Last edited by cgjoker : 28-Sep-2005 11:10 AM.
cgjoker's Avatar
Senior Member with 205 posts.
 
Join Date: Aug 2003
28-Sep-2005, 10:47 AM #13
also, I can figure this out on my own, but I need only the first 2 digits of the filename in case there are other numeric values in the filename.

this should work fine...

echo $file | head -c2

I think ideally though id like the command to get whatever characters are at the beginning of the filename that are numeric... so if I have a filename of 10file.dat the returned value is 10, alternatively, if the filename is 2file.dat, then the returned value is 2.

Last edited by cgjoker : 28-Sep-2005 11:13 AM.
AGCurry's Avatar
Senior Member with 431 posts.
 
Join Date: Jun 2005
Location: Kansas City area
Experience: advanced but learning
28-Sep-2005, 11:27 AM #14
Quote:
Originally Posted by cgjoker
excuse my ignorance, but when I attempt to run the code I get an error:
./file.sh[42]: seconds_age=%s-: 0403-057 Syntax error
./file.sh[41]: stat: not found.

Im using Korne shell, and if I man stat it tells me the entry is not found.
The stat command is found on some systems, but not all. I have it on my Red Hat system here at work, and I've also found it on other systems. It basically makes available any or all elements of the C "struct stat". The ls command is basically a listing of struct stat elements. If you don't have it, it's a pretty simple C program to write. The syntax error above is because stat - because it's not found - returned nothing.

OR... some versions of ls have an option like:

ls -l --time-style='+%s'

which will present the mtime in Unix epoch time.

echo $file | head -c2

will return the first two LINES of $file, not the first two characters. Use

"cut -c1-2" for the first two characters.

so...

[code]
other_directory=/whatever/place
now=$(date '+%s') # Current Unix time in seconds

ls -g --time-style='+%s' | while read perms links owner size mtime file
do
if [ -f $file ]
then
daysage=$(echo $file | cut -c1-2) #extract first two chars of name
if let $daysage=$daysage+0 > /dev/null # numeric?
then
let seconds_age=$now-$mtime # file age in seconds
let max_seconds=$daysage\*86400 # convert max age to seconds
if [ $seconds_age -gt $max_seconds ]
then
mv $file $other_directory
fi
fi
fi
done
[\code]
cgjoker's Avatar
Senior Member with 205 posts.
 
Join Date: Aug 2003
28-Sep-2005, 01:33 PM #15
nope, no dice...

server1:$:/]ls -l --time-style='+%s'
ls: Not a recognized flag: -
ls: Not a recognized flag: -
ls: Not a recognized flag: y
ls: Not a recognized flag: =
ls: Not a recognized flag: +
ls: Not a recognized flag: %
Usage: ls [-1ACFLNRabcdefgilmnopqrstux] [File...]
Closed Thread Bookmark and Share   techguy.org/401505

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Smart Search

Find your solution!



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.

Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 07:35 AM.
Copyright © 1996 - 2010 TechGuy, Inc. All rights reserved.
Powered by Cermak Technologies, Inc.