Jump to content
  • Checkout
  • Login
  • Get in touch


The e-commerce.

Find and Find/Replace for mutiple files


Recommended Posts

here are a couple of tips for doing searches across multiple files that will make your life easier. i'm going to write this for MAC users who may not be familiar with UNIX commands. if you're already a unix user you can just scroll down for the appropriate commands.


1. put all the files you want to check into a folder, make sure the folder has a 1 word title (e.g. Test), and make sure that folder is inside your home folder.

2. open Applications/Utilities/Terminal. this will open a unix command shell.

3. from the command line of the Terminal window type: "pwd" (without quotes) and hit return.

4. you should then see something like this: /User/yourUserName

5. type "cd Test" where Test is whatever you named your folder in step 1, and hit return.

6. if you type "ls" and hit return, you should see a list of all your files to test.


to find find out if a particular bit of text appears in any of the files in the folder type the following command:

find . -type f -exec grep "TEXT TO SEARCH FOR" {} \; -print


this will output a list of all the files that contain the search text, as well as the line in each file containing the text.


why would you want to do this? for example, i'm going through my stylesheet.css and trying to tidy it up. i find an old selector called ".oldStyle" I think i can delete this, but i want to make sure it isn't actually used anywhere in my shop. so i do the above search on the text "oldStyle". if the only place it appears is in stylesheet.css, then i can safely delete it. if however it appears in some other file along with something like 'class="oldStyle"', then i know that it's being used (and where if i want to change that). another example: suppose you're troubleshooting some problem, then this is a quick way to figure out which file a particular bit of text comes from (something i've spent a lot of time trying to figure out in the past).


the second command is a find and replace command, which is useful when you want to change the same bit of text in multiple files:

find /path/to/start/from/ -type f | xargs perl -pi -e 's/FIND/REPLACE/g'

for example i just used this to replace "doctype html public" with "DOCTYPE HTML PUBLIC" (which is how the doctype statement is supposed to be written).


always back up first! happy coding! your milage may vary!

Link to comment
Share on other sites

a couple of pointers.


in the first command for finding text, if your search text contains quotation marks you need to escape them, with backslashes. for example, to find language="javascript" you would using the following:

find . -type f -exec grep "language=\"javascript\"" {} \; -print



similarly, for the second command, if your find/replace text has slashes in it you will need to escape them with backslashes, which looks odd, but works. for example to find language="javascript" and replace it with type="text/javascript" (to make it valid HTML) you would use:

find /path/to/start/from/ -type f | xargs perl -pi -e 's/language="javascript"/type="text\/javascript"/g'



always back up first! happy coding! your milage may vary!




did i mention you should back up first? this sort of batch replacing really has the potential to mess things up if you make a mistake.



Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...