Link to home
Start Free TrialLog in
Avatar of ehout
ehout

asked on

How to list files based on sizes?

Hi,

For you experts I guess it's a small thing, for me as newbie it's a puzzle.

I need a script (Bourne shell) which lists all files in a directory which are not 0 bytes.

So if the directory /tmp has the files
file1.dat   0Kb
file2.dat   3Kb
file3.dat   0Kb
file4.dat   1Kb
file5.dat   6Kb

I expect a string back like "/tmp/file2.dat /tmp/file4.dat /tmp/file5.dat"
And it would be nice if I could pass the directory as input parameter (or via variable)

Can anyone help?
Kind regards
Avatar of sunnycoder
sunnycoder
Flag of India image

Hi ehout,

find <dir> type -f -size +0

remove type -f part if you entries other than files to be listed too

Sunnycoder
Avatar of sri_prasanth
sri_prasanth

hiiii,
ls with -S option will give the  files  in the order of their sizes..................
if u want this in a sorted order then simply give this command

x=`ls -S /tmp`;
echo $x;

Now this x has the names of the files in sorted order of their sizes..................
Avatar of ehout

ASKER

Hi,

Thanx for the quick responses.
The sort order is not relevant. But the output of the function,(BTW, type -f seems to be written as -type f ) Must appear on 1 line. Can I catch it in a variable?
>I could pass the directory as input parameter (or via variable)

#say dir is ur variable which has the directory name then

x=`ls -S $dir`;
echo $x;

This would do that..............
var=`find <dir> -type f -size +0`
echo $var

sorry for the typo
Avatar of ehout

ASKER

Hai sri,

your option seems helpful,
However It lists all the files. But it should omit files of 0 bytes.

If there is a rm option that first will erase all o byte files, that's OK too.

Avatar of ehout

ASKER

Hai sunny,

Thanx for your input too, I'm learning a lot.

Your option actually lists what I want, but can I get these listed files on 1 line?

Reason is I have a certain mailscript and This way I can give multiple files as attachment.

ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ehout

ASKER

Wait... I was to quick... Your script does a terrific thing.
Avatar of ehout

ASKER

luv you ;-)
Sri, Thanks too for your effort, but sunny's thing was exactly what I needed.

Kind regards