better unix life

This entry is part 1 of 2 in the series Unix swissknife

File mass replace

Use the sed -i command

sed -i.BAK 's|foo|bar|g' files # -i does in-place replacement
perl -pi.bak -e 's|foo|bar|g' files                # or
perl -pi.bak -e 's|foo|bar|g' `find /pathname -name "filespec"`


Perl is the preferred way, but on some production system you must use the old good sed. For some tips on sed take a look to its faq.


Another very powerful command is xargs. Xargs is a rapid  way to process files containing spaces, using  a  combo with find:

find . -print0 -type f   | xargs -0 ls  | grep " "

Xargs is normally fastest then relaying on backtick substitution. For instance:

ls $(find . -type f)

is  slower then

find . -type f | xargs ls

because this second form create two process which works at the same time, using the unix pipe.

Series NavigationBash secret powers

Leave a Reply