Martin Martin - 1 year ago 62
Bash Question

Shell: Delete if doesn't exist

I want to check all JPGs in a directory (rf). If no WEBP file with the same filename exists > delete the JPG file.


  • /a/1.jpg (ok)

  • /a/1.webp

  • /x/2.jpg (del)

  • /x/3.jpg (ok)

  • /x/3.webp

Answer Source

I won't give you the full script so you have a chance to figure it out and learn something. Here's how to chop off and append strings:

if ! test -f "$webp"; then rm -f -- "$jpg"; fi
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download