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?
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?
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
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...
– Daniel Pittman
Mar 02 '12 at 18:19
rename
.
– Gilles 'SO- stop being evil'
Mar 02 '12 at 23:03
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.
–
Mar 02 '12 at 18:45
${file/sw/sw.}"
also matches files likeFOOsw001
. 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 02 '12 at 19:16sw
. Good to point out in general though. – Kevin Mar 02 '12 at 19:20