Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UNIX / Linux / Mac OSX get permission of file as number

This must be really simple to do but have completely drawn a blank. I can see the permission of files by using ls -la which can give something like:

-rwxr-xr-x   1 james  staff   68  8 Feb 13:33 basic.sh*
-rw-r--r--   1 james  staff   68  8 Feb 13:33 otherFile.sh*

How do I translate that into a number for use with chmod like chmod 755 otherFile.sh (with out doing the manual conversion).

like image 658
AJP Avatar asked Feb 13 '13 13:02

AJP


2 Answers

stat -f "%Lp" [filename] works for me in OS X 10.8.

like image 114
Patrick Lewis Avatar answered Sep 16 '22 21:09

Patrick Lewis


You should be able to use the stat command instead of ls. From looking at the manpage, this should work to get the file permissions:

for f in dir/*
do
    perms=$(stat -f '0%Hp%Mp%Lp' $f)
    echo "$f has permissions $perms"
done

(although I am not at my Mac at the moment and therefore cannot test it).

like image 32
trojanfoe Avatar answered Sep 17 '22 21:09

trojanfoe