Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 20 down vote accepted

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>
share|improve this answer
    
thanks, selected as answer because it was the first one and also seems to be the simplest one :-) @seg_fault: stat -x YOUR_FILE gives me more than just the mode value. –  stefan.at.wpf Sep 1 '12 at 21:06

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+) = 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.

share|improve this answer
    
Yes, i found it under FreeBSD box, use stat -x YOUR_FILE under FreeBSD box –  Mohsen Pahlevanzadeh Sep 1 '12 at 19:02
    
stat -r YOUR_FILE |awk '{print $3}' gives your permission. –  Mohsen Pahlevanzadeh Sep 1 '12 at 19:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.