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
ls -laLR /home/tools > all_site1.txt

I need the state of all folders and files in subfolders of /home/tools. With this command I put all in this file. What I need now is a solution how to parse from this file all_sites1.txt only lines where owner of dir or owner of the file is root.

I tried this:

awk '/root/{print $0}' all_sites1.txt

But I get all places where root occurs. I need only those entries where owner is root and what file and/or dir is to be printed.

share|improve this question
up vote 2 down vote accepted

If you are looking to save the state of folders/files, getfacl is a much better command to use. You could do the following:

getfacl -LR /home/tools >all_site1.txt

One thing to note though is that the behaviour of the -L option is different from that of ls. It only follows symlinks to directories and not files.

You could print the output of files owned by root like this (provided none of the paths contain newlines):

awk '$2=="file:" && file_line=$0 {}
  $2=="owner:" && $3=="root" { print substr(file_line,9) }' \
share|improve this answer
Creating a pipeline from awk and cut is a bit strange, isn't it? – Hauke Laging Feb 22 '14 at 16:42
@Hauke Not really, awk has no support for selecting multiple arbitrary fields. Originally I had cut -d ' ' -f 3-, which replaces a complex awk line. cut -b isn't scuppered by leading whitespace in the filename though. But now that you mention it, I can just use substr in awk. So in this case, yes I guess it is strange. – Graeme Feb 22 '14 at 17:03

For reasonable file and directory names (not starting with space, no newlines) this works:

ls -laLR 2>/dev/null | 
    awk 'NF>8 && ($3 == "root" || $4 == "root") '\
    '{$1=""; $2=""; $3=""; $4=""; $5=""; $6=""; $7=""; $8=""; '\
    'sub("^ *",""); print;}'

As usual find is the better solution:

find . -user root -or -group root 2>/dev/null
share|improve this answer

Another solution:

shopt -s nullglob
stat -c%n:%U:%G * .*[!.]* ..?* | awk -F: '{if( $2=="root" || $3=="root")print $1}'

This will work on files in the current directory only.

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.