linuxshelllswcone-liner

Counting all characters except space in the using the ls|wc-c?


Suppose running ls command gives the following output on the terminal:

1.txt 2.txt 3.txt '4 b'

['4 b' is the name of a folder]

When I run ls | wc -c at the same location, I get 18, which is because it considers space as a character. What should I do to avoid counting spaces as characters(even the spaces within the filenames)?


Solution

  • You could delete all spaces:

    ls | tr -d ' ' | wc -c