递归读取文件夹并在每个文件夹上执行命令 [英] Recursively read folders and executes command on each of them

查看:74
本文介绍了递归读取文件夹并在每个文件夹上执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图递归到文件夹,然后使用bash脚本在文件夹上运行命令.有什么建议吗?

I am trying to recurse into folders and then run commands on them, using bash script. Any suggestions?

推荐答案

如果要递归到目录中,请在其中找到的每个文件上执行命令,我将使用find命令,而不是使用shell编写任何内容-脚本,我想.

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.

该命令可以接收很多参数,例如type可以过滤返回的文件类型,或者exec可以对每个结果执行命令.

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.


例如,要查找我当前所在目录下的目录:


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'
...


有人会说不是壳"……但是为什么要重新发明轮子呢?
(从某种意义上讲,它是shell ^^)


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

这篇关于递归读取文件夹并在每个文件夹上执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆