Search for files in multiple directories


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search for files in multiple directories
# 1  
Old 10-25-2006
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 05:03 PM..
# 2  
Old 10-25-2006
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

# 3  
Old 10-25-2006
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?
# 4  
Old 10-25-2006
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 07:09 PM..
# 5  
Old 10-26-2006
Thanks for all your help. I got an easy command:
$ find 2006*/ -name ftp_server*
# 6  
Old 10-26-2006
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.
# 7  
Old 10-26-2006
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. Shell Programming and Scripting

Command to Search a FILE for a STRING in multiple DIRECTORIES

Hi, Can someone please help me with a Command to Search a FILE for a STRING in multiple DIRECTORIES. I am searching for the VIP in HTTPD.CONF in multiple httpd instances. I am using find ./ -name "httpd.conf" | xargs grep 10.22.0.141 cut -d: -f3- | cut -d ' ' -f4 | sort | uniq -c ... (1 Reply)
Discussion started by: crosairs
1 Replies

3. Shell Programming and Scripting

Recursive search for files and copy to new directories

So I have extremely limited experience with shell scripting and I was hoping someone could point out a few commands I need to use in order to pull this off with a shell script like BASH or whatnot (this is on OS X). I need to search out for filenames with account numbers in the name itself... (3 Replies)
Discussion started by: flyawaymike
3 Replies

4. Shell Programming and Scripting

perform 3 awk commands to multiple files in multiple directories

Hi, I have a directory /home/datasets/ which contains a bunch (720) of subdirectories called hour_1/ hour_2/ etc..etc.. in each of these there is a single text file called (hour_1.txt in hour_1/ , hour_2.txt for hour_2/ etc..etc..) and i would like to do some text processing in them. Each of... (20 Replies)
Discussion started by: amarn
20 Replies

5. Shell Programming and Scripting

How to access files from different directories and to perform search action in those files?

Hi, I want to access files from different directories (for example: /home/dir1/file1 , /home/dir2/file2 ...) Like this i have to access these files(file1, file2...). (3 Replies)
Discussion started by: bangarukannan
3 Replies

6. UNIX for Advanced & Expert Users

word search in multiple directories

need a Command in UNIX which can find out a word from files in multiple directories e.g. /home contains multiple directories /home/d1 /home/d2 . . . . /home/dn under d1,d2...dn contains multiple files. I need to search a specific word in a files under these multiple... (1 Reply)
Discussion started by: jagkoth
1 Replies

7. Shell Programming and Scripting

FTP multiple files from multiple directories

I have multiple files that starts as TRADE_LOG spread across multiple folders in the given structure.. ./dir1/1/TRADE_LOG*.gz ./dir2/10/TRADE_LOG*.gz ./dir11/12/TRADE_LOG*.gz ./dir12/13/TRADE_LOG*.gz when I do ftp uisng mput from the "." dir I am getting the below given error mput... (1 Reply)
Discussion started by: prasperl
1 Replies

8. UNIX for Dummies Questions & Answers

Loop through Sub Directories and search for set of files

I have the below directory in unix environment /home/bkup/daily: ls -lrt drwxrwx--x 2 user user 256 Jan 12 18:21 20110112/ drwxrwx--x 2 user user 256 Jan 13 17:06 20110113/ drwxrwx--x 2 user user 256 Jan 14 16:44 20110114/ drwxrwx--x 2 user user ... (2 Replies)
Discussion started by: prasannarajesh
2 Replies

9. UNIX for Dummies Questions & Answers

Multiple search in multiple directories

Hi, I am facing the below problem I have a list of number to be searched(around 1000) files resideing in multiple directories as i dont know where it resides in. for example search string is like (abc)123456 please help. very urgent Thanks a lot Uma (3 Replies)
Discussion started by: umapearl
3 Replies

10. Shell Programming and Scripting

Search directories for files with zero sizes to delete

Hi, I am trying to write a script that will use ls on a directory and list the files one at a time and their size. If the size is 0 i want it to ask me if I want to delete it (yes or no). If I say yes, I want it to delete but it won't know what the file name is just from running from the script.... (2 Replies)
Discussion started by: akeenabawa
2 Replies
Login or Register to Ask a Question