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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Comparing time/date of a file...

Reply to Message Icon

Name: Laila63
Date: August 27, 2002 at 12:48:00 Pacific
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.



Sponsored Link
Ads by Google

Response Number 1
Name: Ned
Date: August 29, 2002 at 13:03:32 Pacific
Reply:

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.


0

Response Number 2
Name: PaulS
Date: September 5, 2002 at 22:24:43 Pacific
Reply:

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


0

Response Number 3
Name: derfull
Date: September 6, 2002 at 02:39:13 Pacific
Reply:

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


0

Response Number 4
Name: Manuel
Date: September 11, 2002 at 06:56:54 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Comparing time/date of a file...

how to get the modified time of a file www.computing.net/answers/unix/how-to-get-the-modified-time-of-a-file/2110.html

update the contents of a file in Un www.computing.net/answers/unix/update-the-contents-of-a-file-in-un/5963.html

how to identify the size of a file www.computing.net/answers/unix/how-to-identify-the-size-of-a-file-/7523.html