Finding Replacing Filenames & text inside files using perl

A few “Bash Tricks” for the peeps

First up:

Find & replace filenames

This finds file that match Somethinghtml.html and renames to Something.html (HTTP Track users will recognize these two examples as fixes to common naming problems when scraping a site.) Note you can change the mask of files searched by changing the find mask at the end of the line.

<code>

perl -p -i -e ’s/(.*)html\.html/\1.html/g;’ `find ./ -name ‘*.html’`

</code>

Then Find & Replace text inside of files:

This finds things that have been incorrectly prefixed with dev.mistcat.com and removes that prefix so that this 2112.js file can load from a remote domain correctly.  Note you can change the find mask at the beginning to restrict or un-restrict your searched files for text replacement.

<code>

find . -name ‘*.html’ | perl -pi -e ’s/http:\/\/dev\.mistcat\.com\/(www\.dwin1\.com\/2112\.js)/http:\/\/\
1/g;’

</code>

Coding, Linux / Unix / CLI

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Leave Comment

(required)

(required)