5

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?

3 Answers 3

8

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
2
  • 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}" Mar 2, 2012 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, 2012 at 19:20
5

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).

7
  • Whups. Wrong link. :( Mar 2, 2012 at 18:14
  • @Daniel: What do you mean "wrong link"?
    – rsaw
    Mar 2, 2012 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... Mar 2, 2012 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.
    – rsaw
    Mar 2, 2012 at 18:47
  • I am as surprised as you are, in the opposite direction. :) Mar 2, 2012 at 19:05
1

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.

5
  • Can you help me to understand what the / notation means?
    – Paul
    Mar 2, 2012 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. Mar 2, 2012 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.
    – user13742
    Mar 2, 2012 at 18:45
  • 1
    @hesse, it would in the match part, not in the replacement part. :) Mar 2, 2012 at 19:05
  • The Perl version is the default in Debian and derivatives including Ubuntu. It's not shipped by other distributions. Mar 2, 2012 at 23:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .