Find And Replace On All Files In And Below A Directory

The following shell command uses the find function to find all files in or below the current directory that have the extension php. It then passes each file found onto a sed command which then replaces all <? with the longer <?php version.

find . -name '*.php' -exec sed -ie 's#<?#<?php#' {} \;

The -name argument in find will look at the base of the file name, that is, the file without any directory path. The -exec command is used to pass each file found onto another command, in this case sed is used.

Comments

A quick note: if you are searching to replace a link to a directory ie text/documents to text/document/new you must escape the / char so for the above example the command would be:

find . -name '*.php' -exec sed -ie 's#text//documents#text//document//new#' {} \;
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
1 + 14 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.