Bash Find Files with zero size and delete files with different extensions

bashfind

This command will find files of zero size:

find . -size 0

A filename returned might be

filename.001

I am looking for a one liner that will delete files found with this, plus any that have the same filename with a different extension (which would be non-zero sized), so these files would be deleted too:

filename.txt
filename.bak
filename.ZZz

Best Answer

$> find . -size 0 | while read f; do rm "${f%.*}."* ; done

explanation:

  1. find all files with size 0
  2. pipe the names to the while loop
  3. cut of the suffix (extension) part ${f%.*} (read man bash)
  4. rm all other files with the same base
Related Question