0

I have a directory's tree with several files inside.
I want to replace in every file, the string index.html for a character /.
Since / is a special character, How can I instruct to the linux "sed" command so I can change all files that I have under a directory?

Thanks!

Jotne
  • 40,548
  • 12
  • 51
  • 55
Goose
  • 351
  • 4
  • 15
  • 4
    The first character of the pattern determines the separator; there is nothing special about /, it's just often seen as the separator. I like to use @ if I need to use / in a pattern. e.g. echo 'a/index.html' | sed 's@index.html@/@g' gives a// – Nick Jul 25 '14 at 20:01
  • 1
    Alternatively, although maybe not as readable as Nick's suggestion, one can, in typical Unix fashion, escape the `/` with a `\`: `sed 's/index.html/\//g'` – John1024 Jul 25 '14 at 20:35

1 Answers1

6

Sed is usualy used with / as a separator. Actually you can use any symbol as a separator, sed just uses the first symbol after s command for that. For example sed 's|/|slash|'. Or you can escape / like that sed s/\//slash/. It's dangerous command, be careful, make a backup of your dir:

for file in `find /your/dir/ -type f`; do sed -i "$file" 's|index.html|/|g'; done
user3132194
  • 2,381
  • 23
  • 17
  • Boo, hiss. [Don't read lines with `for`](http://mywiki.wooledge.org/DontReadLinesWithFor) -- this will fail badly if names contain spaces or can be interpreted as glob characters. See also [BashPitfalls #1](http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29). – Charles Duffy Jan 30 '18 at 23:03
  • 1
    Much safer (and somewhat more efficient) to write `find /your/dir/ -type f -exec sed -i -e 's|index.html|/|g' {} +` – Charles Duffy Jan 30 '18 at 23:04