套用一个脚本来子目录
我看过很多次,如果我想对所有子目录执行的东西我想其中的一个运行的东西:
I have read many times that if I want to execute something over all subdirectories I should run something like one of these:
找到。 -name*-exec命令参数{} \\;
找到。型的F -print0 | xargs的-0命令参数
找到。型F | xargs的-I {}命令参数{}参数
的问题是,它与corefunctions工作得很好,但不是当该命令是用户定义的函数或脚本如预期。如何解决呢?
The problem is that it works well with corefunctions, but not as expected when the command is a user-defined function or a script. How to fix it?
所以,我在找的是code的行或者脚本中,我可以取代命令
为 myfunction的
或 myscript.sh
,并从当前目录进入每一个子目录并执行这样的功能或脚本那里,我提供任何参数。
So what I am looking for is a line of code or a script in which I can replace command
for myfunction
or myscript.sh
and it goes to every single subdirectory from current directory and executes such function or script there, with whatever arguments I supply.
解释,我想要的东西给所有子目录很好的工作在文件中。*;做command_myfunction_or_script.sh参数$文件;做
工作过当前目录。
Explaining in another way, I want something to work over all subdirectories as nicely as for file in *; do command_myfunction_or_script.sh arguments $file; done
works over current directory.
这是你给的例子,如:
find . -name '*' -exec command arguments {} \;
不要去每一个子目录,从当前目录并执行命令
在那里,而是执行命令
从当前目录的路径由find作为参数中列出的每个文件
Don't go to every single subdirectory from current directory and execute command
there, but rather execute command
from the current directory with the path to each file listed by the find as an argument.
如果你想要的是真正改变目录并执行一个脚本,你可以尝试这样的事:
If what you want is to actually change directory and execute a script, you could try something like this:
STDIR=$PWD; IFS=$'\n'; for dir in $(find . -type d); do cd $dir; /path/to/command; cd $STDIR; done; unset IFS
下面将当前目录保存到 STDIR
和bash的内部字段分隔符设置为换行符这样的名字将不会在空间分割。然后,每个目录(型ð
)的找到
的回报,我们 CD
到该目录下,执行命令(这里使用的完整路径,如更改目录将打破相对路径),然后CD回到起始目录。
Here the current directory is saved to STDIR
and the bash Internal Field Separator is set to a newline so names won't split on spaces. Then for each directory (-type d
) that find
returns, we cd
to that directory, execute the command (using the full path here as changing directories will break a relative path) and then cd back to the starting directory.