Sunday, April 10, 2011

How to find empty files in UNIX?

There are multiple ways to find whether a file is empty or not. Please see few examples below,

volcano@volcano-laptop:~/shellscript$ ls -ltr | grep '\<0\>'
-rw-r--r-- 1 volcano volcano    0 2011-03-12 15:41 sujay
-rw-r--r-- 1 volcano volcano    0 2011-03-12 15:42 jsk
-rw-r--r-- 1 volcano volcano    0 2011-03-12 15:45 a
-rw-r--r-- 1 volcano volcano    0 2011-03-12 15:46 b
-rw-r--r-- 1 volcano volcano    0 2011-04-10 22:30 emptyfile

volcano@volcano-laptop:~/shellscript$ ls -ltr | awk ' $5==0 {print}'
-rw-r--r-- 1 volcano volcano    0 2011-03-12 15:41 sujay
-rw-r--r-- 1 volcano volcano    0 2011-03-12 15:42 jsk
-rw-r--r-- 1 volcano volcano    0 2011-03-12 15:45 a
-rw-r--r-- 1 volcano volcano    0 2011-03-12 15:46 b
-rw-r--r-- 1 volcano volcano    0 2011-04-10 22:30 emptyfile

volcano@volcano-laptop:~/shellscript$ for fname in `ls`;do if [ ! -s $fname ];then ls -l $fname;fi;done
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:45 a
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:46 b
-rw-r--r-- 1 volcano volcano 0 2011-04-10 22:30 emptyfile
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:42 jsk
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:41 sujay

volcano@volcano-laptop:~/shellscript$ find . -maxdepth 1 -size 0 -ls
 58438    0 -rw-r--r--   1 volcano  volcano         0 Mar 12 15:42 ./jsk
 58434    0 -rw-r--r--   1 volcano  volcano         0 Mar 12 15:46 ./b
 58437    0 -rw-r--r--   1 volcano  volcano         0 Mar 12 15:41 ./sujay
 58211    0 -rw-r--r--   1 volcano  volcano         0 Mar 12 15:45 ./a
 58234    0 -rw-r--r--   1 volcano  volcano         0 Apr 10 22:30 ./emptyfile

1 comment:

BharathKumarRaju Dasararaju said...

very good to see all possibilities in once... as usual you rockz