The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Multiple search in multiple files maxvirrozeito Shell Programming and Scripting 2 12-13-2007 09:32 AM
cd to multiple directories and gzipping files within sunsysadm2003 Shell Programming and Scripting 0 10-30-2007 01:50 PM
get files from multiple directories using FTP amit1209 Shell Programming and Scripting 2 09-26-2007 06:39 AM
copy multiple files in different directories ken2834 UNIX for Dummies Questions & Answers 3 03-25-2007 10:35 AM
FTP multiple files to different directories abrd600 Shell Programming and Scripting 12 09-23-2004 04:56 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-25-2006
Registered User
 

Join Date: Oct 2006
Location: NewYork
Posts: 21
Search for files in multiple directories

I want to search for a file pattern in more than one directory. How I need to do that?

Here is the scenario:

I am having a directory structure like the following:
/log
...../20051001
..........ftp_server_20051001.log
..........ftp_down_server.log
..........ftp_up_server.log
...../20051101
..........ftp_server_20051101.log
...../20051201
..........ftp_down_server.log
..........ftp_up_server.log
...../20060101
..........ftp_down_server.log
..........ftp_up_server.log
...../20060201
..........ftp_server_20060201.log
...../20060301
..........ftp_down_server.log
..........ftp_up_server.log
..........ftp_server_20060201.log
...../20060401
..........ftp_down_server.log
..........ftp_up_server.log

I want to find out all the logs that start with "ftp_server" in the
year 2006 from the log directory.
Can some one help me with the command?

I used to try like: $ find . -name 2006*/ftp_server*. It listed only one
file information and quits with some error. I am looking for all the files

Thanks,
Ravi

Last edited by ravikirankethe; 10-25-2006 at 01:03 PM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-25-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 923
The -name option specifies the filename, not the path.

If there's a small number of them you can do this with shell globbing:
Code:
ls 2006*/ftp_server*
Otherwise, you might do this with find and grep:
Code:
 find . -iname 'ftp_server*' | grep 2006
Reply With Quote
  #3 (permalink)  
Old 10-25-2006
Registered User
 

Join Date: Oct 2006
Location: NewYork
Posts: 21
I think the command need to be like this:
find . -name 'ftp_server*' | grep 2006

This particular command is having the over head of pulling the
file information from all the folders and then it greps for only 2006.

The overhead here is searching all the files rather I want to narrow
the search to search only in the 2006* folders.

Can we achieve this?
Reply With Quote
  #4 (permalink)  
Old 10-25-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 923
find has options relating to this, but they're all depreciated for security reasons. We can do this, sure, but probably not in one command. Here we have 'find' locating directories beginning with 2006, and bash locating files within them beginning with ftp_server. The 2> /dev/null redirects error messages to /dev/null for directories with no ftp_server* file in them.

Code:
find . -mindepth 1 -maxdepth 1 -type d -name '2006*' |
        while read DIR
        do
                ls ${DIR}/ftp_server*
        done 2> /dev/null

Last edited by Corona688; 10-25-2006 at 03:09 PM.
Reply With Quote
  #5 (permalink)  
Old 10-26-2006
Registered User
 

Join Date: Oct 2006
Location: NewYork
Posts: 21
Thanks for all your help. I got an easy command:
$ find 2006*/ -name ftp_server*
Reply With Quote
  #6 (permalink)  
Old 10-26-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 923
If that will work, then so will the ls 2006*/ftp_server* I suggested in the first place. Your solution has the same caveat -- too many directories will exceed the maximum commandline length of your shell.
Reply With Quote
  #7 (permalink)  
Old 10-26-2006
Registered User
 

Join Date: Oct 2006
Location: NewYork
Posts: 21
Yeah thats right.. we can use "ls" also to get the same results. Infact, I think "ls" is the best to use as we have more options in that.

Ex:
$ ls -l 2006*/ftp_server*
$ ls -l 2006*/*NCPAY_MAR*.enc
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:49 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0