1

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)?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    Running `ls` when the output is *not* a terminal (eg, when using a pipe like this) is equivalent to running `ls -1` -- it prints each file on a line by itself, separated by newlines. You need `ls -C` to force listing in columns like this – Chris Dodd Dec 11 '21 at 21:55

2 Answers2

1

You could delete all spaces:

ls | tr -d ' ' | wc -c
tink
  • 14,342
  • 4
  • 46
  • 50
1

You can use tr to remove the spaces:

ls | tr -d ' ' | wc -c
Mureinik
  • 297,002
  • 52
  • 306
  • 350