Brett
Brett

Reputation: 43

Linux batch rename

I am trying to batch rename multiple files and so far I am pretty close to what I am trying to achieve. I have some files called "website.txt", "website1.txt", "website2.txt", "website3.txt" and I am trying to rename only the files that have a number associated with them (so excluding "website.txt"). My first attempt is as follows (I'm using -n for testing):

rename -n 's/website/website_edit/' *txt

Result:

rename(website1.txt, website_edit1.txt)
rename(website2.txt, website_edit2.txt)
rename(website3.txt, website_edit3.txt)
rename(website.txt, website_edit.txt)

As you can see this almost works but it is renaming the "website.txt" file as well which I don't want. So to try and remove it I did this:

rename -n 's/website\w/website_edit/' *txt

Result:

rename(website1.txt, website_edit.txt)
rename(website2.txt, website_edit.txt)
rename(website3.txt, website_edit.txt)

This time it did remove "website.txt" from the list but it also removed the the numbers from the end on the new names. I have also tried messing around with some regular expressions as well but to no avail.

Upvotes: 0

Views: 182

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185560

Try this :

rename -n 's/website(\d+)/website_edit$1/' *txt
                    ____              __
                     ^                ^
                     |                |
capturing at least one digit      captured group

Upvotes: 2

Related Questions