Computing.Net > Forums > Unix > Comparing time/date of a file...

Comparing time/date of a file...

Reply to Message Icon

Original Message
Name: Laila63
Date: August 27, 2002 at 12:48:00 Pacific
Subject: Comparing time/date of a file...
Comment:

Hi, I'm a newbie to scripting in Unix and I need help with the following.
I want to write a script that would retrieve the creation date/time of a file and compare that to the current date/time to determine how old the file has been created. I have no idea how to do this. Please help me...

Thank you in advance.


Report Offensive Message For Removal


Response Number 1
Name: Ned
Date: August 29, 2002 at 13:03:32 Pacific
Subject: Comparing time/date of a file...
Reply: (edit)

How you will find creation date/time of a file. It is not necessary that the time you get with "ls -l" is the creation date/time.

If you want to compare that "ls -l"'s date/time with current date/time then it can be done.

Let us know.


Report Offensive Follow Up For Removal

Response Number 2
Name: PaulS
Date: September 5, 2002 at 22:24:43 Pacific
Subject: Comparing time/date of a file...
Reply: (edit)

Hi, I too am a 'newbie' at scripting. This is a problem that I have been stuck on for a while.

I have a script that is creating files from another system, and adding them into a specific directory. I would like to compare the creation date of a file with the current system date and delete it if it is older than 2 weeks old. I am not sure how this can be done, or if it can be done. Has anyone got any ideas??? Thanx


Report Offensive Follow Up For Removal

Response Number 3
Name: derfull
Date: September 6, 2002 at 02:39:13 Pacific
Subject: Comparing time/date of a file...
Reply: (edit)

Hi, in order to compare date file with the current date, i use this

find [path] -daystart -[a/c/m]time [+/-]n

-atime n
File was last accessed n*24 hours ago.
-ctime n
File's status was last changed n*24 hours ago.
-mtime n
File's data was last modified n*24 hours ago
+ == >
- == <
n whitout sign === only this day before the current date
You can use it with redirection, or as a kind of filter usefull for remove file
rm -f $(find blabla), and in one command you have a solution

Sorry for my bad english, i'am just a little french


Report Offensive Follow Up For Removal

Response Number 4
Name: Manuel
Date: September 11, 2002 at 06:56:54 Pacific
Subject: Comparing time/date of a file...
Reply: (edit)

Hi,

you can simply use the `test' command for your problem, it will return zero (true) if file1 is newer than file2 (therefore `-nt') and will return > 0 if not:

$> test file1 -nt file2
$> echo $?
1
$>

You can use it in combination with an `if'-statement as well, good for batch scripting:

if [ file1 -nt file2 ]
then
echo "file1 is newer"
fi

Or on the shell directly, optionally in a different notation:

$> test file1 -nt file2 && echo "file1 is newer"
$>

or, other way round:

$> test file1 -nt file2 || echo "file1 is not newer"
file1 is not newer
$>

Hope, I could give you a short introduction. try `man test' on your shell to get more info about the `test' command. Maybe you'll find a good documentation about `if' / `&&' / `||' and return code handling somewhere on the net.

Cheers,
Manuel


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Comparing time/date of a file...

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge