title: Using Shell script and sed - Find and replace a word in file and rename file
body: <p>I am writing one shell script which finds a given text in the files and replace that from a specified path and after replacing the text, Rename that file with with the given word.
  I am getting an error of permission denied while using the sed. My script looks like this</p>
  
  <pre><code>`echo "Please Insert the path of the folder"
   read input_variable
  
      read -p "You entered: $input_variable is correct y/n  " yn
  
      read -p "Enter the word to find = " word
      read -p "Enter word to replace = " replace
      case $yn in
          [Yy]* ) find "${input_variable}" -type f -iname "${word}.*" | while read filename; do "`echo "${filename}" | sed -i 's/$word/$replace/g' ${filename}| sed -i 's/\$word/\$replace/' ${filename}`"; done ;;
          [Nn]* ) exit;;
          * ) echo "Please answer yes or no.";;
      esac`
  </code></pre>
  
  <p>I am getting the below error</p>
  
  <blockquote>
    <p>bulk_rename.sh: 34: bulk_rename.sh: : Permission denied</p>
  </blockquote>
  
  <p>Any suggestions ?</p>
  
  <p>After the suggestion by @vijay updated the script</p>
  
  <pre><code>echo "Please Insert the path of the folder"
  read input_variable
  
  read -p "You entered: $input_variable is correct y/n  " yn
  
  read -p "Enter the word to find = " word
  read -p "Enter word to replace = " replace
  case $yn in
      [Yy]* ) find "${input_variable}" -type f -iname "${word}.*" | while read filename; do 
      perl -pi -e 's/$word/$replace' ${filename}
      mv ${filename} $word;   done;;
  
      [Nn]* ) exit;;
      * ) echo "Please answer yes or no.";;
  esac
  </code></pre>
  
  <p>now I am getting the following </p>
  
  <hr>
  
  <blockquote>
    <p>Substitution replacement not terminated at -e line 1</p>
  </blockquote>
  
  <p>This is what i get when I chmod and displays the output</p>
  
  <pre><code>abc@PC-DEV-41717:~/Documents/blog$ chmod +x bulk_rename.sh ; /bin/ls -l bulk_rename.sh
  chmod +x bulk_rename.sh ; /bin/ls -l bulk_rename.sh
  + chmod +x bulk_rename.sh
  + /bin/ls -l bulk_rename.sh
  -rwxrwxr-x 1 abc abc 1273 Aug  1 16:51 bulk_rename.sh
  </code></pre>