Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unix KSH script - how to find empty files in a directory

Status
Not open for further replies.

HHG

Technical User
Nov 8, 2003
68
GB
Hi all,

Can anyone help. I have a set of files held in a directory some of the files are empty. What I want to do is generate a list from that directory excluding the empty files.

Any clues how I can achieve this please.

Thanks in advance for your help.
 
What you want is this page where you will find the -s flag.

Then
Code:
for i in *
do
  [[ -s $i ]] || echo $i is empty
done

On the internet no one knows you're a dog

Columb Healy
 
find . -type f -size +0 # though this will traverse sub dirs
 
Thanks all.

each time it find an empty file I will want to do a mv so I think I will need to do columb's way. There will be no sub directories but thanks its useful to know the find command way.

 
You could try
find . -type f -size +0 -exec mv {} newdir \; or for more control of the new file name something like
find . -type f -size +0 | xargs -I {} mv {} {}.old \;
 
Bit of a misread. From above, if you want to act on a file of zero bytes, then it's size 0, ot size +0 !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top