Recursively change filenames and find/replace text in Linux

Sometime for renaming files the "mv" command just isnt' enough, luckily though you can use the "rename" command instead to do pattern-matched renaming of files. It can also work in-combination with the find command.

eg. you can rename all files/directories at once using :

find . -exec rename -e 's/OLD/NEW/g' {} \; -print

Another good technique is to rename the folders first, then the files.

find . -type d|xargs -r rename "s/Old/New/g"
find . -type f|xargs -r rename "s/Old/New/g"

To find/replace text in the files you can use "sed -i" to interactively find/replace text in files. Combine that with find and you can do it recursively.

find . -type f -exec sed -i 's/Startnet2019/Startnet2019/g' {} \; -print

If you are developing you probably want to ignore the ".git" folder as well.

find . -path ./.git -prune -o -type f -exec sed -i 's/Startnet2019/Startnet2019/g' {} \; -print

Leave a Reply