2
1
I have multiple files (about 1000) named as such:
abcdefg123456.xyz
abcdefg123457.xyz
abcdefg123458.xyz
abcdefg123459.xyz
Some of the files have 4 additional random numbers and letters (in any order) after the name. These are possibly duplicates, but not always, so I need to change them to the original format to verify whether they are duplicate or not. They have this format:
abcdefg123456a789.xyz
abcdefg123457b987.xyz
abcdefg123458c879.xyz
abcdefg123459d897.xyz
On occasion, there is a wrong extension as well,
abcdefg123456.xyzedf
abcdefg123456.xyzfed
I want to rename these files to the original format of abcdefg followed by the original 6 numbers - i.e. to delete the trailing 4 random numbers and letters, and to delete the trailing extension back .xyz What I have so far is this:
rename -n "s/[a-z][0-9]{6}.xyz/.xyz/g" *
But it doesn't seem to work. For some reason the output is:
abcdef.xyz (no numbers)
EDIT: I was a bit torn between which answer to choose from, because both helped in finding the solution. I went for stuts because he helped with the second part of the question as well. But your help is greatly appreciated too Mark Perryman - and the commenters as well of course.
Your main error is the use of
{6}
digits: for your examples this should be{3}
. To remove extra characters after the.xyz
you need to add.*
to the end of the match string, givingrename -n "s/[a-z][0-9]{3}\.xyz.*/.xyz/g" *
as the command (omitting the-n
when you are happy with the actions). – AFH – 2017-01-04T17:17:38.967I see. I was capturing the part I want to keep, instead of the part I want to remove. How would I delete the files if they cannot be renamed? and what if the order of numbers and letters is not exactly
...a789.xyz
,...b987.xyz
, but follow a random pattern instead:...a7b8.xyz
,...c9d7.xyz
. Thanks. – user681866 – 2017-01-04T17:29:42.223If the first of the additional characters is a letter, then
rename -n "s/[a-z][a-z0-9]{3}\.xyz.*/.xyz/g" *
will do it. If not, you cannot simply use[a-z0-9]{4}
in the match pattern, as this will remove the last four digits in the standard format files, and you will need to use match groups, as in the answers, though you could tryrename -n "s/[a-z0-9]{4}\.xyz.*/.xyz/g" ?????????????????.xyz*
(17 queries), which should process only the longer file names. Note the difference between regular expression matching and shell file expansion. – AFH – 2017-01-04T19:32:34.840My updated answer (using single quotes to allow $ to work) and the -f option to ensure that duplicate files are deleted is a neater one-line solution ;-) – Mark Perryman – 2017-01-05T12:18:22.203