rename command and extended regular expressions

This forum is dedicated to basic help and support :

Ask here your questions about basic installation and usage of Mageia. For example you may post here all your questions about getting Mageia isos and installing it, configuring your printer, using your word processor etc.

Try to ask your questions in the right sub-forum with as much details as you can gather. the more precise the question will be, the more likely you are to get a useful answer

rename command and extended regular expressions

Postby ricard13 » May 16th, '23, 14:07

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.
ricard13
 
Posts: 17
Joined: Feb 22nd, '20, 13:58

Re: rename command and extended regular expressions

Postby doktor5000 » May 16th, '23, 17:25

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?
Cauldron is not for the faint of heart!
Caution: Hot, bubbling magic inside. May explode or cook your kittens!
----
Disclaimer: Beware of allergic reactions in answer to unconstructive complaint-type posts
User avatar
doktor5000
 
Posts: 18042
Joined: Jun 4th, '11, 10:10
Location: Leipzig, Germany

Re: rename command and extended regular expressions

Postby ricard13 » May 16th, '23, 19:39

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.
ricard13
 
Posts: 17
Joined: Feb 22nd, '20, 13:58

Re: rename command and extended regular expressions

Postby doktor5000 » May 16th, '23, 21:23

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.
Cauldron is not for the faint of heart!
Caution: Hot, bubbling magic inside. May explode or cook your kittens!
----
Disclaimer: Beware of allergic reactions in answer to unconstructive complaint-type posts
User avatar
doktor5000
 
Posts: 18042
Joined: Jun 4th, '11, 10:10
Location: Leipzig, Germany

Re: rename command and extended regular expressions

Postby ricard13 » May 17th, '23, 10:05

Thank you so much. I see that it is the basic one and cannot be used for extended expressions. I'll try krename.
ricard13
 
Posts: 17
Joined: Feb 22nd, '20, 13:58


Return to Basic support

Who is online

Users browsing this forum: No registered users and 1 guest

cron