bash 递归读取文件夹并对每个文件夹执行命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1333813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recursively read folders and executes command on each of them
提问by Jinhua
I am trying to recurse into folders and then run commands on them, using bash script. Any suggestions?
我正在尝试递归到文件夹中,然后使用 bash 脚本对它们运行命令。有什么建议?
回答by Pascal MARTIN
If you want to recurse into directories, executing a command on each file found in those, I would use the find
command, instead of writing anything using shell-script, I think.
如果你想递归到目录中,在这些目录中找到的每个文件上执行一个命令,我会使用该find
命令,而不是使用 shell-script 编写任何东西,我想。
That command can receive lots of parameters, like type
to filter the types of files returned, or exec
to execute a command on each result.
该命令可以接收大量参数,例如type
过滤返回的文件类型,或exec
对每个结果执行命令。
For instance, to find directories that are under the one I'm currently in :
例如,要查找我当前所在的目录下的目录:
find . -type d -exec echo "Hello, '{}'" \;
Which will get me somehthing like :
这会让我得到类似的东西:
Hello, '.'
Hello, './.libs'
Hello, './include'
Hello, './autom4te.cache'
Hello, './build'
Hello, './modules'
Same to find the files under the current directory :
同样查找当前目录下的文件:
find . -type f -exec echo "Hello, '{}'" \;
which will get me something like this :
这会让我得到这样的东西:
Hello, './config.guess'
Hello, './config.sub'
Hello, './.libs/memcache_session.o'
Hello, './.libs/memcache_standard_hash.o'
Hello, './.libs/memcache_consistent_hash.o'
Hello, './.libs/memcache.so'
Hello, './.libs/memcache.lai'
Hello, './.libs/memcache.o'
Hello, './.libs/memcache_queue.o'
Hello, './install-sh'
Hello, './config.h.in'
Hello, './php_memcache.h'
...
Some would say "it's not shell"... But why re-invent the wheel ?
(And, in a way, it is shell ^^ )
有人会说“它不是壳”……但为什么要重新发明轮子?
(而且,在某种程度上,它是外壳 ^^ )
For more informations, you can take a look at :
有关更多信息,您可以查看:
man find
- lots of tutorials found with google, like, for instance, Unix Find Command Tutorial
man find
- 谷歌找到了很多教程,例如,Unix Find Command Tutorial
回答by Trey Blancher
Bash 4.0 introduced the globstar option, so a construct like:
Bash 4.0 引入了 globstar 选项,因此构造如下:
for f in mydir/**/*
do
# operations here
done
...will act recursively on whatever lands in $f. Turn this on with "shopt -s globstar", otherwise the ** will be treated as a singular *.
...将递归地作用于 $f 中的任何土地。使用“shopt -s globstar”打开它,否则**将被视为单数*。
Found this gem today at http://www.linuxjournal.com/content/globstar-new-bash-globbing-option, after being inspired by the zsh construct (which I have enabled by default).
今天在http://www.linuxjournal.com/content/globstar-new-bash-globbing-option找到了这个 gem ,在受到 zsh 构造(我默认启用)的启发之后。
回答by dreynold
Something like this should achieve your goal:
这样的事情应该可以实现你的目标:
function RecurseDirs
{
oldIFS=$IFS
IFS=$'\n'
for f in "$@"
do
-----your activity here-----
if [[ -d "${f}" ]]; then
cd "${f}"
RecurseDirs $(ls -1 ".")
cd ..
fi
done
IFS=$oldIFS
}
回答by Paolo Santos
Some basic shells miss commands like 'find' and some of their commands don't support recursivity. In that case you can use this script to run the desired command in all subdirs in the tree:
一些基本的 shell 缺少像“find”这样的命令,并且它们的一些命令不支持递归。在这种情况下,您可以使用此脚本在树中的所有子目录中运行所需的命令:
CDIR=$(pwd)
for i in $(ls -R | grep :); do
DIR=${i%:} # Strip ':'
cd $DIR
# Your command
cd $CDIR
done
If you name the above "recurse.sh" then use:
如果您将上述名称命名为“recurse.sh”,则使用:
./recurse.sh <command>
Example (change the owner/group to 'root' of all files in the tree):
示例(将树中所有文件的所有者/组更改为“root”):
./recurse.sh "chown 0:0 *"
回答by Simon Groenewolt
Have a look at the find
command and check the switches -type
(use d to specify directory) and -exec
(to specify a command to execute).
查看find
命令并检查开关-type
(使用 d 指定目录)和-exec
(指定要执行的命令)。
回答by Michael Lloyd Lee mlk
Sorry I don't understand what you are asking. The best I can guess with your question is
对不起,我不明白你在问什么。你的问题我能猜到的最好的是
find -type d -exec scriptname.sh \{\} \;
回答by ire_and_curses
For most recursive file operations, you want to use find
, as the other answers explain.
对于大多数递归文件操作,您希望使用find
,正如其他答案所解释的那样。
There is an example of a recursive bash script included in the bash-docpackage. If you've got those examples installed, it will be at /usr/share/doc/bash/examples/functions/recurse
(on Debian).
bash-doc包中有一个递归 bash 脚本示例。如果您安装了这些示例,它将位于/usr/share/doc/bash/examples/functions/recurse
(在 Debian 上)。