Showing posts with label find. Show all posts
Showing posts with label find. Show all posts

Tuesday, June 12, 2007

Perl - Find and replace all

Find cpp and hpp recursively from the current directory then append those files onto the perl command, which substitutes "old" for "new" in those files while creating .bak backups.
find . -name "*.[ch]pp" -type f | xargs perl –i.bak –p –e 's/old/new/g;'

or non recursive:
perl -pi -w -e 's/search/replace/g;' *.php
-e means execute the following line of code.
-i means edit in-place
-w write warnings
-p loop

Tuesday, April 17, 2007

UNIX/Linux - Find command

To look for files in the current directory tree then run a command on them:
find . -name <file-pattern> -exec <command> {} \;

Examples

To grep for "hello" inside all .txt files:
find . -name *.txt -exec grep -Hn hello {} \;

Put this in a shell script to recursively grep .cpp and .hpp files for the first argument:
#!/bin/bash
find . -name *.[ch]pp -exec grep -Hn "$1" {} \;