[SOLVED] Looking into .../foo/ tree for the file with the greatest mtime.
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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:
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.
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.
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.]
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.