简体   繁体   中英

Shell script to iterate through sub-directories of a directory and execute a command

I want to compile each and every file in a directory as well as its sudirectories and directories of that subdirectories. So the basic idea is to compile all the files in that project. For that i have to write a shell script.

My code

cd pathtomaindirectory
gcc *.c

In this i have to run the that command on all the subdirectory files with .c extensions. So how do i use a for loop to it. I'm new to shell script

Thank You

With bash, enable recursive glob wildcards:

shopt -s globstar nullglob
gcc **.c

You could do something like

cd path/to/main/directory
find . -name '*.c' -exec gcc -c {} \;
gcc -o app *.o

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM