3

My iomega NAS, which uses a linux-like OS, has a bunch of backed-up files on it with filenames containing double quotes. Like this:

"Water"-4

"Water"-5

etc. (don't ask how they got there; they were originally created on a Mac)

This is causing problems when I try to copy the files to a backup drive: the quote marks are apparently causing the copy to fail. (The built-in copy facility uses rsync, but a rather old version.)

Is there a terminal command to batch-rename these files, just deleting the quote marks? Failing that, is there a command to rename them one at a time? The quote marks seem to really be messing things up (I know: the user has been warned!)

user3238181
  • 115
  • 1
  • 10
  • I should note that a few of the filenames also contain spaces (outside the portion within the quote marks). For example: "Water"-10 2 – user3238181 Dec 01 '14 at 16:33

3 Answers3

4

simple single line bash code:

for f in *; do mv -i "$f" "${f//[\"[:space:]]}"; done

$f is your current file name and ${f//[\"[:space:]]} is your bash substring replacer which stands for:
in this f (file name), // (replace) these [\"[:space:]] (characters) with nothing[1].

NOTE 1: string replacement statement: ${string//substring/replacement}; because you don't need to replace your substring to nothing, leave /replacement to blank.

NOTE 2: [\"[:space:]] is expr regular expression.

01e
  • 691
  • 3
  • 14
  • Worked great, thanks! Is there a way to do this recursively in subdirectories? And rather than removing a character (or string), can it be _replaced_ by a character (or string)? – user3238181 Dec 01 '14 at 18:43
  • you can use `find` command in for loop: `for f in $(find . -iname "*");`. to replace, I mentioned bash string replacement syntax in NOTE 1, for example: `"${f//[\"[:space:]/_}"` will replace all double quotes and spaces to underline. – 01e Dec 01 '14 at 19:06
  • Question: aren't you missing a right square bracket after [:space:] in your first line of code? It seemed to work the way you have it, but it appears to be inconsistent with your explanation. – user3238181 Dec 02 '14 at 07:18
  • yes, I edit them ;): you can use find command in for loop: `for f in $(find . -iname "*");`. to replace, I mentioned bash string replacement syntax in NOTE 1, for example: `"${f//[\"[:space:]]/_}"` will replace all double quotes and spaces to underline. – 01e Dec 02 '14 at 15:35
0

You can loop on the files and use mv:

for i in *
do
    mv "$i" "`echo $i | sed 's/"//'`"
done
Maroun
  • 94,125
  • 30
  • 188
  • 241
0

Try doing this :

$ rename -n 's/"//' *Water*

and remove the -n switch when your tests will be OK.

Take care, you need the perl's version of , there's sometimes another versions installed depends of your distro. If you don't have it, try to search prename or visit https://metacpan.org/pod/distribution/File-Rename/rename.PL

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223