Find and replace text in multiple files 4


On Unix, in one command line ?

# Here the search is in PHP files, but 
# replace by whatever your file extension is.
find . -name "*.php" -print | xargs sed -i 's/<search>/<replace>/g'
# and that's all.
Fab
Latest posts by Fab (see all)

About Fab

Solutions Architect, I build great workflows for the news and media production industries. I play with data too.

Leave a comment

Your email address will not be published. Required fields are marked *

4 thoughts on “Find and replace text in multiple files

  • Chabadarl

    Dans le même ordre d’idée, pour modifier un grand nombre de nom de fichier en même temps :
    (dans cet exemple je remplace les .LOG en .log)
    ls *.LOG | sed ‘s/\(.*\).LOG/mv \1.LOG \1.log/g’ | sh

    Pour vérifier, dans un premier temps, l’action réalisée :
    ls *.LOG | sed ‘s/\(.*\).LOG/mv \1.LOG \1.log/g’

    et ne rajouter le pipe | sh que lorsque vous êtes sûrs de votre coup.

  • Nico

    Also, the rename command on Debian distros does just that and uses the same Perl regexp syntax.

    rename -n s’///’ *.whatever

    The -n allows you to make a test run in case you’d do anything silly which you cancel. -v is for verbose

    Cheers

  • Nico

    My comment was truncated. The correct (more explicit) syntax is:
    rename -n s’/stringtofind/stringtoreplacewith/’ *.whatever