Home > Back-end >  Find desired attributes of a file using awk
Find desired attributes of a file using awk

Time:12-21

If i am looking for certain attributes on a file, e.g. "r-x" for user, a regular expression like '^-r.x' will do the job. But, what if i have to show files that user and others have the same rights for "write" attribute? Ok, what i though was to pass to awk the result of stat command:

stat --format="%a %n" filename (permissions in octal name)

an then use the script:

others=$1 % 10;

group = ($1 % 100 - others)/10;

owner = int($1 / 100);

to get the permissions for each one and then

switch (owner) {

case 7: case 6: case 3: case 2:

if (other!=1 || other !=4 || other!=5)

print $2;

case 1: case 4: case 5: ...

I guess there must be a way more simplest way, though...

CodePudding user response:

I'd rather go for find:

find -type f -perm -u w,o w -o \( ! -perm -u w -a ! -perm -o w \)

Will find files b and c from:

$ls -l
-rw-r--r-- 1 john john 0 Dez 19 16:13 a
-r--r--r-- 1 john john 0 Dez 19 16:13 b
-rw-r--rw- 1 john john 0 Dez 19 16:13 c

Be careful with the logics though: the seemingly simpler

\( ! -perm -u w,o w \)

for matching files without write permissions for user and others would match both match a AND b.

  • Related