How to Search & Replace Multiple Files in UNIX

Step 1 Open a blank file in any text editor.

Step 2 Place the following script into the the text editor:

#!/bin/bash
#
echo "Type your find string followed by [Enter]:"
read fstring
echo "Type your replace string followed by [Enter]:"
read rstring
for y in `ls *`;
do sed "s/$fstring/$rstring/g" $y > temp; mv temp $y;
done

Step 3Save the script with the name "findrep.sh" in the same directory as the files you want to manipulate.

Step 4Open a Terminal Window. The terminal window will be found in the operating system's main "Application" menu, under either "System Tools" or "Utilities." You will be presented with a command prompt where you will type the following commands.

Step 5Type the command "mv " to move into the directory with the script. For example, the command would be "mv Files/" if the script was held in the "Files" directory in your home directory.

Step 6Type the command "chmod +x findrep.sh" to make the file executable.

Step 7Type the command "./findrep.sh" to execute the script.

No comments:

Post a Comment

Please Provide your feedback here