Bash脚本如何查找文件夹中的每个文件并在其上运行命令 [英] Bash Script How to find every file in folder and run command on it

查看:55
本文介绍了Bash脚本如何查找文件夹中的每个文件并在其上运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图创建一个在文件夹中查找并找到所有具有.cpp的文件类型并在其上运行g ++的脚本的脚本.到目前为止,我有,但没有运行,它说意外结束

So im trying to create a script that looks in a folder and finds all the file types that have .cpp and run g++ on them. so far i have but it doesn't run it says unexpected end

for i in `find /home/Phil/Programs/Compile -name *.cpp` ; do echo $i ; 
done

谢谢

推荐答案

您的代码存在的问题是,通配符 * 在传递给查找之前已由外壳程序进行了扩展.因此引用它:

The problem with your code is that the wildcard * is being expanded by the shell before being passed to find. Quote it thusly:

for i in `find /home/Phil/Programs/Compile -name '*.cpp'` ; do echo $i ;  done

不过,其他人建议的

xargs 是解决此问题的好方法.

xargs as suggested by others is a good solution for this problem, though.

这篇关于Bash脚本如何查找文件夹中的每个文件并在其上运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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