How would I get awk to output the whole file name in ls -l
if some of the files have spaces in them. Usually, I can run this command:
ls -l | awk '{print $9}'
That doesn't work if the files have spaces in them. Is it possible to print $9, $10, $11 etc as well somehow?
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With