In FreeBSD and also in Linux, how can I get the numerical chmod value of a file? For example, 644 instead of -rw-r--r--? I need an automatic way for a Bash script.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can get the value directly using a stat output format, e.g. BSD/OS X:
stat -f "%OLp" <file>
or in Linux
stat --format '%a' <file>
and in busybox
stat -c '%a' <file>
Method 2
use stat YOUR_FILE unless write script that calculate :
rwx rwx rwx ==> ( r = 4 ) if set + ( w = 2) if set + (x = 1) if set , for example: You have : -rw-wxrw- => (4+2+0)(0+2+1)(4+2+0) = 0636 First argument before 9 permissions is one of : - = regular file d = directory b = block device c = character device s = socket p = pipe f = fifo
By the way , I use stat command on Linux box, not freebsd, because it investigate HFS probably work with UFS.
Method 3
Some additional information on stat:
$ stat -c %a file.txt 777 $ stat -c %A file.txt -rwxrwxrwx
Method 4
try this to list all
stat --format "%a %n" *
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0