Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename file containing substring 'foo' with 'bar'

I want to rename all files which contain the sub-string 'foo', replacing it with 'bar' within a given folder.How can I accomplish this?

Update:

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

works!!!

like image 852
Chirag Rupani Avatar asked Oct 22 '22 16:10

Chirag Rupani


1 Answers

If you have the rename(1) that came with perl (Debian provides it), you can use:

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

If you have the other rename(1) (I've seen it in 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 doesn't recognize the flag, it's the perl version. If it prints version information, it's the other version.

like image 146
cha0site Avatar answered Oct 24 '22 10:10

cha0site