LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help searching through a list of directories (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-searching-through-a-list-of-directories-622174/)

Twilight 02-19-2008 02:58 AM

Need help searching through a list of directories
 
Hi everyone! :0)

I am trying to find a specific file in a 100+ list of directories. The script needs to cycle through these dirs, scanning every folder for a file.
I know how to do this for a single directory, but I am not sure what to do with a list.
Secondly, I need to search for a certain text, in all the files with a certain extension in these directories..

I would truly appreciate your help!

SlowCoder 02-19-2008 08:50 PM

Research the "find" and "grep" commands. I think you'll have all you need with them.

Twilight 02-20-2008 02:26 AM

Right.

It would be

Code:

find /media/disk -mtime -5 -print | grep .mp3
to search for an mp3 file, modified / created within the last 5 days. (I'm not searching for mp3s or last modified, this is just an example).

But how would I do this for a list of dirs? I.e. exact, specific list of dirs?

It would seem I'd need to do a loop in bash, but every example I've looked up doesn't deal with a list, but rather general search....

SlowCoder, thank you for your reply, though.



P.S. how come I can't upload an avatar?



_______________

before submitting, I've found a couple of things, and modified them accordingly

Code:

cat example.txt | while read line ; do find "$line" -mtime -5 -print ; done
It almost works!
If I plug-in do echo, I see the output

Code:

find /media/disk/dir
 -mtime -5 -print

if for $line I substitute actual path, it works

But still.. it needs to remove newline breaks

like so

Code:

sed '/^.*$/N;s/\n//g'
or so
Code:

sed '{:q;N;s/\n//g;t q}'
or maybe even so
Code:

cat example.txt | perl -p -e 's/s+$/ /g'
I just need to plug this in.... somewhere


I still can't figure out how to search inside files...

darn it why is this so hard. simple search operations shouldn't be an exercise in futility...


plz oh magnificent gurus, shine your light of knowledge upon me! :)

bigrigdriver 02-20-2008 04:11 AM

Go the Advanced Bash-scripting Guide, and look up 'redirection'. Specifically, redirecting input from a file instead of to a file.

Something like:

for i in < list_of_directories.txt; do find $i -mtime -5 -print | grep .mp3; done

Twilight 02-21-2008 04:43 AM

Code:

for i in < list_of_directories.txt; do find $i -mtime -5 -print | grep .mp3; done
for some reason this goes into infinite loop, and does it only for 1 dir

If I remove -mtime -5 and write to file, I get this

Code:

/home/example/
 -print
/home/example/
 -print
/home/example/
 -print
/home/example/
 -print
/home/example/
 -print
/home/example/
 -print
/home/example/
 -print
/home/example/
 -print

the newline break problem still persists..

while this is supposed to work as well
Code:

while read line < list.txt ; do find “$line” -mtime -5 -print ; done
it went into an infinite loop too, doesn't produce anything


I did read the simple man http://www.tuxfiles.org/linuxhelp/iodirection.html, its all quite logical..

Advanced Bash-Scripting Guide
http://tldp.org/LDP/abs/html/io-redi...tml#IOREDIRREF
wasn't as clear


But this code is supposed to work now, though!

:(

bigrigdriver 02-21-2008 09:02 AM

Let's back up a bit. How do you generate the list of directories to search through? What command do you use, and what does the output look like?


All times are GMT -5. The time now is 06:43 AM.