shell - Avoid going into subdirectories when "find" has a hit -


i trying file in multiple folders. when hit file, want stop going subdirectories. example:

/foo/.target   /bar/buz/.target   /foo/bar/.target 

i want first two:

/foo/.target   /bar/buz/.target 

your requirements not clear. understand them as: “wanted” files inside directory tree; if directory directly contains @ least 1 match, print them, otherwise recurse directory.

i can't think of pure find solution. write awk or perl script parse output of find.

here's shell script think you're looking for. warning: i've minimally tested it.

#!/bin/sh  ## return 0 if $1 matching file, 1 otherwise. ## note $1 full path file. wanted () {   case ${1##*/} in     .target) true;;   esac }  ## recurse directory $1. print wanted files in directory. ## if there no wanted file, recurse each subdirectory in turn. traverse () {   found=0   x in "$1"/.* "$1"/*;     if [ "$x" = "$1/." ] || [ "$x" = "$1/.." ];       continue # skip '.' , '..' entries     fi     if ! [ -e "$x" ];       continue # skip spurious '.*', '*' non-matching patterns     fi     if wanted "$x";       printf '%s\n' "$x"       found=$(($found+1))     fi   done   if [ $found -eq 0 ]; # no match here, recurse     x in "$1"/.*/ "$1"/*/;       x=${x%/}       if [ "$x" = "$1/." ] || [ "$x" = "$1/.." ];         continue       fi       if [ -d "$x" ]; # actual subdirs, not symlinks or '.*' or '*'         found_stack=$found:$found_stack # no lexical scoping in sh         traverse "${x%/}"         found=${found_stack%%:*}         found_stack=${found_stack#*:}       fi     done   fi }  found_stack=: x;   if wanted "$x";     printf '%s\n' "$x"   else     traverse "$x"   fi done 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -