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

4 comments:

Raaaj said...

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

Patrick Co Eban said...


Networking Projects for Final Year CSE Students


The IEEE Network projects Networking Projects for Final Year CSE Students has direct impact on undergraduate and graduate student education and training. Final Year Engineering Students who are software developers can structure a project around building a network firewall application Final Year Project Centers in Chennai

JavaScript Training in Chennai

JavaScript Training in Chennai

preethi minion said...

good one....

afghanistan hosting
angola hosting
afghanistan web hosting
bahrain web hosting
belize web hosting
india shared web hosting

talliewachter said...

Casino - Dr. MGM D.C.
M 하남 출장샵 life Rewards - M life Rewards - M life Rewards. Dr. MGM D.C. offers 영천 출장안마 a wide 광주광역 출장마사지 variety of 과천 출장마사지 casino games, including 동해 출장안마 slots, poker,