50

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

Nifle
  • 34,998
David B
  • 2,614

7 Answers7

68

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

Daenyth
  • 6,460
21

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

Edit:
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.

Nifle
  • 34,998
14
ls -l | awk '{if ($5 != 0) print $9}'

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

MaQleod
  • 13,258
10

find dirname -type f ! -empty

Joril
  • 2,035
10

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.

3
 $ find /* -type f ! -size 0

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

Telemachus
  • 7,065
Trezoid
  • 732
2

Bash 4.0+

shopt -s globstar
shopt -s nullglob
for file in **/*; do  test -f "$file" && [[ -s "$file" ]] && echo "$file"; done
user31894
  • 2,937