Solved

How to list files based on sizes?

Posted on 2004-08-05
10
208 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:ehout
  • 5
  • 3
  • 2
10 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11724315
Hi ehout,

find <dir> type -f -size +0

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

Sunnycoder
0
 

Expert Comment

by:sri_prasanth
ID: 11724376
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..................
0
 
LVL 3

Author Comment

by:ehout
ID: 11724392
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?
0
 

Expert Comment

by:sri_prasanth
ID: 11724394
>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..............
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11724402
var=`find <dir> -type f -size +0`
echo $var

sorry for the typo
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Author Comment

by:ehout
ID: 11724404
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.

0
 
LVL 3

Author Comment

by:ehout
ID: 11724413
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.

0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 300 total points
ID: 11724418
Hi ehout,

I just posted that method
var=`find <dir> -type f -size +0`
echo $var
0
 
LVL 3

Author Comment

by:ehout
ID: 11724419
Wait... I was to quick... Your script does a terrific thing.
0
 
LVL 3

Author Comment

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

Kind regards
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now