Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have many files of the form

sw001
sw002
sw003
...

I want to insert a period between the sw's and the number values. How can I accomplish this?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

On Linux:

rename 'sw' 'sw.' sw*

On Debian, Ubuntu and derivatives, use rename.ul instead of rename (rename is a different file renaming command on those distributions).

share|improve this answer
    
Whups. Wrong link. :( –  Daniel Pittman Mar 2 '12 at 18:14
    
@Daniel: What do you mean "wrong link"? –  ryran Mar 2 '12 at 18:17
    
I edited my comment - apparently there is a second utility by the name rename out there. Which is awesome. I linked to the wrong manual page, but didn't notice because they are close enough... –  Daniel Pittman Mar 2 '12 at 18:19
    
Gotcha. It's interesting that you say the perl rename is usually the default -- I've never run into it before at all. As you say, the rename I'm familiar with should be on most linux boxen, shipping with util-linux. –  ryran Mar 2 '12 at 18:47
    
I am as surprised as you are, in the opposite direction. :) –  Daniel Pittman Mar 2 '12 at 19:05

If you don't have rename and don't feel like downloading it, use this:

for file in sw*; do
    mv "$file" "${file/sw/sw.}"
done
share|improve this answer
    
Be aware that the pattern substitution ${file/sw/sw.}" also matches files like FOOsw001. Besides - this kind of substitution is not supported by all shells. Removing a prefix pattern should be more appropriate and portable: "$sw.{file#sw}" –  maxschlepzig Mar 2 '12 at 19:16
2  
@maxschlepzig I suppose it would, but because of the glob all names it encounters necessarily begin with sw. Good to point out in general though. –  Kevin Mar 2 '12 at 19:20

If you can can express the transformation as a Perl regular expression, rename that ships with Perl is a great choice. It applies a Perl expression to each filename, then changes the name if it is different. Often, a Perl regular expression substitution is what you want:

rename 's/sw/sw./' sw*

This is different from the rename(1) that ships with util-linux-ng, but normally the Perl version is the default. See man 1 rename to check which one your system has.

share|improve this answer
    
Can you help me to understand what the / notation means? –  Paul Mar 2 '12 at 18:09
    
Hopefully that edit is clearer: the man page was for a different utility with the same name, and I didn't notice because it was close enough. The Perl rename applies a Perl expression; 's///' is a regular expression substitution. –  Daniel Pittman Mar 2 '12 at 18:18
1  
If this rename expression is indeed a regular expression, the . character would mean "any one character", so it should be escaped in order to be taken literally. –  Herman Torjussen Mar 2 '12 at 18:45
1  
@hesse, it would in the match part, not in the replacement part. :) –  Daniel Pittman Mar 2 '12 at 19:05
    
The Perl version is the default in Debian and derivatives including Ubuntu. It's not shipped by other distributions. –  Gilles Mar 2 '12 at 23:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.