If the trailing number is a single digit, and they are all .pem
files, you could use parameter expansion to strip the digit and extension, then replace the extension:
for file in cert1.pem chain1.pem fullchain1.pem privkey1.pem
do
mv "$file" "${file%%?.pem}.pem"
done
The parameter expansion works by removing the longest trailing match (%%
) of any single character (?
) followed by .pem
; after that is done, we manually add back the .pem
at the end.
Note that this would also rename files like certX.pem
or fullchain-.pem
, because the ?
character expands to any single character.
This is made safer in the above example because I've manually listed the files to rename. You could alternatively use something like:
for file in *1.pem
do
mv "$file" "${file%%?.pem}.pem"
done
... which loops explicitly over files with 1.pem
at the end of their names.
Setup:
$ ls
cert1.pem chain1.pem fullchain1.pem privkey1.pem
Execution:
$ for file in cert1.pem chain1.pem fullchain1.pem privkey1.pem
do
mv "$file" "${file%%?.pem}.pem"
done
Result:
$ ls
cert.pem chain.pem fullchain.pem privkey.pem