aspose file tools*
The moose likes Linux / UNIX and the fly likes how to find out if any files under directory recursively modified? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "how to find out if any files under directory recursively modified?" Watch "how to find out if any files under directory recursively modified?" New topic
Author

how to find out if any files under directory recursively modified?

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30752
    
156

I'm trying to write a UNIX command to indicate whether any files under a subdirectory have been modified in the last 24 hours. I need it to recurse within directories as the one changed file could be many levels down. That part is easy:


The thing is this is on the slow side because it has to go through the entire directory structure. I only need a boolean true/false answer. I'd like it to stop once it finds a modified file rather than continue compiling the list. Any thoughts on a way to accomplish this?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ifteqar Ald
Ranch Hand

Joined: Dec 26, 2011
Posts: 73
You can directly execute that command by going to the particular directory that will save execution time
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30752
    
156

I don't know which directory (if any) has a modified file though. If I did, I wouldn't need to run the command. If there are no matches, I understand UNIX having to look at each file. If there is a match, I feel like there should be a way to stop looking.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11477
    
  94

Untried, and a bit of a hack, but would it be possible to run a script that would kill the find itself while writing output somewhere that you could look at? Something like:


Where myScript might be something like:


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30752
    
156

Andrew,
I read about that hack but it adds so much complexity. Especially since I have scores or hundreds of these commands running at once (and then repeated for different directories).

It's actually a non-issue. I had started timing yesterday and found a big directory takes "at least 15 seconds." Turns out it takes 20.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30752
    
156

A co-worker answered my question:


This runs 3-5 times faster than the original. It's hard to say exactly how much faster since each time I run it the command runs faster (presumably less disk lookups).
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30752
    
156

This doesn't work unless the top level directory is the one modified. For example, suppose I have /a/b/c. If I modify a, it works as I expected. If I modify c, it returns c. I'm thinking I have to ladd some code to look for the level of directory I want.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16137
    
  21

Depending on the filesystem, modifying a file in a directory would percolate timestamps upwards so that the parent directories would show the timestamp of the latest file modification under them.

I think probably a straight "find" can be set up to do everything, with, perhaps the help of its "printf" options, but if not, the next best thing would probably be to let find scan for files, printf out their directory paths, and "uniq" the results.

As far as the stopping part goes, I doubt that the actual timestamp checking is going to be much overhead, although if you have to do text processing to pare down returned filepaths, that could add up.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to find out if any files under directory recursively modified?