0

I have a directory with ogg file, and want to change file names starting with i-.

What can I do? For instance, doing rename 's/i-nuovi/nuovi/g' * gave me

Unknown option: marino-barreto-cinque-minuti-ancora.ogg
Usage:
    rename [ -h|-m|-V ] [ -v ] [ -n ] [ -f ] [ -e|-E perlexpr]*|perlexpr
    [ files ]

Something is wrong, but cannot track it down. How can I ensure that I watch i- from the beginning?

Vera
  • 1,173
  • 4
  • 17
  • 1
    There are a few `rename` implementations. Which one are you using? Have you tried `rename -e 's/i-nuovi/nuovi/g' ./*`? – xiota Jan 21 '23 at 04:42
  • 1
    The title says `-i`, but the body says `i-`. The first could cause problems. The second shouldn't. – xiota Jan 21 '23 at 04:43
  • 1
    Thank you for pointing out the problem with the title. I found out that some files started with `-` and `rename` interpreted that as an option. – Vera Jan 21 '23 at 05:03
  • Things are not very clear, what I know is that `rename` is version 0.20 – Vera Jan 21 '23 at 05:16

2 Answers2

1

If you don't demand using rename tool, this can be easily done with bash native functions/built-ins:

for-loop and parameter expansion (use man bash and search for Parameter Expansion for more info):

for ef in $(ls i-*); do mv $ef ${ef#i-}; done

If your files contain white spaces in their names you can use IFS (bash builtin also):

old_IFS=$IFS; IFS=$'\n'; for ef in $(ls i-*); do mv $ef ${ef#i-}; IFS=$old_IFS; done
Damir
  • 491
  • 2
  • 4
  • 1
    Better `for ef in i-*; do mv "$ef" "${ef#i-}"; done` - see [bash pitfall #1](http://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29). With proper quoting, there's no need to mess with `IFS`. – steeldriver Jan 21 '23 at 15:24
  • @steeldriver, I realy like your approach. I will definitely use it. – Damir Jan 21 '23 at 15:39
0

Different implementations of Perl's rename:

Try

rename --version

and check my link

Then

rename -n 's/^i-nuovi/nuovi/g' -- ./*.ogg
#                              ^^
#                       end of parameters

If that doesn't work, just try:

rename -n 's/^i-nuovi/nuovi/g' ./*.ogg
#                              ^^
#                       that should helps 

Remove -n (aka dry-run) when the output looks satisfactory.

Gilles Quénot
  • 31,569
  • 7
  • 64
  • 82
  • How can I ensure the match starts from the beginning of the file? – Vera Jan 21 '23 at 05:38
  • Added `^` anchor, check new code. – Gilles Quénot Jan 21 '23 at 05:53
  • 1
    @GillesQuenot at least with my version of Perl rename (*File::Rename version 1.30*), `^` doesn't match at the beginning of the actual filename when the glob pattern start with `./`, **unless** you add one of `-d, --filename, --nopath, --nofullpath` – steeldriver Jan 21 '23 at 14:20