I'm about to program a file parser which is operating in a directory tree structure. Once I find a specific leaf directory I want to go through all directories which the path consists of and do some operations within them.
Let's say the path is: /d1/d2/d3
.
Now I want to check whether or not a file x
is present in /d1
, /d1/d2
and /d1/d2/d3
respectively and in that order.
Of course, one could do something like this:
fields=`find $base_dir -name "leaf_directory" | grep -o "/" | wc -l`
[[ $fields > 0 ]] || exit 1
for (( i=1; i <= $fields + 1; i++ )) do
current_dir="`find $base_dir -name "leaf_directory" | cut -d "/" -f $i`"
source_path="$source_path$current_dir/"
if [ -f $source_path$file ]; then
# do sth.
fi
done
But is there any more elegant solution for this?
Thank you.