2

I have a command in a Korn script which is like as written below,

find <path1> <path2> <path3> -name filename

After this, it stores the output in a txt file. Using this txt file it does some operations.

Is there any way using which if the required file is found in path1, then the find command would not search in path2 and path3 and would stop?

I tried using -quit option, it it seems -quit option does not work with the IBM AIX version of find. There is no locate command in the system. Neither do I have permissions to install.

Using the pipe option with head -1 would do the job but if there is no other file in path2 and path3 and the file is only in path1, then it would still go the whole mile and search all the paths.

1 Answers1

1

The way I would approach this is to loop over the directories, running find in each one until it matched.

for dir in path1 path2 path3
do
    echo "Searching $dir" >&2
    match=$(find "$dir" -type f -name "filename" 2>/dev/null)
    test -n "$match" && echo "$match" && break
done

You can remove the two echo statements if you want a silent solution. On exit from the loop, if $match is not empty then it contains the full pathname and $dir contains the top-level directory under which it can be found.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287