5

I'd been trying to use the rename function in my debian, I searched here at unix.stackexchange but it seems the solution is not the same in my scenario which files contains []

[800p]-[WOLU-H]-test1.mkv
[800p]-[WOLU-H]-test2.mkv

desired output is just

[WOLU-H]-test1.mkv
[WOLU-H]-test2.mkv

i tried

rename [800p]-[WOLU-H] [WOLU-H] [800p]-*

but it keeps saying:

Bareword found where operator expected at (eval 1) line 1, near "800p"
    (Missing operator before p?)
syntax error at (eval 1) line 1, near "800p"

I tried other such as

rename 's/[800p]-[WOLU-H]/[WOLU-H]/' [800p]-*

but output also failed

Invalid [] range "U-H" in regex; marked by <-- HERE in m/[800p]-[WOLU-H <-- HERE ]/ at (eval 1) line 1.

can somebody enlighten me with the correct process? Thanks!

UPDATE

I tried this:

rename 's/\[800p\]-\[WOLU-H\]/\[WOLU-H\]/' \[800p\]-*

but error:

Bareword found where operator expected at (eval 1) line 1, near "800p"
        (Missing operator before p?)
Backslash found where operator expected at (eval 1) line 1, near "p\"
Backslash found where operator expected at (eval 1) line 1, near "]\"
        (Missing operator before \?)
Backslash found where operator expected at (eval 1) line 1, near "]\"
        (Missing operator before \?)
syntax error at (eval 1) line 1, near "800p"
Unmatched right square bracket at (eval 1) line 1, at end of line
Unmatched right square bracket at (eval 1) line 1, at end of line

I also tried this:

rename "[800p]-[WOLU-H]" "[WOLU-H]" "[800p]-"*

But still error

Bareword found where operator expected at (eval 1) line 1, near "800p"
        (Missing operator before p?)
syntax error at (eval 1) line 1, near "800p"

I think the - with numerics are messing?

1
  • 1
    You need to escape [ using \ .
    – zer0rest
    Apr 16 '15 at 11:25
6

[ and ] have a special meaning in bash and also in regular expressions, so you have to escape them as \[ and \]. Something like this should work:

rename 's/\[800p\]-\[WOLU-H\]/\[WOLU-H\]/' \[800p\]-*

Example:

$ touch [800p]-[WOLU-H]-test1.mkv [800p]-[WOLU-H]-test2.mkv
$ ls
[800p]-[WOLU-H]-test1.mkv  [800p]-[WOLU-H]-test2.mkv
$ rename 's/\[800p\]-\[WOLU-H\]/\[WOLU-H\]/' \[800p\]-*
$ ls
[WOLU-H]-test1.mkv  [WOLU-H]-test2.mkv
4
  • 2
    This is true, strictly though the error is a perl error message, arising from the use of perl rename command. Apr 16 '15 at 12:43
  • @mr.spuratic My answer was intended to be a bit more generic. bash refers to the expansion of [800p]-* in the shell and regular expression to the s/[800p]-[WOLU-H]/[WOLU-H]/ part. Such kind of an error would have probably been thrown by another tool, written in another language...
    – FloHimself
    Apr 16 '15 at 13:02
  • @FloHimself I don't know, but if my intuition is correct, the - is messing with the command? Bareword found where operator expected at (eval 1) line 1, near "800p" (Missing operator before p?) Backslash found where operator expected at (eval 1) line 1, near "p\" Backslash found where operator expected at (eval 1) line 1, near "]\" (Missing operator before \?) Backslash found where operator expected at (eval 1) line 1, near "]\" (Missing operator before \?)
    – arvil
    Apr 16 '15 at 13:06
  • @TheWolf I've added an example of how I've used the command.
    – FloHimself
    Apr 16 '15 at 13:17
-1

Why don't you simply use quotes? So you don't need to escape those [.

rename "[800p]-[WOLU-H]" "[WOLU-H]" "[800p]-"*

(Note the * is out of the "...")

EDIT: Comments make me notice it doesn't work. I don't understand why, because it works here:

$ ls -1
[800p]-[WOLU-H]-foobar.txt
[800p]-[WOLU-H]-hello.txt
[800p]-[WOLU-H]-world.txt

$ rename "[800p]-[WOLU-H]" "[WOLU-H]" "[800p]-[WOLU-H]"*

$ ls -1
[WOLU-H]-foobar.txt
[WOLU-H]-hello.txt
[WOLU-H]-world.txt

$ rename -V
rename (util-linux-ng 2.17.2)

I'm using CentOS 6.6, maybe that's the difference... Anyway, sorry for the wrong answer...

4
  • I am getting this? Bareword found where operator expected at (eval 1) line 1, near "800p" (Missing operator before p?) syntax error at (eval 1) line 1, near "800p"
    – arvil
    Apr 16 '15 at 13:04
  • 1
    Because it does not work. Bareword found where operator expected at (eval 4) line 1, near "800p" (Missing operator before p?) syntax error at (user-supplied code), near "800p"
    – A.B.
    Apr 16 '15 at 13:04
  • 1
    @A.B. Oh, sorry. It was working on my box (see my update), I didn't notice the debian flag...
    – apaul
    Apr 16 '15 at 13:22
  • 1
    Should you wish to experiment, Debian's rename (perl "alternative") does not use anything expected like search.cpan.org/~rmbarker/File-Rename-0.20 , but instead something exceedingly ancient (from the same author, rmb) which you can extract from the source patch package here: packages.debian.org/wheezy/perl Apr 16 '15 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.