How to see the list of files which exists with nonzero bytes

Hi,

I would like to see the file list which contains the data.

Ex : - When I enter the "ls -ltr" command it will show all the files which are exists in the directory, But I want only those files list which are non zero byte by using only ls command.

I knew that I can write a script with "-s" option through script but I want the list through ls command...

For better understanding I am posting with example : -

$ls -ltr
-rw-r--r-- 1 userID MyGroup 0 Mar 2 10:27 a.txt
-rw-r--r-- 1 userID MyGroup 0 Mar 2 10:27 b.txt
-rw-r--r-- 1 userID MyGroup 50 Mar 2 10:27 c.txt
-rw-r--r-- 1 userID MyGroup 0 Mar 2 10:27 d.txt
-rw-r--r-- 1 userID MyGroup 200 Mar 2 11:24 e.txt
-rw-r--r-- 1 userID MyGroup 0 Mar 4 03:14 f.txt
-rw-r--r-- 1 userID MyGroup 100 Mar 4 03:14 g.txt
-rw-r--r-- 1 userID MyGroup 0 Mar 4 03:14 h.txt
-rw-r--r-- 1 userID MyGroup 0 Mar 4 03:14 i.txt

I want to see only c.txt, e.txt and g.txt from the above list, not all.

Please advice me how to solve this.

---------- Post updated at 05:17 AM ---------- Previous update was at 05:15 AM ----------

I don't want to delete the 0 byte files, I want to list out only those files which caontains data.

Try this,

ls -lrt | sed -r "s/(.+\s+.+\s+.+\s+.+\s+0\s+.+)//g"

When I try the below command.....I am getting the "illegal option" meesage.

$  ls -lrt | sed -r "s/(.+\s+.+\s+.+\s+.+\s+0\s+.+)//g"
sed: illegal option -- r
$

I am working in linux system which has the version 2.6.26-2-686.The '-r' option in sed is available in this .

See the following command.

ls -lrt | egrep -v "(.*) (.*) (.*) 0 (.*) (.*) (.*)"

or

ls -lrt | egrep -v "(.*?) (.*?) (.*?) 0 (.*?) (.*?) (.*?)"

Thank you, I am getting the results what I am looking for.

But Could you please explain me how it works....It would be very great....

egrep is equal to grep -E

Using regular expression I matched the pattern.
-v option is used to invert match options.

( ) = grouping in regular expression.

For More information refer the following link.
Regular Expressions in grep

also you might be interesting in this simple solution

ls -ltr | awk '{if ($5!=0) print $0}'

if 5th column is not 0 then print whole line.

Hi.

With an awk filter:

ls -ltr | awk '$5 != 0'

which uses an arithmetic comparison.

The awk processor implicitly reads lines and breaks them apart into fields that are numbered $1, $2, etc. One can then check the content of these fields in a number of ways, one being arithmetically.

So the above code means print each line where field 5 is not zero. Many awk scripts are complex, but many are simple, and often are easily constructed when dealing with operations that are naturally numeric. Regular expressions may still be used, but that's a different story.

There are controls that can change the settings for what characters separate the fields, etc., but the defaults are usually acceptable for many cases.

For the data posted, this produced:

-rw-r--r-- 1 userID MyGroup 50 Mar 2 10:27 c.txt
-rw-r--r-- 1 userID MyGroup 200 Mar 2 11:24 e.txt
-rw-r--r-- 1 userID MyGroup 100 Mar 4 03:14 g.txt

Best wishes ... cheers, drl

(The columns in the above output do not line up because the data was not originally posted within CODE tags.)

It is the same solution as mine :slight_smile: but written in shorter form (not very explaining for awk begginer)

It could be also written like this:

ls -ltr | awk \$5

With zsh:

ls -ltr  *(^L0)
find . -size +0 -exec ls -lctr {} \;

And if the order is irrelevant:

find . -size 0 -ls