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
link|improve this question

feedback

2 Answers

up vote 3 down vote accepted
$> 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
link|improve this answer
Great, I was trying to get the suffix cutting working in -exec with find but this way is easier. – Paul Dec 2 '11 at 8:36
feedback

This one-liner can help

find . -name "*" -size 0 -exec rm {} \;

Test:

[jaypal:~/Temp/Test] ls -lrt
total 0
-rw-r--r--  1 jaypalsingh  staff  0  1 Dec 19:51 File1.txt
-rw-r--r--  1 jaypalsingh  staff  0  1 Dec 19:51 Foo.bar
-rw-r--r--  1 jaypalsingh  staff  0  1 Dec 19:51 bar.foo
-rw-r--r--  1 jaypalsingh  staff  0  1 Dec 19:52 foo.foo
[jaypal:~/Temp/Test] find . -name "*" -size 0 -exec rm {} \;
[jaypal:~/Temp/Test] ls -lrt
[jaypal:~/Temp/Test] 

If you just want to delete files with certain names then instead of * you can give filename*. For eg.

find . -name "filename*" -size 0 -exec rm {} \;
link|improve this answer
you are deleting all files. OP wants to delete all files which have a common pattern, if one of these files has size 0. – akira Dec 2 '11 at 8:29
Thanks Jaypal, the question requires the deletion of additional files other than the zero length ones. – Paul Dec 2 '11 at 8:30
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.