Linux rename folders recursive

Rename folders and subfolders search and replace

To search and replace in folder names recursivly you can use this short bash script ...

## rename all directories which contain searchstr to replacestr
cd /YourStartFolder/
NUMBER_OF_DIRS=`find . -name *searchstr* |wc -l`
count=1
while [ $count -le $NUMBER_OF_DIRS ]
  do
    echo $count 
    echo "mv `find . -name *searchstr* | head -1` `find . -name *searchstr* | head -1 | sed 's/searchstr/replacestr/g'`"
    mv `find . -name *searchstr* | head -1` `find . -name *searchstr* | head -1 | sed 's/searchstr/replacestr/g'`
    count=$[$count+1]
done

The script is not build for performance - but it works ;) ... It builds a new dir-list after each iteration, because when you rename a folder in a higher level the "mv" command wouldn't work anymore for subfolders.

Example:

/YourStartFolder
  /test2
  /test3
    /test4

Running the script with "searchstr=test" and replacestr=abc in folder "YourStartFolder" will result in:

/YourStartFolder
  /abc2
  /abc3
    /abc4