Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash loop through all find recursively in sub-directories

I have a bash script that looks like the following:

#!/bin/bash
FILES=public_html/*.php  # */ stupid syntax highlighter!
for f in $FILES
do
  echo "Processing $f file..."
  # take action on each file.
done

Now I need it to go through all subdirectories in public_html so it should run on:

/public_html/index.php
/public_html/forums/status.php
/public_html/really/deep/file/in/many/sub/dirs/here.php

What do I change FILES=public_html/*.php to in order to do that?

Also I need to check to make sure that there is at least one file or else it prints

Processing *.php file...
like image 545
qwertymk Avatar asked Feb 19 '12 20:02

qwertymk


1 Answers

FILES=$(find public_html -type f -name '*.php')

IMPORTANT: Note the single quotes around the *.php to prevent shell expansion of the *.

like image 117
sgibb Avatar answered Oct 14 '22 02:10

sgibb