You are not logged in.

#1 2011-02-21 20:01:18

whoops
Member
Registered: 2009-03-19
Posts: 891

[solved]remove backslashes from file names?

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

#2 2011-02-21 22:01:18

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,222
Website

Re: [solved]remove backslashes from file names?

whoops wrote:

So confused.

Me too... Post some examples of what you need to fix/rename...

Offline

#3 2011-02-21 22:35:06

sisco311
Member
From: Romania
Registered: 2008-05-23
Posts: 112

Re: [solved]remove backslashes from file names?

Backslashes aren't invalid characters in file names. So there is something wrong with the scripts/applications you are using. smile

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

#4 2011-02-22 00:02:55

pulce
Member
From: Italy
Registered: 2011-02-14
Posts: 64

Re: [solved]remove backslashes from file names?

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

#5 2011-02-22 00:08:01

whoops
Member
Registered: 2009-03-19
Posts: 891

Re: [solved]remove backslashes from file names?

sisco311 wrote:
#!/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"

pulce wrote:

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 big_smile - 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

#6 2011-02-22 00:27:43

sisco311
Member
From: Romania
Registered: 2008-05-23
Posts: 112

Re: [solved]remove backslashes from file names?

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. wink

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

Board footer

Powered by FluxBB