0

I have a large directory of files where there are duplicate filenames but different extensions. What I would like to do is remove the duplicates but have a priority order of which extension gets kept.

So, for example, if I had 4 file types *.aaa, *.bbb *.ccc, *.ddd I would want to only keep *.ddd if there was no other file with this name, keep *.ccc if there was no *.aaa or *.bbb, keep *.bbb if there is no *.aaa and always keep *.aaa.

Is there a way to do this?

1 Answer 1

1

With zsh, you could do:

#! /bin/zsh -
ext=(aaa bbb ccc ddd) # in order of preference.
files=( *.$^ext(ND^/) )
typeset -A seen=()
for f ($files) if (( seen[\$f:r]++ )) echo rm -f -- $f

(remove the echo if happy).

3
  • Thanks but that doesn't work. I can see lots of ".aaa" files being deleted. In that example it shouldn't delete any ".aaa" files as they are the top priority.
    – TimR75
    Commented Jul 11, 2022 at 16:23
  • @TimR75, I can't reproduce and I can't see how it could delete *.aaa files as they are listed first, so seen[their-root-name] can only be 0 at the time they're processed. Only way I can imagine it deleting higher priority ones is if there are extensions that overlap in your actual list of extensions as in ext=(tar.gz gz) where anything.tar.gz would be included twice. Commented Jul 11, 2022 at 18:24
  • Sorry it did work. I was looking in the wrong folder. Really appreciate the help. Thanks.
    – TimR75
    Commented Jul 11, 2022 at 20:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .