PDA

View Full Version : [SOLVED] Why does ls -l and find come up with different counts?



saphil
November 23rd, 2011, 08:29 PM
There must be some reason why the counts come out so different. Grep is counting lines with "-c," right? Does this mean that there are a number of lines with more than one instance of "*.jp2" in them?



$ ls -R| grep jp2 -c
96674
$ find -name "*.jp2" | wc -w
1308547

San_SS!
November 23rd, 2011, 08:39 PM
Two things:
* Do you happen to have spaces in your paths? Cause in that case wc -w counts the number of words, which is different from the number of lines. To solve that use wc -l instead of wc -w
* With the grep you're only filtering the ones that have jp2 in the name not that end with that. you should use a regex with grep by giving it the -e argument

Hope this helps!

Bachstelze
November 23rd, 2011, 08:40 PM
How about looking at the output of the commands and find out what they do differently?

saphil
November 23rd, 2011, 08:49 PM
How about looking at the output of the commands and find out what they do differently?

Thanks, this makes sense, but...
These are very large directories and even an output of 90000 lines is hard to get through.
Would it make sense to redirect the 2 outputs to a pair of files and run diff on them?

saphil
November 23rd, 2011, 08:52 PM
Two things:
* Do you happen to have spaces in your paths? Cause in that case wc -w counts the number of words, which is different from the number of lines. To solve that use wc -l instead of wc -w
* With the grep you're only filtering the ones that have jp2 in the name not that end with that. you should use a regex with grep by giving it the -e argument

Hope this helps!

It did help. I have no idea where the million+ jp2 instances are, but counting lines is far more useful to me, and it also comes to the same answer as ls -l. (I know that doesn't make it true, but it improves the probability of truth.)

sisco311
November 23rd, 2011, 08:57 PM
Check out BashFAQ #4 (link in my signature) and http://mywiki.wooledge.org/ParsingLs

Bachstelze
November 23rd, 2011, 09:30 PM
Thanks, this makes sense, but...
These are very large directories and even an output of 90000 lines is hard to get through.
Would it make sense to redirect the 2 outputs to a pair of files and run diff on them?

Yes, or you can pipe them to less for example. the difference should be obvious by just looking at them.

mikejonesey
November 23rd, 2011, 10:08 PM
$ ls -R| grep jp2 -c
96674
$ find -name "*.jp2" | wc -w
1308547


to make the ls the same as the find it would have to be;
ls -R | grep -c ".*\.jp2$"

(the extra .* begins with anything, will match the begging can be omited in your case)
(the \. ensures a dot as in find statment)
(the dollar means ends with as in find statment)

to make the find the same as the ls line it would have to be;
find -name "*.jp2*"

(the extra star to say may not end in...)

other differences can be found in the stdout of these commands ie;
mkdir dir dir2
touch dir2/afile
ls -R | grep -c "dir" = 4
find . | grep -c "dir" = 3

finaly as find prints a new line per file you would also want to use the -l instead of -w (a file or directory may have one or more words).

the final command to count;


find . -type f "*.jp2" | wc -l

sisco311
November 24th, 2011, 09:44 AM
the final command to count;


find . -type f "*.jp2" | wc -l

Nope.



[sisco@acme xtmp]$ mkdir foo
[sisco@acme xtmp]$ cd foo
[sisco@acme foo]$ > "file
> name"
[sisco@acme foo]$ ls -1
file?name
[sisco@acme foo]$ find ./ -type f | wc -l
2

mikejonesey
November 24th, 2011, 02:51 PM
filename with a line break, lol, will have a look in a mo... :P

saphil
November 24th, 2011, 09:58 PM
The filename with a line-break is interesting.
autocomplete doesn't like it, but if I enter
vi file?name it opens for editing and adds lines to the file.