Using util-linux rename command
Asked Answered
H

3

9

I've been attempting to use the util-linux version of rename (2011) to replace a specific string in all files with another. While I realize the perl version of rename would offer a solution, I can't figure out how to use this version of rename.

The specific example are a set of files (something--2013.mkv, somethingelse--2011.mkv), and I'm trying to remove the double dashes and replace with a space.

Heppman answered 16/2, 2014 at 6:43 Comment(8)
did you try something like this rename 's/--/\ \ /' *.mp4Mendiola
rename 's///' is syntax for the perl version, not the util-linux version. If I were using the perl version, your command would work on .mp4 files :)Heppman
-- means "the end of the options" for programs using getopt (such as rename). The solution is most likely to find the way around that. Perhaps you could translate - into something else first (e.g. _) and then replace that?Doubledecker
I have tried rename '--' ' ' *.mkv but this does not work.Heppman
Of course it doesn't: the shell interprets the quotes and the command just gets --Doubledecker
How should it be able to rename '--' to '_' if it cannot correctly interpret the variables?Heppman
Rename one dash (-) at first (e.g. to a single underscore _), then rename two (e.g. __) to a single dash. I can't test whether it works because I'm not on Linux right ATM. Otherwise I'd have posted as an answer. Another options could be to try rename -- -- - fileDoubledecker
Renaming a single dash at a time to an underscore, then renaming the double underscore works. If you want to post an answer I can mark this as solved.Heppman
D
11

The problem is that rename uses getopt for argument parsing and thus has a special interpretation for double dash (--). -- signifies the end of the arguments.

A solution would be to avoid using -- in your command. One way to do this is to break your command into sub targets, e.g. translate single dash to underscore, then two underscores to single dash:

$ rename - _ *.mkv
$ rename __ - *.mkv

A less roundabout way to do this is to actually use the getopt behavior

$ rename -- -- - *.mkv
Doubledecker answered 16/2, 2014 at 7:33 Comment(0)
D
1

The rename from util-linux is in /usr/bin/rename.ul in Ubuntu. So you could use the rename.ul command.

Donell answered 23/4, 2021 at 8:30 Comment(0)
P
0
mkdir TEMP
numbers=2011
find . -name "*.mkv" | while read filename
do
  echo mv "$filename" "TEMP/somethingelse_"$((numbers++)).mkv
  rm filename
done
Persia answered 16/2, 2014 at 6:49 Comment(1)
this script does not utilize the util-linux version of rename.Heppman

© 2022 - 2024 — McMap. All rights reserved.