Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash Script How to find every file in folder and run command on it

Tags:

bash

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

Thanks

like image 418
user2872510 Avatar asked Dec 26 '22 19:12

user2872510


2 Answers

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 as suggested by others is a good solution for this problem, though.

like image 191
Gavin Smith Avatar answered Jan 20 '23 07:01

Gavin Smith


find has an option for doing exactly that:

find /p/a/t/h -name '*.cpp' -exec g++ {} \;
like image 32
William Pursell Avatar answered Jan 20 '23 07:01

William Pursell