Page 1 of 1

rename command and extended regular expressions

PostPosted: May 16th, '23, 14:07
by ricard13
Hello, I would need help with the rename command and extended regular expressions. I'm not sure if they are supported in Mageia, when I use them in one of these 2 modes they don't work:

find -iname "*" -exec rename -o 'y/[^a-zA-Z0-9]/_/g' {} \;
OR
rename -o 'y/[^a-zA-Z0-9]/_/g' *

does not give any error but does not perform any task

I don't know if I left some command or it is not supported by rename in Mageia.

Re: rename command and extended regular expressions

PostPosted: May 16th, '23, 17:25
by doktor5000
Hi there, you should probably describe what you actually want to rename to what, with an example. Otherwise it's hard to tell.

You want to replace everything that is not an alphanumeric character with an underscore with that regex, or what's the intent?

Re: rename command and extended regular expressions

PostPosted: May 16th, '23, 19:39
by ricard13
410 / 5.000
The intent is to remove all characters other than letters, numbers, underscores, or parentheses.
Rename works very well for me if I only change some set of letters like for example "rename elec cor *" but when using extended expressions to rename as in

find -iname "*" -exec rename -o 'y/[^a-zA-Z0-9]/_/g' {} \;

does not perform the task exits without making any changes to the filenames.

Re: rename command and extended regular expressions

PostPosted: May 16th, '23, 21:23
by doktor5000
ricard13 wrote:The intent is to remove all characters other than letters, numbers, underscores, or parentheses.

That's not what your regex would do, even if "y" in front would be a normal operator.
What you probably want is
Code: Select all
rename -o 's/[^a-zA-Z0-9_()]//g'

And you want to make sure that works on one file as expected, before using it with find.

Although I don't know what the default rename is that we ship, if it's the perl one or the regular GNU util-linux one.

edit: AFAICT it's the basic util-linux one when you're referring to /usr/bin/rename - that one does not support regexes at all,
it only supports basic expressions and only a different syntax, not the perl one that you want to use. It also only does the first occurence.

SYNOPSIS
rename [options] expression replacement file...
DESCRIPTION
rename will rename the specified files by replacing the first
occurrence of expression
in their name by replacement
.
[...]


Maybe you want to try krename: https://userbase.kde.org/KRename - it has a find & replace mode and also supports regexes, although the Qt ones, not standard PCRE.

Re: rename command and extended regular expressions

PostPosted: May 17th, '23, 10:05
by ricard13
Thank you so much. I see that it is the basic one and cannot be used for extended expressions. I'll try krename.