LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to run a script on subfolder file in a folder (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-run-a-script-on-subfolder-file-in-a-folder-335378/)

saggi_sam 06-20-2005 06:41 AM

how to run a script on subfolder file in a folder
 
i have a folder 'root' with .html files in it. Want to run a script which should search and replace a string in all these .html files. this script should search these .htmls as well as any other .html file in subfolders of 'root'. So script should also search sub-folder of root folder.

theYinYeti 06-20-2005 07:19 AM

find 'root' -type f -iname "*.html" -print | while read file; do sed -i 's/regexp/replacement/g' "$file"; done

Yves.

ahh 06-20-2005 04:01 PM

Or a slightly shorter version:-

Code:

find 'root' -type f -iname "*.html" -exec sed -i 's/regexp/replacement/g' '{}' \;

saggi_sam 07-12-2005 05:10 AM

thnx...dear..it helped

rgds.
sumit

Centinul 07-12-2005 08:30 PM

Quote:

Originally posted by ahh
Or a slightly shorter version:-

Code:

find 'root' -type f -iname "*.html" -exec sed -i 's/regexp/replacement/g' '{}' \;

just out of curiousity and since I'm new to linux can someone explain what that code does? Thanks!

ahh 07-13-2005 05:04 AM

Quote:

Originally posted by Centinul
just out of curiousity and since I'm new to linux can someone explain what that code does? Thanks!
Code:

find 'root' -type f -iname "*.html" -exec sed -i 's/regexp/replacement/g' '{}' \;
find - The name of a command to find files/directories.
'root' - The path of the diretory you weant to start searching in, e.g. /home/centinul
-type f - Find regular files.
-iname "*.html" - Do a case insensitive search to match <anything>.html
-exec - execute the following, up to the \;, once for each result of the search, replacing '{}' with the file name.


sed - The name of a command to edit text
-i - Do the editing in place, not to the console or another file.
's/regexp/replacement/g' - 'do a search and replace / look for text that matches this regular expression / replace it with this text / do it globally, dont stop at the first match'


All times are GMT -5. The time now is 10:12 PM.