awk '{print $9}' the last ls -l column including any spaces in the file name

A better solution: Don't attempt to parse ls output in the first place.

The official wiki of the irc.freenode.org #bash channel has an explanation of why this is a Bad Idea, and what alternate approaches you can take instead: http://mywiki.wooledge.org/ParsingLs

Use of find, stat and similar tools will provide the functionality you're looking for without the pitfalls (not all of which are obvious -- some occur only when moving to platforms with different ls implementations).

For your specific example, I'm guessing that you're trying to find only files (and not directories) in your current directory; your current implementation using ls -l is buggy, as it excludes files which have +t or setuid permissions. The Right Way to implement this would be the following:

find . -maxdepth 1 -type f -printf '%f\n'

There's probably a better approach that involves combining fields somehow, but:

$ echo 1 2 3 4 5 6 7 8 9 10 11 12 13 14... | 
  awk '{for (i = 9 ; i <= NF ; i++) printf "%s ", $i}'
9 10 11 12 13 14... 

Using printf "%s " $i will print the i-th field with a space after it, instead of a newline. The for loop just says to go from field 9 to the last field.


Just for completion. It can also be done with sed:

# just an exercise in regex matching ...
ls -l | sed -E  -e '1d; s/^([^ ]+ +){8}//' 

Tags:

Bash

Awk