user3426358 user3426358 - 3 months ago 17
Bash Question

Remove multiple spaces in ls -l output

I need to display the filesize and the filename. Like this:

4.0K Desktop

I'm extracting these two fields using cut from the ls -l output:

ls -lhS | cut -d' ' -f5,9

Due to multiple spaces in the
ls -l
output, I'm getting a few erroneous outputs, like:

4.0K 19:54
4.0K 19:55

How should I fix this?

I need to accomplish this task using pipes only and no bash scripting ( output could be multiple pipes ) and preferably no sed, awk.

If no alternative to sed or awk is available- use of sed is OK.


You can use translate character command before using cut.

ls -lhS | tr -s ' ' | cut -d' ' -f 5,9