我将要编写一个文件解析器,它在目录树结构中运行。一旦我找到一个特定的叶目录,我想遍历路径所包含的所有目录,并在其中执行一些操作。
假设路径是:/d1/d2/d3
。现在我想按顺序检查文件x
是否分别存在于/d1
、/d1/d2
和/d1/d2/d3
中。
当然,可以这样做:
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
但是有没有更好的解决方案呢?
谢谢。
https://stackoverflow.com/questions/56116103
复制相似问题