How to Search & Replace Multiple Files in UNIX

104 6
    • 1). Open a blank file in any text editor.

    • 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

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

    • 4). Open 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.

    • 5). Type the command "mv <path to file>" 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.

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

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

Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.