LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 09-29-2010, 09:41 AM   #1
kamalp
LQ Newbie
 
Registered: Sep 2010
Posts: 19

Rep: Reputation: 0
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)?

Thanks a lot,
Kamal
 
Old 09-29-2010, 10:07 AM   #2
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Hi kamalp,

Try this

Code:
ls -lt dir2/* dir1/*
OR just this

Code:
ls -lt *
The one which gets listed on top is the latest modified.

Last edited by vinaytp; 09-29-2010 at 10:08 AM.
 
Old 09-29-2010, 10:43 AM   #3
kamalp
LQ Newbie
 
Registered: Sep 2010
Posts: 19

Original Poster
Rep: Reputation: 0
Hi,

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.

Thanks,
Kamal

Last edited by kamalp; 09-29-2010 at 10:45 AM.
 
Old 09-29-2010, 10:53 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,693

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Maybe check out man find and look at the -ctime, -cmin, -cnewer, ... options
 
Old 09-29-2010, 11:06 AM   #5
kamalp
LQ Newbie
 
Registered: Sep 2010
Posts: 19

Original Poster
Rep: Reputation: 0
Hi,

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.

Thanks,
Kamal
 
Old 09-29-2010, 11:09 AM   #6
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Hi kamalp,

This is the output from my system

Code:
[308718@CDCTGIMCLSB ~]$ touch dir1/file

[308718@CDCTGIMCLSB ~]$ ls -lt dir1/* dir2/*
-rw-rw-r-- 1 308718 308718 0 Sep 29 20:35 dir1/file
-rw-rw-r-- 1 308718 308718 0 Sep 29 19:36 dir2/file

[308718@CDCTGIMCLSB ~]$ touch dir2/file


[308718@CDCTGIMCLSB ~]$ ls -lt dir1/* dir2/*
-rw-rw-r-- 1 308718 308718 0 Sep 29 20:35 dir2/file
-rw-rw-r-- 1 308718 308718 0 Sep 29 20:35 dir1/file
If you observe carefully. Order of Outputs with respect to directories are different after the execution of touch dir1/file and touch dir2/file

By the way, which distro are you using ?

Warm Regards,

Last edited by vinaytp; 09-29-2010 at 11:11 AM.
 
Old 09-29-2010, 11:20 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,693

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Quote:
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.
 
Old 09-29-2010, 11:21 AM   #8
kamalp
LQ Newbie
 
Registered: Sep 2010
Posts: 19

Original Poster
Rep: Reputation: 0
Hi Vinay,

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,
Kamal
 
Old 09-29-2010, 11:24 AM   #9
kamalp
LQ Newbie
 
Registered: Sep 2010
Posts: 19

Original Poster
Rep: Reputation: 0
Hi grail,

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.

Thanks,
Kamal
 
Old 09-29-2010, 11:37 AM   #10
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Hi kamalp,

Sorry, I was not aware of sub directories inside. Still we can make it work.

Have a look here.

Lets take this directory hierarchy wherein multiple subdirectories also exist.

Code:
[308718@CDCTGIMCLSB dir1]$ pwd
/home/308718/dir1

[308718@CDCTGIMCLSB dir1]$ tree
.
|-- dir11
|   |-- dir111
|   |   `-- file
|   |-- dir112
|   |-- file
|   `-- file2
|-- dir12
|   |-- file
|   `-- new
`-- file

4 directories, 6 files

[308718@CDCTGIMCLSB dir1]$ ls -ltud *
drwxrwxr-x 4 308718 308718 4096 Sep 29 21:04 dir11
drwxrwxr-x 2 308718 308718 4096 Sep 29 21:03 dir12
-rw-rw-r-- 1 308718 308718    0 Sep 29 20:55 file
Lets change time stamp of file which is under dir12

Code:
[308718@CDCTGIMCLSB dir1]$ touch dir12/file

[308718@CDCTGIMCLSB dir1]$ ls -ltud *
drwxrwxr-x 2 308718 308718 4096 Sep 29 21:05 dir12
drwxrwxr-x 4 308718 308718 4096 Sep 29 21:04 dir11
-rw-rw-r-- 1 308718 308718    0 Sep 29 20:55 file
Now if you observe the output, dir12 came up in the order.

Here just make use of -u access time switch available with ls

Warm Regards,

Last edited by vinaytp; 09-29-2010 at 11:40 AM.
 
Old 09-29-2010, 11:39 AM   #11
djsmiley2k
Member
 
Registered: Feb 2005
Location: Coventry, UK
Distribution: Home: Gentoo x86/amd64, Debian ppc. Work: Ubuntu, SuSe, CentOS
Posts: 343
Blog Entries: 1

Rep: Reputation: 72
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.
 
Old 09-29-2010, 11:46 AM   #12
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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}'
)
To run:
Code:
root@reactor: ./newfind Sep
/home/sasha/.i3
root@reactor:
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.
 
Old 09-29-2010, 11:50 AM   #13
kamalp
LQ Newbie
 
Registered: Sep 2010
Posts: 19

Original Poster
Rep: Reputation: 0
Hi Vinay,

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.

Thanks,
Kamal
 
Old 09-29-2010, 11:52 AM   #14
djsmiley2k
Member
 
Registered: Feb 2005
Location: Coventry, UK
Distribution: Home: Gentoo x86/amd64, Debian ppc. Work: Ubuntu, SuSe, CentOS
Posts: 343
Blog Entries: 1

Rep: Reputation: 72
you can use date to get the month:

Code:
date +%b
 
1 members found this post helpful.
Old 09-29-2010, 11:55 AM   #15
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
djksmiley,

cool, good idea. (Not sure if you were talking to me, or OP, but...)

To fix my code (if you really want to use that tangle) so you don't need to enter the date, replace:
Code:
grep ${1}
with:
Code:
grep $(date +%b)
and remove the if .. fi test at the beginning.
 
  


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
Get the absolute path of the latest file modified on a directory mortonmorton Linux - Newbie 2 10-15-2009 10:05 PM
Tail Last Modified File in a Directory tyoungblood Linux - Software 3 08-12-2005 09:42 AM
triggering bash script if directory modified cbb Linux - Software 1 10-23-2004 08:52 AM
monitoring directory to get the modified file names appas Programming 1 09-06-2004 05:56 AM
Get directory stats when a file has been modified? marri Programming 2 05-13-2004 09:44 PM


All times are GMT -5. The time now is 04:38 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration