LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-03-2012, 04:16 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,485

Rep: Reputation: 51
Looking into .../foo/ tree for the file with the greatest mtime.


Hi:
I want to inspect the whole tree with vertex at directory foo/ looking for the latest file that was copied to that tree. I.e., I would be looking for the greatest mtime and its corresponding filename. I guess find is a candidate command for this, in combination with some other one.

What would be an appropriate command line for this task?
 
Old 03-03-2012, 04:25 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Find and sort

check out the -printf option for find--the %T and %p special fields specifically. I haven't tested this personally, but something like this should work:

Code:
find /path/to/foo -printf "%T@ %p\n" | sort -rn
 
1 members found this post helpful.
Old 03-03-2012, 04:56 AM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,485

Original Poster
Rep: Reputation: 51
It works to perfection! I think I understand. I always used find with the -name option to find a file whose name or part of it I already knew. Lacking all options except -printf, it just lists every file in the tree. -printf is like -print except I can specify a format string. %T@ makes it print mtime in seconds, %p, the filename. The presence of '\n' I do not quite understand.

Really, find deserves that one allots a good time to its study. Thanks a lot.
 
Old 03-03-2012, 05:14 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
The '\n' just causes the find output to advance to a new line after printing the time and filename. Normally find does that by default, but that default behavior is suppressed when you use -printf. So you must explicitly include '\n' whenever you want to start a new line in the output.

And as a side note, you can combine more options like you normally would. For instance, you could add '-type f' to limit the list to regular files... or use combinations of '-path' and '-prune'... or all kinds of other good stuff.

Like you said, find's man page has lots of info. It won't be wasted time if you ever decided to experiment with it whenever you have the time and interest. It's quite a flexible tool.
 
Old 03-03-2012, 07:20 PM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,485

Original Poster
Rep: Reputation: 51
The file I was looking for was copied into foo/ as a result of downloading a file from the web, being in the GUI. I now think that if I had copied it by hand, as I usually use cp with the -a option, which implies --preserve=mode,ownership,timestamps, the task of finding it would have been an impossible mission. [Not quite so: ls -c would give me the time the copy was made (the time when I issued the cp command). The task is more difficult now, but not impossible.]
 
  


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 can I move a file when it's mtime is now? tdnnash25 Linux - Server 3 02-17-2011 02:15 PM
using find to compare file mtime with another file's mtime TheFueley Linux - Newbie 1 12-23-2008 09:06 PM
Can you move a file or directory and still retain mtime? ddenton Linux - General 3 11-21-2008 05:54 PM
Which config file should I use... foo or foo.new? davidguygc Slackware 6 08-01-2007 06:21 PM
why does sftp change mtime file attribute? hazelhill Linux - Software 3 10-17-2004 10:42 PM


All times are GMT -5. The time now is 03:12 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration