LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-19-2008, 03:58 AM   #1
Twilight
LQ Newbie
 
Registered: Feb 2008
Posts: 3

Rep: Reputation: 0
Smile 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!
 
Old 02-19-2008, 09:50 PM   #2
SlowCoder
Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Fedora (Desktop), CentOS (Server), Knoppix (Diags)
Posts: 934

Rep: Reputation: 38
Research the "find" and "grep" commands. I think you'll have all you need with them.
 
Old 02-20-2008, 03:26 AM   #3
Twilight
LQ Newbie
 
Registered: Feb 2008
Posts: 3

Original Poster
Rep: Reputation: 0
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!
 
Old 02-20-2008, 05:11 AM   #4
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Jessie 8.4
Posts: 5,873

Rep: Reputation: 348Reputation: 348Reputation: 348Reputation: 348
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
 
Old 02-21-2008, 05:43 AM   #5
Twilight
LQ Newbie
 
Registered: Feb 2008
Posts: 3

Original Poster
Rep: Reputation: 0
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!

 
Old 02-21-2008, 10:02 AM   #6
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Jessie 8.4
Posts: 5,873

Rep: Reputation: 348Reputation: 348Reputation: 348Reputation: 348
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?
 
  


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
Searching multiple directories and sub directories for a file jeep99899 Linux - Newbie 2 10-13-2005 01:23 PM
Searching all directories HadesThunder Linux - Software 2 04-29-2004 08:37 AM
list sub-directories only CowboyJ Linux - Newbie 1 12-05-2003 12:42 AM
Asking LS for a list of directories only gary knott Linux - Newbie 1 11-12-2003 06:49 PM
list of directories greg108 Linux - Newbie 5 08-08-2003 04:11 AM


All times are GMT -5. The time now is 04:08 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration