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

最有效的写法是什么?


当前回答

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

其他回答

这将创建一个子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

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

没有必要

找到 呜 ...

如果目录名中有空格,则接受的答案将在空格上中断,对于bash/ksh,首选语法是$()。使用GNU find -exec选项+;如

找到……- mycommand +;#这和传递给xargs是一样的

或者使用while循环

find .... | while read -r D
do
    # use variable `D` or whatever variable name you defined instead here
done 

方便的俏皮话

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
for D in `find . -type d`
do
    //Do whatever you need with D
done