Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using util-linux rename command

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.

like image 907
user3179658 Avatar asked Feb 16 '14 06:02

user3179658


People also ask

How do I rename a file in Linux terminal?

To use mv to rename a file type mv, a space, the name of the file, a space, and the new name you wish the file to have. Then press Enter. You can use ls to check the file has been renamed.

How to rename a file according to regular expression in Linux?

rename command in Linux is used to rename the named files according to the regular expression perlexpr. It can change the name of the multiple files. If the user will not specify any file names on the command line with this command then it will take the file name from the standard input. Syntax: rename [options] expression replacement file...

What is the rename utility?

The rename utility is a Perl-based program that makes batch renaming simple through its advanced use of regular expressions. You can apply robust pattern matching techniques in order to rename multiple files at once.

How to rename multiple files at the same time in Linux?

Since there is no output if the command is successful, we are using the ls command to check if the name is changed: On its own, the mv command renames a single file. However, combining it with other commands allows you to rename multiple files at the same time.


1 Answers

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
like image 117
jackrabbit Avatar answered Oct 12 '22 19:10

jackrabbit