I have many files in a directory, and I want to remove all but one of the files that have the same prefix. For example, I have the files with the pattern filename.__<random_string>.pdf
, (filename can be any string of some length)
foo.__.pdf
foo.__resume.pdf
foo.__name.pdf
bar.__.pdf
bar.__resume.pdf
bar.__name.pdf
Now from them I only want one of the three files which have the same prefix, i.e, I only want either of the first three files and either one of the last three. For example, the directory should contain,
foo.__.pdf
bar.__.pdf
Answer with any of the scripting language or shell is accepted.
rm *.__?*
? The?
forces at least one character to be there. Of course, that only works, if there is afoo.__.pdf
for everyfoo
.