【问题标题】:Get file depth in directory tree获取目录树中的文件深度
【发布时间】:2016-07-15 23:10:41
【问题描述】:
我正在使用命令find 递归浏览目录树、计算文件、大小等...
现在我需要获取每个文件的目录深度。
FreeBSD 和 CentOS 是否有任何可移植的方式?
我知道find 能够打印实际的目录深度,但遗憾的是这只适用于 CentOS,不适用于 FreeBSD。
另外 - 我需要保持标准 find 输出或将目录深度放在输出的开头并从那里剪切。
【问题讨论】:
标签:
bash
directory
centos
freebsd
depth
【解决方案1】:
您可以在路径中计算/:
$ find . -type f -exec bash -c 'echo '{}' | grep -o / | wc -l' \;
或者用文件名:
$ mkdir -p one/two/three four/five && touch file one/two/file one/two/three/file
$ find . -type f -exec bash -c 'echo -n '{}' :; echo '{}' | grep -o / | wc -l' \;
./file :1
./one/two/file :3
./one/two/three/file :4
【解决方案2】:
试试这个:
find . -type d -exec bash -c 'echo $(tr -cd / <<< "$1"|wc -c):$1' -- {} \; | sort -n | tail -n 1 | awk -F: '{print $1, $2}'