Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My goal is deceptively simple (at least to me). I wish to take the output of ls -l or ls -lh and select just one field.

I am looking for this to be as bulletproof as possible, by which I mean, assume that filenames can have a variable number of spaces, not everything in the field has the same length, etc.

Bonus points for having a script that will take the name of the the field (or even just a field number), and then return the contents of the field.

I want to turn

enter image description here


enter image description here

share|improve this question
up vote 23 down vote accepted

Try ls -l | awk '{print $7}'.

awk selects columns so it's perfect for this task.

share|improve this answer
As explained by @ormaaj: Do not rely on pieces of information being available in specific places with ls. The find command is tremendously helpful. – Paddy Landau Jul 18 '12 at 10:02

Never parse ls. Use GNU find. Or if portability isn't important, stat(1).

find . -maxdepth 1 -printf '%Td\n'

For reading data other than lists of filenames line-by-line and splitting into fields, see: BashFAQ/001

There are no methods to reliably read a newline-delimited list of filenames that make sense under most circumstances.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.