tilefrae
tilefrae

Reputation: 163

How to check CHMOD using BASH (numeric)?

How can I check chmod in bash? How can I check that user, group... have 7 (R-W-X) ? Or 4 etc ?

#!/bin/bash  
.
.
for file in *
do

if [ $file check.... ? ]
then
     echo $file
fi

done
exit 0

Upvotes: 2

Views: 3353

Answers (2)

Karoly Horvath
Karoly Horvath

Reputation: 96258

For the most common tasks you can use the builtin [ ... ] test, man test.

This doesn't allow fine grained checking, but that's rarely what you need, usually you only care whether a file exists, readable, etc...

-r FILE
       FILE exists and read permission is granted
-w FILE
       FILE exists and write permission is granted
....

E.g.:

if [ -r $file ]; then
   ..
fi

Upvotes: 0

Chris Seymour
Chris Seymour

Reputation: 85785

The tool you want is stat:

$ touch /tmp/file

# permissions + filename
$ stat -c "%a %n" /tmp/file
644 /tmp/file

# just file permissions
$ stat -c "%a" /tmp/file 
644

# human readable format
$ stat -c "%A" /tmp/file
-rw-r--r--

See man stat for the full feature set.

Upvotes: 3

Related Questions