Run breath-first-search find


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Run breath-first-search find
# 1  
Old 07-23-2010
CPU & Memory Run breath-first-search find

I am trying to look for a certain file in multiple folders. When I hit the file, I want to stop going to subdirectory.
For example:
Code:
/foo/.target
/bar/buz/.target
/foo/bar/.target

I want only
Code:
/foo/.target
/bar/buz/.target

Any ideas howSmilie?

Last edited by Franklin52; 07-23-2010 at 03:52 PM.. Reason: Please use code tags
# 2  
Old 07-23-2010
Could you be more specific? Which file/pattern exactly you're searching for?
# 3  
Old 07-23-2010
I'm going to put a bunch of .indexMe file in directories. So my tool can know what directory to index. If parent and child directory both have .indexMe, only the parent is needed, because all subdirectories will be indexed.

My problem is how to list the directories that contain .indexMe
# 4  
Old 07-23-2010
You could try something like this:

Code:
perl -MFile::Find -le'
  find { 
    wanted => sub {
      $File::Find::prune = 1 and return 
        if  @dirs{$File::Find::dir =~ m|(.*)/[^/]*$|};
      print $File::Find::name and $dirs{$File::Find::dir} = 1
        if $_ eq ".indexMe";
	  }
	}, shift	
  ' .


Last edited by radoulov; 07-23-2010 at 06:49 PM..
# 5  
Old 07-23-2010
The find utility does not have a breath first option.

Here is a Python script which I picked up from somewhere some time ago which does a BF search
Code:
#!/bin/python

import os
import sys

rootnode = sys.argv[1]
fifo = [rootnode]

while fifo:
    file = fifo.pop(0)
    print(file)
    if os.path.isdir(file):
        fifo.extend(os.path.join(file,x) for x in os.listdir(file))

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search: find current line, then search back

Hello. I want to find a line that has "new = 0" in it, then search back based on field $4 () in the current line, and find the first line that has field $4 and "last fetch" Grep or Awk preferred. Here is what the data looks like: 2013-12-12 12:10:30,117 TRACE last fetch: Thu Dec 12... (7 Replies)
Discussion started by: JimBurns
7 Replies

2. UNIX for Dummies Questions & Answers

find Search - Find files not matching a pattern

Hello all, this is my first and probably not my last question around here. I do hope you can help or at least point me in the right direction. My question is as follows, I need to find files and possible folders which are not owner = AAA group = BBB with a said location and all sub folders ... (7 Replies)
Discussion started by: kilobyter
7 Replies

3. Shell Programming and Scripting

Code to search for a phrase and run a command

hi All, Am newbie to unix and would require your help to complete this task. Condition: I have a server start up script and I would like to append the code in such a way that it searches for a phrase "process completed" in taillog when server starts up and also should run another script... (9 Replies)
Discussion started by: fop4658
9 Replies

4. UNIX for Advanced & Expert Users

Find the process was run using nohup or ksh.

Hi, I want to write a script which should be run only on foreground. Is there any way that the script can check itself whether it was run using nohup or ksh and if the user runs the script using nohup then it should prompt the user to run it using ksh? If (The user triggers the script using... (4 Replies)
Discussion started by: RRVARMA
4 Replies

5. UNIX for Dummies Questions & Answers

find command and run it

Hi guys, how would I find out if the command/script exists on the system ( HP-UX, Linux ) and if it does run it so it would display the output? lets say I can do which any-command and if it finds any-command I want to run it ... I can use echo $? to see what the which command... (2 Replies)
Discussion started by: mirusko
2 Replies

6. Shell Programming and Scripting

How to find the time taken for a script to run?

I have just written a Csh script and i want to find out how long does the script take to complete executing. Is there any codes which i can put into my script to capture its run time ? (9 Replies)
Discussion started by: Raynon
9 Replies

7. UNIX for Dummies Questions & Answers

Find command to run only in the base directory

Hi. I'm trying to get my find command to only search in the directory i tell it to, but i don't want it to search in the sub directories as well... For example, i have a /data/files/ and /data/files/old I want to search for all .sav files within /data/files but i don't want it to drill... (4 Replies)
Discussion started by: Stephan
4 Replies

8. Filesystems, Disks and Memory

Find run-level in solaris 8.

When the solaris 8 have come up, which command can find out the current run-level? thanks. (2 Replies)
Discussion started by: nianzhe
2 Replies

9. UNIX Desktop Questions & Answers

Where can I find a Unix OS that will run on a 386 processor?

Hey, can anyone recommend a URL (or website) where I can download a basic (or old) UNIX OS that will run on an ancient laptop that has a 386 processor, 4MB of RAM and a 40MB harddrive? Your help and suggestions are much appreciated! :confused: (4 Replies)
Discussion started by: methudrez
4 Replies
Login or Register to Ask a Question