【发布时间】:2019-09-30 15:15:33
【问题描述】:
我要编写一个在目录树结构中运行的文件解析器。一旦我找到一个特定的叶目录,我想遍历该路径所包含的所有目录并在其中执行一些操作。
假设路径是:/d1/d2/d3。
现在我想检查/d1、/d1/d2 和/d1/d2/d3 中是否存在文件x,并按此顺序。
当然,也可以这样做:
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
但是有没有更优雅的解决方案呢?
谢谢。
【问题讨论】:
标签: bash shell find directory-traversal path-traversal