LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 03-10-2006, 02:43 PM   #1
Oinopion
Member
 
Registered: May 2005
Location: Kraków, PL
Distribution: Slackware
Posts: 33

Rep: Reputation: 15
file age checking


Hi!
Sorry for such noob question, but how to check which of two files is newer?

Thx
 
Old 03-10-2006, 02:45 PM   #2
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
ls -l <filename> (lower-case L, not the number 1). All files get a date/timestamp when they're created, and it's updated whenever they're updated (written to).
 
Old 03-10-2006, 02:47 PM   #3
Oinopion
Member
 
Registered: May 2005
Location: Kraków, PL
Distribution: Slackware
Posts: 33

Original Poster
Rep: Reputation: 15
Can i do it with regexp? I mean: I have two files a.txt and b.txt and want to know whether a.txt is newer (mod. time) then b.txt and then do sth with newer file. ls -l is known to me, but I don't find this much helpful in automated updater (?!?), but thx any way.

Last edited by Oinopion; 03-10-2006 at 02:50 PM.
 
Old 03-10-2006, 03:00 PM   #4
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
ls -lt will sort files by modification time so you could use the following to return the listing for the new file:
Code:
ls -lt a.txt b.txt | head -1
If you just want the name:
Code:
ls -lt a.txt b.txt | head -1 | awk '{ print $8}'
or
Code:
ls -lt a.txt b.txt | head -1 | cut -d' ' -f9
 
Old 03-10-2006, 03:03 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 683Reputation: 683Reputation: 683Reputation: 683Reputation: 683Reputation: 683
Read the "info bash" manual for more.

Quote:
`FILE1 -nt FILE2'
True if FILE1 is newer (according to modification date) than
FILE2, or if FILE1 exists and FILE2 does not.

`FILE1 -ot FILE2'
True if FILE1 is older than FILE2, or if FILE2 exists and FILE1
does not.
 
Old 03-10-2006, 03:11 PM   #6
Oinopion
Member
 
Registered: May 2005
Location: Kraków, PL
Distribution: Slackware
Posts: 33

Original Poster
Rep: Reputation: 15
So why does in not work:
Code:
if 'a -nt b' then echo "aaa" else echo "bbb" fi;
 
Old 03-10-2006, 03:19 PM   #7
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
Try the following instead:
Code:
if [ a -nt b ]; then echo "aaa"; else echo "bbb"; fi
 
Old 03-10-2006, 03:23 PM   #8
Oinopion
Member
 
Registered: May 2005
Location: Kraków, PL
Distribution: Slackware
Posts: 33

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by gilead
Try the following instead...)
BIG THX. Thas right what I was looking for. And I'll read man bash!
 
Old 03-11-2006, 06:37 AM   #9
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
I'm in the habit of using

Code:
ls -alt
all of the time...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to calculate file age ? "File xy is 2 days 3 hours old" fluppi Linux - Software 5 06-03-2013 10:27 AM
Checking a file is complete. carlmarshall Linux - Software 3 12-14-2005 09:46 AM
Can't get Age of Empires II: Age of Kings started (I've could it before !!!) vegetassj2 Linux - Games 44 08-28-2005 04:59 PM
checking if a file exists veilig Programming 6 02-01-2004 04:16 AM
Java does "age" or "Age" matter when declaring an Int?? Laptop2250 Programming 3 10-13-2003 12:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 07:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration