Rename the file containing the substring 'foo' with 'bar'

I want to rename all files containing the substring " foo ", replacing it with " bar " in this folder. How can i do this?

Update:

 for i in ./*foo*; do mv "$i" "${i//foo/bar}";done 

works!!!

+4
source share
1 answer

If you have rename(1) that comes with perl (provided by Debian), you can use:

 cd /path/to/directory rename 's/foo/bar/g' * 

If you have another rename(1) (I saw it on Red Hat Enterprise Linux and some other distributions, it comes from util-linux), you can try:

 cd /path/to/directory rename foo bar *foo* 

You can check which version of rename you have by trying rename -V . If it does not recognize the flag, this is the perl version. If it prints version information, this is a different version.

+3
source

All Articles