LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-03-2012, 03:08 AM   #1
akeka
Member
 
Registered: May 2012
Posts: 39

Rep: Reputation: Disabled
Find hundreds of file on many directory


Hi,

I want to search hundreds of files on many directory

Is there any way that I can do this on single command ?

Thanks
 
Old 10-03-2012, 03:13 AM   #2
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,159

Rep: Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364
what is the goal? have you tried ls -lR or the command: find ?
 
Old 10-03-2012, 03:13 AM   #3
Didier Spaier
Senior Member
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slackware{,64}-{14.1,current} on a Lenovo Thinkpad W520
Posts: 4,678

Rep: Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240
The "find" command is the usual candidate for that.

Please tell us some more about your search criteria and the scope of the search if you need a more customized answer.
 
Old 10-03-2012, 03:17 AM   #4
akeka
Member
 
Registered: May 2012
Posts: 39

Original Poster
Rep: Reputation: Disabled
Hi all,

thanks for the reply

The goal is simple, there's a bunch (hundreds) of files that I need to search on 9 path

I know I can use find with -name file1 -o -name file2

I can afford it if it's only less than 5 files, but hundreds ?

Maybe some combination of find, awk, xargs or something can help me, but I don't know how

Thanks

Last edited by akeka; 10-03-2012 at 03:18 AM.
 
Old 10-03-2012, 03:22 AM   #5
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,159

Rep: Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364
I still do not understand: do you have a list of filenames and you want to locate them, or ????
 
Old 10-03-2012, 03:33 AM   #6
akeka
Member
 
Registered: May 2012
Posts: 39

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
I still do not understand: do you have a list of filenames and you want to locate them, or ????
Yes

Maybe I can explain more

Let say I have file list_of_files.txt which the content are all files that I need to search

Quote:
file1
123
234fnk
dseouo
.
.
.
The files doesn't have similar pattern, completely random

Thanks
 
Old 10-03-2012, 03:37 AM   #7
Didier Spaier
Senior Member
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slackware{,64}-{14.1,current} on a Lenovo Thinkpad W520
Posts: 4,678

Rep: Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240
Code:
for i in $(cat list_of_files.txt); do locate $i; done
I assume that your locate database is up to date, else run "updatedb" first.

Last edited by Didier Spaier; 10-03-2012 at 03:39 AM.
 
Old 10-03-2012, 03:42 AM   #8
akeka
Member
 
Registered: May 2012
Posts: 39

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Didier Spaier View Post
Code:
for i in $(cat list_of_files.txt); do locate $i; done
I assume that your locate database is up to date, else run "updatedb" first.
Hi,

Thank you for the reply

How do I define the path to be search ?
 
Old 10-03-2012, 03:49 AM   #9
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,159

Rep: Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364
locate has a database and will search inside. Therefore if this database is not uptodate it will not find those files. (you can scan your environment and collect info using updatedb). If you want to look for special directories you need to grep the result.
 
Old 10-03-2012, 04:38 AM   #10
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
locate will be efficient because it will search only its own database, not the file system

But if you need other or more limited directories to be searched, you could do it directly - but if you are searching 1000s of subdirectories you may want to think about a depth-first versus breadth-first approach - by depth-first I mean search for one of the possible file names in all directories, then the next:

Code:
for fn in $(cat list_of_files.txt); do find /dir1 /dir2 /dir3 -name $fn; done
or ("breadth-first") you may want to search for all possible file names in the top directory, then in the next, and so on:

Code:
find /dir1 /dir2 /dir3 -type d -exec bash -c 'for fn in $(cat list_of_files.txt); do find "{}" -maxdepth 1 -name "$fn"; done' \;
The second version looks more complex but may be more efficient as the file name matching search is only done once in each directory (and the directory tree is only walked once).

That said, if you're only checking the name, it should be fast enough anyway as find won't have to stat anything.

(In the above /dir1 /dir2 /dir3 is the list of directories you want to search in, and I assume like Didier that you've got a list_of_files.txt. If some of the possible file names contain spaces, they'll need to be quoted.)
 
Old 10-03-2012, 06:30 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,948

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
Note:
Code:
never do this: for file in $(cat filelist); do ... done
instead:       cat filelist | while read file; do ... done
even better:   while read file; do ... done <filelist
Reasons:
1. 'filelist' might be large
2. filenames might contain spaces

Last edited by NevemTeve; 10-03-2012 at 06:32 AM.
 
1 members found this post helpful.
Old 10-03-2012, 01:19 PM   #12
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Well, honestly, I would use find and then grep.

Code:
bash-4.2$ find /usr -type f > usr.txt
bash-4.2$ cat grepfile.txt 
/usr/local/bin/geany$
/usr/local/bin/7z$
bash-4.2$ grep -f grepfile.txt usr.txt
/usr/local/bin/7z
/usr/local/bin/geany
You can of course use regex in the grep file.
 
Old 10-03-2012, 01:40 PM   #13
dugan
Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 5,002

Rep: Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560Reputation: 1560
Do an updatedb once, and then run locate once for each file in the list.

EDIT: In other words, what Didier Spaier said. You don't need to "define the path to be searched" (and this should be apparent if you know how updatedb and locate work).

Last edited by dugan; 10-03-2012 at 03:40 PM.
 
Old 10-03-2012, 05:57 PM   #14
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by dugan View Post
(and this should be apparent if you know how updatedb and locate work)
Which is that, as controlled by /etc/updatedb.conf, they search most of the standard paths - which may return many false positives, depending on the kind of names involved - but do not search all paths. In particular, a sensible administrator will have configured them not to search large removable or network drives.

So it's apparent, if you know how updatedb and locate work, that you might need to define the set of paths to be searched.
 
Old 10-03-2012, 10:27 PM   #15
ip_address
Member
 
Registered: Apr 2012
Distribution: RedHat
Posts: 42

Rep: Reputation: 2
you can also try this ..

Code:
while read line; do echo "Finding file $line .."; find / -name "$line" 2>/dev/null; done < list_of_files.txt
 
  


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
[SOLVED] find out chmod of file or directory ggalan Linux - Newbie 1 01-09-2012 11:11 AM
LXer: How To Find Servers and Hundreds of Online Players for Popular Linux FPS Game Urban Terror LXer Syndicated Linux News 0 10-14-2010 08:21 PM
a2ps: cannot find file `c0419bt_.afm': No such file or directory rg3 Slackware 5 08-02-2009 04:11 PM
find pattern in any file in directory sancho1980 Linux - Newbie 4 12-19-2006 09:14 AM
Hundreds of hidden files in home directory doobers Linux - Newbie 3 02-14-2005 03:57 PM


All times are GMT -5. The time now is 11:39 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