1

I am trying to run through a directory and ensure that root it not the owner of any of the files. With awk, I am having an issue where comparing a field to a string does not yield an accurate result.

ls -d -l /path/to/file/**/* | awk '{ if ( "$3" == "root" ); print "File has wrong permissions: " $9;}'

What is generated is a list of files, and when double checked, whose owner and groups are not root.

-rw-rw-r-- 1 foobar foobar 78828 Dec 4 11:15 /p/t/f/xyz/abc.jpg

Ideally, the end result would be something like:

ls -d -l /path/to/file/**/* | awk '{ if ("$3" != "deploy" || "$4" != "deploy") [...]

What is wrong?

Thank you.

1
  • 1
    awk is not like shell: replace "$3" != "deploy" with $3 != "deploy" Commented Dec 4, 2015 at 21:33

2 Answers 2

1

The problem in your statement is the ; after the if-condition. This means the if-block is empty.
Remove the ; or better put the if-block in {...} and everything will work as you expect it. Moreover, you should not put fields in "..." ("$3" -> $3)

ls -d -l / | awk '{ if ( $3 == "root" ) print "File has wrong permissions: "$9;}'

or

ls -d -l / | awk '{ if ( $3 == "root" ){print "File has wrong permissions: " $9}}'
Sign up to request clarification or add additional context in comments.

1 Comment

I knew had to be something small. Thanks!
0

You don't want to put $3 in quotes. If you do it becomes a string. In addition, you don't want a semicolon after the if condition - if you have it, then the if does nothing. It's likely you want to make those two changes:

ls -d -l /path/to/file/**/* | awk '{ if ( $3 == "root" ) print "File has wrong permissions: " $9;}'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.