Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have 2 directories as below

/appl/exe/04/dat>ls -alrt
total 290
drwxrwxr-x  30 exceed   exceed      1024 Jun 11 09:04 ..
drwxrwxrwx   2 exceed   exceed    138240 Aug 17 03:56 .

/appl/exe/13/dat>ls -alrt
total 18
drwxrwxr-x  28 exceed   exceed      1024 Jul  6 13:12 ..
drwxrwxrwx   2 exceed   exceed      7680 Aug 16 20:46 .

Why does it show a difference in the wc? What does this mean?

wc -l /appl/exe/04/dat -> shows outout as below:

   4 /appl/exe/04/dat

wc -l /appl/exe/13/dat -> shows output as below:

   0 /appl/exe/13/dat
share|improve this question

1 Answer 1

First of all, note that you are running wc -l on a directory. That command is for counting lines in text files. So wc is basically looking at whatever internal binary format your file system uses to represent directories and counting the number of times byte 0x0a appears there, which probably won't tell you anything useful.

That said, even though both directories are empty, notice that the size of 04/dat is much bigger. That means at one point it contained a lot of files that have since been deleted. But deleting files does not remove all traces from the directory blocks. Depending on the file system it might just zero out the inode or coalesce the directory entry into the previous entry of the chunk.

So there's basically a lot more left over crud in one directory than in the other, so it should be no surprise that more crud contains a few more newline characters (byte 0xa). And note that those bytes don't even necessarily have to have been part of a filename, they might have been part of an inumber.

share|improve this answer
    
I understand that I'm running wc -l on a directory. One of my scripts has been doing a wc -l on a directory(instead of a file) and returning non-zero, so the script has been successful. Recently, I had huge number of files in that directory which I deleted and since then, the "wc -l" on that directory is returning zero, so my script is failing. This brought to light that my script had an issue though. So trying to understand if deleting the huge files in that directory brought the issue to light. –  Sandra Aug 17 at 10:11
    
Well, unless you were sticking newlines in your filenames, the most likely source of newlines is from the bytes that specify inumbers. Those typically do get zeroed out when you unlink a file, so it makes sense that deleting a bunch of files would reduce the number of newlines, possibly to zero. –  user3188445 Aug 17 at 10:14
    
Thanks for the responses. I'll get back if I need more info. –  Sandra Aug 17 at 10:19
    
So basically, the wc -l 04/dat is showing non-zero because some traces of previous files exist in that directory block/inode which are not visible to us. But wc -l 13/dat is showing zero because there are no traces of any previous files and the inode is cleared out. Is my understanding correct? –  Sandra Aug 17 at 10:33
    
Basically. I wouldn't say no traces, but probably fewer traces, and no traces that contain newline characters. –  user3188445 Aug 17 at 10:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.