You are not logged in.
Hi!
I have a lot of files that contain backslashes in file name, spread over many many folders.
How do I remove that character from all file names? The approaches / scripts / programs I usually work with can't handle the invalid file names (e.g. loose the '\' and then 'cant stat()' -.-").
Thanks!
edit: Argh... now I've got copies of half of the files without the "\" in the same folders, because copying did work on most but deleting only on some... also multiple \\\ because I messed up one sed line. and "echo" failed to warn me about the result, just dropping a few \ while mv didn't... or something. So confused.
Last edited by whoops (2011-02-22 00:22:58)
Offline
So confused.
Me too... Post some examples of what you need to fix/rename...
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline
Backslashes aren't invalid characters in file names. So there is something wrong with the scripts/applications you are using.
Anyway, in bash you can do something like:
#!/usr/bin/env bash
while IFS= read -rd '' file; do
dirname="${file%/*}"
basename="${file##*/}"
mv --backup=numbered -- "${file}" "${dirname}/${basename//\\/_}"
done < <(find /full/path/to/dir -depth -name '*\\*' -print0)
Last edited by sisco311 (2011-02-21 22:44:32)
don't drink unwashed fruit juice.
i never make predictions, especially about the future.
Offline
Not sure that's the point, but backslash is an escape, it preceeds spaces or partenthesys or stuff in your filenames. For example if you have a file called "My text file (1).txt" you will see it as My\ text\ file\ \(1\).txt
Another example: a file called "\mytext.txt" will be \\mytext.txt, the escape let's you use special characters as if they were regular characters.
So if you have files with double backslash in their names the script posted by sisco311 should do the trick.
Offline
#!/usr/bin/env bash while IFS= read -rd '' file; do dirname="${file%/*}" basename="${file##*/}" mv --backup=numbered -- "${file}" "${dirname}/${basename//\\/_}" done < <(find /full/path/to/dir -depth -name '*\\*' -print0)
Thanks, that did it!
Don't know what exactly I've been doing wrong, but it seems not to happen any more since I try to do stuff with "while, read, ${}" instead of "for, $(), echo, sed"
Not sure that's the point, but backslash is an escape
Yes, I know, that was the problem. That's why those stupid things always keep either disappearing or multiplying - I guess I just suck at keeping track of what program handles them which way and depending on what.
Last edited by whoops (2011-02-22 00:15:30)
Offline
You're welcome!
If you want to learn bash scripting, then check out:
http://mywiki.wooledge.org/
and stay away from guides which teach you how to write bugs, like the advanced bash-scripting guide.
Last edited by sisco311 (2011-02-22 00:30:08)
don't drink unwashed fruit juice.
i never make predictions, especially about the future.
Offline