This problem usually occurs when ls is used with a wildcard, like this:
ls *test* | wc -l
Bash first expands the wildcard expression first, and when that list gets too large, the kernel cannot handle it (a limitation of the kernel). So to fix it, use the find command instead, like this:
find . -name "*test*" | wc -l