Count number of characters per listed filename

lswc

Given the following ls result:

$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

This will give the total number of characters for all files:

$ ls | wc -m
67

But how can I have calculate the number of characters per file name? For the same file list the result I'm after would be something like this:

8
10
10
6
etc...

Best Answer

This can be done in a very simple shell script:

for file in *; do echo -n "$file" | wc -m; done

Just loop through each file echoing the name to wc. The -n on the echo is so that it doesn't append a newline, which would erroneously increase the count by 1.