LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Ubuntu (https://www.linuxquestions.org/questions/ubuntu-63/)
-   -   Difficulty with rename (https://www.linuxquestions.org/questions/ubuntu-63/difficulty-with-rename-4175588879/)

Fixit7 09-06-2016 05:46 PM

Difficulty with rename
 
I am having trouble with using rename.

Trying to remove spaces and dashes from file names.

I thought all I had to do was add /char to be deleted/

rename "s/ //-//g" *

Turbocapitalist 09-07-2016 01:58 AM

Quote:

Originally Posted by Fixit7 (Post 5601638)
I am having trouble with using rename.

Trying to remove spaces and dashes from file names.

I thought all I had to do was add /char to be deleted/

rename "s/ //-//g" *

There are two important things about "rename". The first is that you can run it with the -n option to test your patterns before actually applying them. The other is that the part inside the quotes can be any perl expresssions at all. You could even write a short program or routine inside the quotes and it would work.

But the part you are looking for is the substitution operator. The details are found in the manual page perlop
The dividers are usually slashes ( / ) but can be pretty much any character that is repeated three times.

Code:

s/old/new/
s/old//

The first case replaces "old" with "new" whereas the second case simply deletes "old" by replacing it with nothing.

So you are looking for any character in a set consisting of a space or a dash. That's written like [ -]
with the square braces delimiting the set. And, I presume, you want to zap every last occurrence in the file name. So you'll need the g modifier, to tell it to keep matching through the end of the string (file name).

Code:

rename -n 's/[ -]//g' ./*
When you have the results you wish, remove the -n

If you want to see more of what "rename" can do, look up some material on perl regular expressions. They are quite useful.


All times are GMT -5. The time now is 06:36 PM.