[SOLVED] How to find the directory with latest modified content??
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.
How to find the directory with latest modified content??
Hi,
I want to know how to find the latest modified directory.
I know that the command 'ls -rtl' gives the latest modified file/directory at the end.
But my specific requirement is: If I create two directories named dir1, dir2 in the same order. so now my latest modified directory is "dir2". Now inside each directories, I created a file. Now in the last, I modify the file in "dir1". So overall, the content of dir1 got modified recently.
If I use the command 'ls -rtl', this will still show dir2 as latest, as it is created recently. But I want the directory in which any internal content at any sub-hierarchy modified recently.
so with what linux command I'll get this latest modified directory (dir1)?
No, your commands are working similar as the command 'ls -rtl'. It is checking only the directory created timestamp. but not the internal content modified time.
when we update any file in a directory, the file gets latest time stamp. Also the directory in which the file will get the same latest time stamp. but no more upper level directories timestamp get changed.
I want to find the directory whose internal content at any sub-hierarchy got modified latestly.
I tried these options in find command, but all these commands require some relative file or no. of minute/hours.
Also these options are not very useful, say when I have a file modified after populating a large database (say from cvs version database). In this situation i've to give exact minute before i modified the file and after populating the database.
If i give later time, no output for command. If I give earlier time, all files are shown as a match..
Also these options are not giving the output in timed order.
pls suggest if there is command or script to suit my requirement.
In this situation i've to give exact minute before i modified the file and after populating the database.
If i give later time, no output for command. If I give earlier time, all files are shown as a match..
You should read the man page a little closer:
Code:
find . -type f -cmin 5
Show files who changed exactly 5 minutes ago
Code:
find . -type f -cmin -5
Show files who changed within the last 5 minutes
Quote:
Also these options are not giving the output in timed order.
Well firstly this was not an original requirement. You asked to get information showing the most recently changed, hence if you use -1 above it will show all changed within the last minute.
However, the sort command should be able to help you further.
yes, you are right..
As i said earlier, when you modify a file, the directory's time stamp also gets updated. but not any above directories.
In the simple example you showed, there are no sub directories. pls try with sub-directories, you will find the difference.
Also as we may not be aware of directory structure, we simply can't use commands like 'ls dir1/* dir2/*' (this command here points to files in dir1 and dir2 but not any sub-directory's files if any)
thanks for the info. but still the find options needs some time range input.
but what if i don't know what is the latest time that any file got modified? and this is the most probable situation.
So you want to find the directory, containing the last modified file.... right?
something like
Code:
find / -mtime +1 | tail --lines=1
Note: I've written this by googling man pages while at work. Its likely NOT to work and is just a concept.
The idea is find all the files modified within the last 24 hours, and then list the last one. You can of course increase the 24 hour time to whatever you wish, but I'd expect a system to be used every 24hrs
Its likely a bad use of tail, but I dont know another way to only return the first (or last) line.
Not sure if this will help, but it seems to work for me, although I can almost guarantee there's a more efficient way to do this. And it does require a little bit of human input: you have to supply the current month, the same format as it is given in `ls` output. So, Sep would be given for September.
Code:
#!/bin/bash
if [ "${1}" = "" ]; then
echo "Script requires an argument: Month, e.g: Sep"
exit 1
fi
dirname $(
for eachdir in $(find /home/sasha/ -type d); do
find "$eachdir" -type f -maxdepth 1 -ctime 0 -exec ls -ltr "{}" \; 2>/dev/null | head -n1
done | sort -Mr | grep ${1} | sort -k7 | tail -n1 | awk '{print $NF}'
)
I called it "newfind", and execute it giving it the Sep argument, indicating "September" (i.e. the current month). The directory produced by the code, contains the most recently modified file in my search path. You will need to modify the search path in the script to suit the location you are searching.
NOTE: While it appears to work great for me, you need to test it and verify its results. And again: it's pretty clunky, so no warranty is included.
Thanks for working on this problem.
but 'ls -u' is similar to 'ls -t'. only difference is access time or modified time.
the typical case would be to find when a file is modified.
Still the problem remains with ls to find the directory with latest modified content.
In your example, use touch to update file at dir1/dir11/dir111/file and try to find out the latest modified directory being at dir1 level or above.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.