Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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

I want to grep the output of my ls -l command:

-rw-r--r--   1 root root       1866 Feb 14 07:47 rahmu.file
-rw-r--r--   1 rahmu user     95653 Feb 14 07:47 foo.file
-rw-r--r--   1 rahmu user   1073822 Feb 14 21:01 bar.file

I want to run grep rahmu on column $3 only, so the output of my grep command should look like this:

-rw-r--r--   1 rahmu user     95653 Feb 14 07:47 foo.file
-rw-r--r--   1 rahmu user   1073822 Feb 14 21:01 bar.file

What's the simplest way to do it? The answer must be portable across many Unices, preferably focusing on Linux and Solaris.

NB: I'm not looking for a way to find all the files belonging to a given user. This example was only given to make my question clearer.

share|improve this question
Note that parsing the output of ls is inherently fragile (think what happens if a user name contains whitespace — and yes, this happens on some platforms). Use find instead. – Gilles Feb 16 '12 at 1:47
up vote 21 down vote accepted

One more time awk saves the day!

Here's a straightforward way to do it, with a relatively simple syntax:

ls -l | awk '{if ($3 == "rahmu") print $0;}'

or even simpler: (Thanks to Peter.O in the comments)

ls -l | awk '$3 == "rahmu"' 
share|improve this answer
Or: ls -l | awk '$3=="rahmu"' ... and by the way, the opening ( bracket should be after if ... awk '{if($3 ...' – Peter.O Feb 15 '12 at 15:11
Yes thank you, I corrected that. – rahmu Feb 15 '12 at 15:34
@Peter.O +1 from me. Your command is more succinct, correct, since print is the default for an awk match. – bsd Feb 15 '12 at 16:06
To avoid localization issues (date format affects column order) it is often suggested to ensure standard format and language: LANG=C ls -l | awk ... – fheub Feb 16 '12 at 10:26

If by column, you mean fixed-size column, you could:

ls -l | grep "^.\{15\}rahmu"

where ^ means the beginning of the line, . means any character and \{15\} means exactly 15 occurrences of the previous character (any character in this case).

share|improve this answer

If you are looking to match only part of the string on a given column, you can use advice from http://stackoverflow.com/questions/17001849/awk-partly-string-match-if-column-partly-matches

some_command | awk '$6 ~ /string/'
share|improve this answer

To grep based on single username column (and print only files), you can try:

ls -la | grep "^-\S\+\s\+\S\+\s\+rahmu"

For directories, change - into d, for any type remove -.

It basically selects each column by \S\+\s\+ which matches a non-space characters followed by a space characters, so we're matching three first columns and then the username.

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.