How to rename a bunch of files to eliminate quote

2019-09-17 13:42发布

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!)

标签: linux
3条回答
神经病院院长
2楼-- · 2019-09-17 14:03

You can loop on the files and use mv:

for i in *
do
    mv "$i" "`echo $i | sed 's/"//'`"
done
查看更多
Bombasti
3楼-- · 2019-09-17 14:13

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.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-09-17 14:15

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

查看更多
登录 后发表回答