我正在编写一个脚本,需要在特定文件夹的每个子目录中执行一个操作。

最有效的写法是什么?


当前回答

for D in `find . -type d`
do
    //Do whatever you need with D
done

其他回答

这将创建一个子shell(这意味着当while循环退出时,变量值将丢失):

find . -type d | while read -r dir
do
    something
done

这不会:

while read -r dir
do
    something
done < <(find . -type d)

如果目录名中有空格,这两种方法都可以工作。

最简单的非递归方法是:

for d in */; do
    echo "$d"
done

末尾的/表示只使用目录。

没有必要

找到 呜 ...

找到。-type d -print0 | xargs -0 -n 1 my_command

方便的俏皮话

for D in *; do echo "$D"; done
for D in *; do find "$D" -type d; done ### Option A

find * -type d ### Option B

选项A适用于中间有空格的文件夹。而且,通常更快,因为它不会将文件夹名称中的每个单词作为单独的实体打印。

# Option A
$ time for D in ./big_dir/*; do find "$D" -type d > /dev/null; done
real    0m0.327s
user    0m0.084s
sys     0m0.236s

# Option B
$ time for D in `find ./big_dir/* -type d`; do echo "$D" > /dev/null; done
real    0m0.787s
user    0m0.484s
sys     0m0.308s

使用find命令。

在GNU find中,你可以使用-execdir参数:

find . -type d -execdir realpath "{}" ';'

或者使用-exec parameter:

find . -type d -exec sh -c 'cd -P "$0" && pwd -P' {} \;

或者使用xargs命令:

find . -type d -print0 | xargs -0 -L1 sh -c 'cd "$0" && pwd && echo Do stuff'

或者使用for循环:

for d in */; { echo "$d"; }

对于递归,请尝试扩展通配符(**/)(通过:shopt -s extglob启用)。


有关更多示例,请参见:如何进入每个目录并执行命令?在所以