Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I list (using ls) all files that are not empty (size > 0) using linux?

share|improve this question
up vote 20 down vote accepted

I'd use find dirname -not -empty -ls, assuming GNU find.

share|improve this answer
Anyone care to explain the downvote? – Daenyth Sep 24 '10 at 15:48
Probably because the asker asked for ls and you used find ;) I upped though... It's a proper solution – BloodPhilia Oct 3 '10 at 10:10

This is a job for find ls is not powerful enough.

find -maxdepth 1 -size +0 -print

-maxdepth 1 - this tells find to search the current dir only, remove to look in all sub dirs or change the number to go down 2, 3 or more levels.

-size +0 this tells find to look for files with size larger than 0 bytes. 0 can be changed to any size you would want.

-print tells find to print out the full path to the file it finds

Late addition: You should probably also add the -type f switch above. This tells find to only find files. And as noted in comments below, the -print switch is not really needed.

share|improve this answer
To avoid a warning you should place -maxdepth 1 before -size +0. Also -print is the default action, so it's not needed. – cYrus Sep 23 '10 at 13:46
@cYrus - No warnings for me (cygwin) – Nifle Sep 23 '10 at 16:02
Implementations of find vary a lot in terms of what valid options are and where they can go. GNU find (which is awfully common) will produce a warning if you put -size before -maxdepth. – Telemachus Oct 2 '10 at 21:12
@Tele - Good to know, thanks. – Nifle Oct 2 '10 at 21:13

find dirname -type f ! -empty

share|improve this answer
+1: more user friendly. But this way it prints not empty directories too, add: -type f. – cYrus Sep 23 '10 at 14:10
Edited, thanks :) – Joril Sep 23 '10 at 15:47

Ls has almost no option to filter files: that's not its job. Filtering files is the job of the shell for simple cases (through globbing) and the job of find for complex cases.

In zsh, you can the L globbing qualifier to retain only files whose size is >0 (the . qualifier restricts to regular files):

ls *(.L+0)

Users of other shells must use find. With GNU find (as found mostly on Linux):

find -maxdepth 1 -type f ! -empty -exec ls {} +

A POSIX-compliant way is:

find . -type f -size +0c -exec ls {} + -o -name . -o -prune

If ls wasn't just an example and you merely intend visual inspection, you could sort by size: ls -S.

share|improve this answer
ls -l | awk '{if ($5 != 0) print $8}'

If you are intent on using ls, you need a little help from awk

share|improve this answer
 $ find /* -type f ! -size 0

will work better if you want all non empty files, rather than just directories.

share|improve this answer

Bash 4.0+

shopt -s globstar
shopt -s nullglob
for file in **/*; do  test -f "$file" && [[ -s "$file" ]] && echo "$file"; done
share|improve this answer

You must log in to answer this question.

protected by Nifle Mar 26 '15 at 19:53

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged .