i need a quick command (linux or windows) to replace every \\ with a /, and all tries with sed failed because of the /.
(I already tried find . -name '*.*' -exec sed -i 's/\\///g' {} \;
, but i think it failed with the "/".
Asked
Active
Viewed 80 times
1

tripleee
- 175,061
- 34
- 275
- 318

Lea Eichelsdörfer
- 76
- 2
- 6
-
I meant \\, but it was replaced :( – Lea Eichelsdörfer Mar 19 '17 at 16:39
-
1You tagged Python here. I believe that if you actually use the `os` module, this *should* all be taken care of for you when providing the respective path on that platform. – idjaw Mar 19 '17 at 16:39
-
1Also, for whatever case you are doing this for, there are several solutions out there that do this for you in many different ways. Have you tried all solutions out there? – idjaw Mar 19 '17 at 16:41
-
1Can you post the error you got? – tdelaney Mar 19 '17 at 16:51
-
1There would have been a `sed` syntax error for the unescaped literal slash. – tripleee Mar 19 '17 at 16:51
-
The error just was "no such file or directory" – Lea Eichelsdörfer Mar 20 '17 at 16:57
-
@tripleee: No, it said it replaced some things, but it didn't – Lea Eichelsdörfer Mar 20 '17 at 17:00
-
@idjaw: No, the problem is a "\\\\", what works on windows, but not on linux – Lea Eichelsdörfer Mar 20 '17 at 17:07
1 Answers
1
find . -name '*.*' -type f -exec sed -i 's:\\\\:/:g' {} \;
You need to escape each backslash, and using a colon or comma as separators is generally recommended when making replacements with forward-slash. However, escaping the forward slash works too:
find . -name '*.*' -type f -exec sed -i 's/\\\\/\//g' {} \;
As pointed out in comments the OS module is probably what you really need to look at.
Edit: thanks to @tripleee for reminding me of the -type f
line, which limits it to files, rather than including the current directory.
Also, I copied the syntax *.*
from the OP but in general it isn't helpful. *
alone is usually what you want, since files aren't guaranteed to have a dot in their name. Assuming you were happy to include files not containing a dot, the simplest thing to do here is have no -name
at all:
find . -type f -exec sed -i 's:\\\\:/:g' {} \;

GKFX
- 1,386
- 1
- 11
- 30
-
@tdelaney OP doesn't want that, they've made it clear in the title and comment they want two backslashes to be replaced, not one. – GKFX Mar 19 '17 at 16:59
-
-
1Okay, I sse. I assumed that comment referred to the code example and that OP had fixed it by the time I got here. – tdelaney Mar 19 '17 at 17:09
-
I get an error when I try this `Cannot process . :That's not a normal file ` (I hope I translated it right) – Lea Eichelsdörfer Mar 20 '17 at 17:05
-
1You'll want to add a `-type f` to the `find` command line, before the `-exec`. – tripleee Mar 20 '17 at 17:21
-
@tripleee Thanks! I'd meant to look up that switch and add it but forgot. It's now in the answer. – GKFX Mar 20 '17 at 21:06