2

On debian terminal, I try to delete all .jpg of a folder and subfolder if .cr2 exist on the same folder.

Assumes they have the same name. 123.jpg 123.cr2

I know how to delete all .jpg with find command.

find {PATH} -type f -name '*.jpg' -delete

but how can I add a condition (if .cr2 exist)

I found this 10y topics but it's for windows and python

tripleee
  • 175,061
  • 34
  • 275
  • 318
overer
  • 147
  • 1
  • 2
  • 10
  • 1
    Remarkably, I was unable to find a duplicate for this, even though it seems like a FAQ. Thanks for filling a gap in the knowledge base! – tripleee Oct 30 '21 at 07:51

1 Answers1

3

You can try something like:

shopt -s globstar
for i in /path/**.jpg
do
RAW=${i%.jpg}.cr2
if [ -f "$RAW" ]
then rm "$i"
fi
done

If you are fan of oneliners you can convert the script to something like:

shopt -s globstar; for i in /path/**.jpg; do [ -f "${i%.jpg}.cr2" ] && rm "$i"; done
Romeo Ninov
  • 6,538
  • 1
  • 22
  • 31