Sunday, September 4, 2022

[SOLVED] Retain original suffix in piped command chain

Issue

I have a script that searches for files that have duplicate main part of filename:

E.g. "IMAGE123.MOV" and "IMAGE123.JPG".

The script looks like this:

find . -iname "*.mov" -o -iname "*.jpg" | cut -d'.' -f2 | uniq -d |uniq | awk '{print "."$1".mov"}'

I could pipe this to "rm" with xargs to delete the files. But the issue is that I do not know if the file had ".MOV" or ".mov" suffix. I use awk to concatenate lowercase - but I am not sure if that was correct. This information gets lost during the command chain here.

Any ideas?


Solution

I think it's easier to find *.mov and test if there is a corresponding *.jpg. Here is how I'd do it:

find . -name '*.[Mm][Oo][Vv]' -exec sh -c '
for mov; do
  for jpg in "${mov%.*}".[Jj][Pp][Gg]; do
    if test -f "$jpg"; then
      echo rm "$mov"
    fi
    break
  done
done' sh {} +

Remove echo if the output looks good.



Answered By - oguz ismail
Answer Checked By - Pedro (WPSolving Volunteer)