Linux – Find and Replace String in filenames

batchcommand linelinuxunix

I have thousands of files with no specific extensions. What I need to do is to search for a sting in filename and replace with other string and further search for second string and replace with any other string and so on. I.e.: I have multiple strings to replace with other multiple strings. It may be like:

  • "abc" in filename replaced with "def" *** String "abc" may be in many files
  • "jkl" in filename replaced with "srt" *** String "jkl" may be in many files
  • "pqr" in filename replaced with "xyz" *** String "pqr" may be in many files

I am currently using excel macro to get the file names in excel and then preserving original names in one column and replacing desired in the content copied in other column. then I create a batch file for the same. Like:

rename Path\OriginalName1 NewName1
rename Path\OriginalName2 NewName2

Problem with the above procedure is that it takes a lot of time as the files are many. And As I am using excel 2003 there is limitation on number of rows as well. I need a script in batch like:

replacestr  abc with def
replacestr  pqr with xyz

in a single directory. Will it be better to do in unix script?

Best Answer

If you can use Bash, then the following script should do what you want:

#!/bin/bash

(( $# != 2 )) && exit 1

for f in *; do
  newf="${f//$1/$2}"
  if [[ $f != $newf ]]; then
    mv "$f" "$newf"
  fi
done

It tries to replace filename with new string, if resulted filename doesn't match original, then it renames the file to resulted filename.

Usage, if saved as replacestr and given mode to be executed:

$ ./replacestr abc def

It will try to rename all. You can use [[ ! -f $f ]] to skip non-file. You can also wrap it in function and source it from your ~/.bashrc if you need this very often.

Related Question