In Mac OS Find & Replace Strings in Multiple files using Batch Script

Mac trick

This is a step-by-step instruction for people who are totally oblivious to the wonderland of programming and coding, people like me  🙂

Suitable scenario:

In Mac OS system, repetitively replace same strings in a large number of plain text files.


  • Reusable; execute in batch
  • Works for Western and Asian characters


  • Only works for plain txt files.
  • Can be quite slow when there are hundreds of txt files.
  • Some symbols can mess up the whole file.
  • Case sensitive (For me it is a disadvantage, but it doesn’t have to be bad. Perhaps there is a parameter to tweak that, but I didn’t manage to find it. If anyone knows please let me know.)
  • There might be limitations in encodings, but I didn’t do any experiment on that.


Make sure you backup all the files before run the script!!!!

Part 1: Single line to execute “Find & Replace”

Extract from: How to Quickly Find and Replace Text Across Multiple Files with One Command

The command is:

perl -pi -w -e 's/SEARCH_FOR/REPLACE_WITH/g;' PATH/*.txt

Example: Replace ‘Lucy‘ with ‘露西‘ in all txt files in a dropbox folder called ‘txtFiles‘:

perl -pi -w -e 's/Lucy/露西/g;' ~/Dropbox/txtFiles/*.txt


perl -pi -w -e ...
  • -p: assume ‘while (<>) { … }‘ loop around program and print each processed line too.
  • -i.bak: change the input file (filename) in place and create the file filename.bak as backup.
  • -w : use warnings
  • -e execute the following command
     's/SEARCH/REPLACE/g' ...
  • s in s/: to mark substitution
  • g: make the substitution globally..that is don’t stop after first replacement.


So, if you just need to replace one string in batch, simply launch Terminal and type this line. That’s it!

If you need to repetitively find & replace same strings in multiple text files, then please read on.

Part 2: Execute commands in batch

The standard syntax in Terminal, end every command with a “;”

So, type in Terminal:

cmd 1;

cmd 2;

cmd n

Now you can already replace multiple strings in multiple txt files.

If you want to be able to replace the same strings again, please read on!!!

Part 3: Create a executable script file

Extract from:

Save all commands in the above format in a plain text file.

Save the file with no extension!!! e.g.


Make sure the script is executable, type in Terminal:

chmod 755 ~/Desktop/exampleScript

This command changes the file’s permission, so it is executable.

The 755 is made up of three digits, that represent the permissions for you, others in your “user group” and all others. You want yourself to be able to write to the file and execute it (7) and you want everyone else to be able to read it or execute it but not write it (5). You could also give them 7 if you want (chmod 777 curlscript), etc.

Part 4 (Almost there!): Execute the script

Simply type the script in Terminal including all path, e.g.


Again, make sure to backup everything before executing the script!!!

The first time I tried, it was a fiasco. One symbol in the script messed up all my txt files, and I didn’t back up!!! There was simply no way back. Don’t be as silly as me.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s