arik
arik

Reputation: 1

BASH - search for file with a specifc pattern that exist in directories with specific pattern -RECURSIVELY

i asked to write script that searching recursively for files that match to the pattern ".foo" BUT the only files which exist in directories that match to the same pattern ".foo". i tried: script name : search_for_foo

function foo_search {
 while read line; do
    echo "$line”   
 done < "$1"
 }
 for file in ${*:1}; do
  if [[ $file == *.foo* ]]; then
    if [[ -f "$file" ]]; then
       foo_search $file
       fi
       if [[ -d "$file" ]]; then
       search_for_foo $file/*
       fi
    fi
    done <"$1"

it has to work this way : ./search_for_foo --some_file--

thanks in advance

Upvotes: 0

Views: 34

Answers (2)

P....
P....

Reputation: 18371

location=$1
pattern=$2

dir=$(find $location -type d -name "*.$pattern")

ls -lrt $dir|grep ^- |awk '{print $NF}'

run like ./script.sh /location/directory/ pattern

Upvotes: 0

Michael Vehrs
Michael Vehrs

Reputation: 3363

Do you mean something like

find -path "*foo/*/foo"

Upvotes: 1

Related Questions