How to replace a string in all folder and file names
you can replace all the file names using for
and mv
command.
here i am replacing all the text files starting with abc
names with xyz
for file in abc*.txt; do mv -v "$file" "${file/abc/xyz}"; done
the below command will replace the files recursively in all folders
for file in `find . -type f -name 'abc*.txt'`; do mv -v "$file" "${file/abc/xyz}"; done
Using find
and rename
:
find . -type f -exec rename 's/string1/string2/g' {} +
It's easier with zsh
's zmv
:
autoload zmv # best in ~/.zshrc
zmv '(**/)(*string1*)' '$1${2//string1/string2}'
Change to
zmv '(**/)(*string1*)(#qD)' '$1${2//string1/string2}'
If you also want to rename hidden files or files in hidden directories.