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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityIf 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
${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
sw
. Good to point out in general though.
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).
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
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.
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.