How do you do a find and replace for all the files in a directory?

PWSowner

Well-Known Member
Nov 10, 2001
2,940
4
343
ON, Canada
Here are some samples of batch file renaming that I saved from somewhere.

# change .htm files to .html
for file in *.htm ; do mv $file `echo $file | sed 's/\(.*\.\)htm/\1html/'` ; done

# change .html files to .htm
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1htm/'` ; done

#change .html files to .shtml
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1shtml/'` ; done


A modification of this looks like what you want.
 

WebHostPro

Well-Known Member
PartnerNOC
Jul 28, 2002
1,726
27
328
LA, Costa RIca
cPanel Access Level
Root Administrator
Twitter
squirrel said:
Here are some samples of batch file renaming that I saved from somewhere.

# change .htm files to .html
for file in *.htm ; do mv $file `echo $file | sed 's/\(.*\.\)htm/\1html/'` ; done

# change .html files to .htm
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1htm/'` ; done

#change .html files to .shtml
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1shtml/'` ; done


A modification of this looks like what you want.

Perfect, thanks!
 

haze

Well-Known Member
Dec 21, 2001
1,548
3
318
You can also use the commands "sed" and "replace" which are usually just as effective.
 

isputra

Well-Known Member
May 3, 2003
575
0
166
Mbelitar
DWHS.net said:
thanks, I'm not familar with this can you post a example for changing text for several files in a directory.
Yes, i would like to know it too ;)
 

haze

Well-Known Member
Dec 21, 2001
1,548
3
318
DWHS.net said:
thanks, I'm not familar with this can you post a example for changing text for several files in a directory.
This is where man pages usually come in handy ;)

replace "old-string" "new-String" -- *.ext
replace "old-string" "new-String" -- *
replace "old-string" "new-String" -- filename