How to find the recent file in many sub-directories?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find the recent file in many sub-directories?
# 8  
Old 06-16-2010
maybe...with any modification...
Code:
for element in $(ls -d *)
do
   if [ -d $element ]
   then
      echo "directory: $element"
      reciente=$(ls -lt $element | head -2 |tail -1)
      echo $reciente
   fi
done

# 9  
Old 06-16-2010
What Operating System and version do you have. What shell(s) do you have available (e.g. ksh) ?

If this is unix rather than certain Linux variants it will not be possible to do what you require in one command. It will need a shell script.
# 10  
Old 06-16-2010
I checked in Solaris and -P is not working u can use the find command without -P option

Code:
find . -name *.log -type f -mtime +1 -print 2>/dev/null


Last edited by Scott; 06-16-2010 at 07:10 PM..
# 11  
Old 06-16-2010
One way is to walk the tree then look at each directory for the newest file, taking into account that there may be non-files in the directory.


Code:
#!/bin/ksh
DIR_START="$1"
if [ "${DIR_START}""X" = "X" ]
then
        DIR_START="`pwd`"
fi
#
find "${DIR_START}/" -type d -follow -print | while read DIR
do
        # Look for newest file in directory (which is definitely a file)
        cd "${DIR}"
        ls -1t | while read FILENAME
        do
                if [ -f "${FILENAME}" ]
                then
                        ls -lad "${DIR}/${FILENAME}"
                        break
                fi
        done
done

# 12  
Old 06-16-2010
Thanks guys!
albertogarcia and methyl - I've checked your code and both of them are exellent.
merajh - also this command fails in Solaris (version 10).
Probably, writing small code is better than one "find" command ...
# 13  
Old 06-16-2010
Quote:
Originally Posted by omerzzz
lose the double quotation
worked for me in Ubuntu and red-hat.
Code:
uid=0(root) gid=0(root) groups=0(root)
root@omer-desktop:/# find -P . -name *.log
./tmp/vmware-root/appLoader-9051.log
./tmp/vmware-root/setup-9530.log
...<snipped for brevity>...

Actually, you most definitely want to quote that glob, using "*.log" or '*.log' or \*.log. If unescaped, the shell expands that glob before the find command ever sees it. That command worked because there were no matches to *.log in the current directory, which from the looks of it is the top level /. When there are no matches, the shell leaves the pattern as is. Had there been one match, find would have been looking for a file with that specific name throughout the entire file hierarchy. Had there been multiple matches, the glob would've been expanded by the shell into a list and most likely would've lead to a find syntax error.

An illustrative example with sh tracing enabled (after `set -x`, each line beginning with a "+" is the command that the shell is actually executing):
Code:
$ mkdir tmp
$ cd tmp
$ ls -a
.       ..
$ set -x
$ find . -name *.log
+ find . -name '*.log'
$ touch 1.log
+ touch 1.log
$ find . -name *.log
+ find . -name 1.log
./1.log
$ touch 2.log
+ touch 2.log
$ find . -name *.log
+ find . -name 1.log 2.log
find: 2.log: unknown expression primary
$ find . -name \*.log
+ find . -name '*.log'
./1.log
./2.log

Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help script find file most recent

Hi, I need to find the most recent files by their name from an X repertoire. The problem is that the name of the files is of type: POWERL10_20151203000.xml POWERL10_20151203001.xml POWERL10_20151202000.xml FIXED VALUE_DATENNN.xml NNN = Sequential number I would need to recover the... (4 Replies)
Discussion started by: verita
4 Replies

2. UNIX for Dummies Questions & Answers

Find most recent file and copy to another directory.

Very new to shell scripting. Not sure if my title is correct but I will try and explain. Directory has 100+ files with this format, " ABCD_ABC_Abc_AB0126.abc ". When a new file gets created, the 16-19 characters in the file name gets incremented by 1. Ex...todays most recent file is... (14 Replies)
Discussion started by: askvip
14 Replies

3. UNIX for Dummies Questions & Answers

Getting directories resulted from Find in one file

Hi Need help for the following scenario. I am having two directories /tmp/a and /tmp/b. /tmp/a again has subdirectories /tmp/a/aa and /tmp/a/ab. I want to run a script from /tmp/b to search for a file user.lst in the folders /tmp/a/aa and /tmp/a/ab, if available, i want to make a file in... (3 Replies)
Discussion started by: sudeep.id
3 Replies

4. Shell Programming and Scripting

Find Directories That Do Not Contain A Certain File

I'm having problems figuring out the process to find directories that DO NOT contain a certain file. I have a mp3 collection that all the album art is name "folder.jpg". Not all the albums have images. I need a way to find the albums/directories that do not contain "folder.jpg". I can find the... (2 Replies)
Discussion started by: subsonic
2 Replies

5. Shell Programming and Scripting

find directories that do NOT contain a certain file extension

for x in `find /vmfs/volumes/v01tstn01a01/ -type d`; do find $x -name '*.vmx' > /dev/null || echo $x; done; The goal of this is to find the subdirectories that do NOT contain a file with the extension of .vmx Any help would be great! (6 Replies)
Discussion started by: hodnov
6 Replies

6. UNIX for Dummies Questions & Answers

command to find most recent file

Hi, Here is my problem: I try to write a script to find data in a file named "data" for exemple. Let's say I am in the directory /x/y/z, there are several sub-directories in the "z" directory (each sub-directory has a file "data") and I am searching for the word "help". So I use this... (9 Replies)
Discussion started by: StephB
9 Replies

7. Shell Programming and Scripting

find the most recent file containing a certain string

I want to find the most recent file containing ' NORESETLOGS" I'm already here but, how to sort this now in a correct way ? By the way, my version of find does not know about 'fprint' find . -type f -exec grep -i " NORESETLOGS" {} \; -exec ls -l {} \; | grep -vi " RESETLOGS" (5 Replies)
Discussion started by: plelie2
5 Replies

8. Shell Programming and Scripting

Find most recent files in dirs and tar them up?

Hey all.. This should be simple but stoopid here can't get head around it! I have many directories, say 100 each with many files inside. I need a script to traverse through the dirs, find most recent file in each dir and add it to a tar file. I can find the files with something like for... (1 Reply)
Discussion started by: bobdung
1 Replies

9. Shell Programming and Scripting

Find a file in sub-directories.. o/p just the path

Hello All, I am somehow stumped with this ting. 'Find' will sure show me.. but I want only thepath of all the occurences of the file in any of the sub-dirs.. Any help will be sincerely appreciated. thanx! (3 Replies)
Discussion started by: pranavagarwal
3 Replies

10. Programming

to find a file in set of directories

Hi, what is the command in unix to find a file abc.c in a directory which had n number of sub-directories. thanx (3 Replies)
Discussion started by: jazz
3 Replies
Login or Register to Ask a Question