在目录树中获取文件深度

Már*_*ner 4 directory bash freebsd centos depth

我正在使用命令find递归浏览目录树,计算文件,大小等...

现在我需要获取每个文件的目录深度.FreeBSDCentOS都有可移植的方法吗?

我知道find能够打印实际的目录深度,但遗憾的是这只适用于CentOS,而不是FreeBSD.

另外 - 我需要保持标准find输出或将目录深度放在输出的开头并从那里切割.

SLe*_*ort 6

您可以计算/路径:

$ find . -type f -exec bash -c 'echo '{}' | grep -o / | wc -l' \;
Run Code Online (Sandbox Code Playgroud)

或者使用文件名:

$ 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
Run Code Online (Sandbox Code Playgroud)