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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
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).
Method 3
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0