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.

I need to set the same chmod, how to get number for -rw-r--r-- ?

share|improve this question
1  
Programmatically or do you just need to know how to translate? –  mattdm May 31 '12 at 4:49
    
I'm lazy to translate :) stat is fine for a moment for me. –  Heather May 31 '12 at 5:19
7  
What do you mean by “set the same”? Do you already have a file with -rw-r--r-- permission and want to set another file with the same permissions? Then see if your chmod supports --reference: “--reference=RFILE use RFILE's mode instead of MODE values” – man chmod. –  manatwork May 31 '12 at 6:44
    
yes, thank you, good point –  Heather May 31 '12 at 6:54

4 Answers 4

up vote 11 down vote accepted

Please check stat output:

# stat .xsession-errors 
  File: ‘.xsession-errors’
  Size: 839123          Blocks: 1648       IO Block: 4096   regular file
Device: 816h/2070d      Inode: 3539028     Links: 1
Access: (0600/-rw-------)  Uid: ( 1000/     lik)   Gid: ( 1000/     lik)
Access: 2012-05-30 23:11:48.053999289 +0300
Modify: 2012-05-31 07:53:26.912690288 +0300
Change: 2012-05-31 07:53:26.912690288 +0300
 Birth: -
share|improve this answer
2  
stat -c %a /path/to/file is the magic trick you need. –  fromnaboo Aug 9 '12 at 13:05
    
and the answer is in the Access block there above. The file he used in the example has different access set then the one in the question. the one in the question would have shown Access: (0644/-rw-r--r--) –  nycynik Jul 9 at 21:54
r=4
w=2
x=1

in every group. Your example is 6(r+w=4+2)4(r=4)4(r=4).

share|improve this answer

The full permissions mode number is a 4-digit octal number, though most of the time, you only use the 3 least-significant digits. Add up each group in the permissions string, taking r=4, w=2, x=1. For example:

 421421421
-rwxr-xr--
 \_/        -- r+w+x = 4+2+1 = 7
    \_/     -- r+_+x = 4+0+1 = 5
       \_/  -- r+_+_ = 4+0+0 = 4     => 0754

Now, sometimes you'll see an odd modestring like this:

-rwsr-xr-T

The fourth digit is overloaded onto the x bits in the modestring. If you see a letter other than x there, then it means one of these "special" fourth-digit bits is set, and if the letter is lower case, then x for that position is also set. So the translation for this one is:

   4  2  1
 421421421
-rwsr-xr-T
   +  +  +  -- s+_+T = 4+0+1 = 5  
 \_/        -- r+w+s = 4+2+1 = 7  (s is lowercase, so 1)
    \_/     -- r+_+x = 4+0+1 = 5
       \_/  -- r+_+T = 4+0+0 = 4  (T is uppercase, so 0)   => 05754

The standard UNIX way to show that a number is octal is to start it with a zero. GNU chmod will assume the mode you're giving it is octal anyway, but it's safest to prepend the zero.

Finally, if you see a + at the end of the modestring:

-rwxr-xr-x+

then that means the file has extended permissions, and you'll need more than chmod. Look into the setfacl and getfacl commands, for starters.

share|improve this answer

This might be straightforward

-bash-3.2$ stat --format=%a sample_file
755
share|improve this answer

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.