82

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

6
  • 2
    Programmatically or do you just need to know how to translate?
    – mattdm
    May 31, 2012 at 4:49
  • I'm lazy to translate :) stat is fine for a moment for me.
    – cnd
    May 31, 2012 at 5:19
  • 9
    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, 2012 at 6:44
  • What Unix are you using?
    – Kusalananda
    Mar 21, 2018 at 13:37

6 Answers 6

99

The numbers are calculated by adding the binary values represented by r, w, and x

r = 100b = 4
w = 010b = 2
x = 001b = 1

in every group. In your case, -rw-r--r-- would be represented by

6(r+w=4+2)4(r=4)4(r=4)

so the relevant command is

chmod 644 path/to/file
67

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: -
2
  • 19
    stat -c %a /path/to/file is the magic trick you need.
    – fromnaboo
    Aug 9, 2012 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, 2014 at 21:54
34

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.

1
12

This might be straightforward

-bash-3.2$ stat --format=%a sample_file
755
1
  • stat -f %a sample_file on macOS, bash verison 5. May 27, 2020 at 0:13
6

Permissions are just the string representation of a binary number.
The 0 is mostly represented by -, the rest are letters.

basic

For basic permissions:

Convert all - and caps S or T to 0, the rest should represent 1.
The resulting binary number so constructed should be printed as octal:

$ a=-rw-r--r--
$ b=${a//[ST-]/0}
$ b=${b//[!0]/1}
$ printf '%04o\n' $((2#$b))
0644

In one line:

$ b=${a//[ST-]/0}; b=${b//[!0]/1}; printf '%04o\n' $((2#$b))
0644

Error correction and detecting the other 3 bits 1000, 2000 or 4000 require some more code:

#!/bin/bash

Say     (){ printf '%s\n' "$@"; }
SayError(){ a=$1; shift; printf '%s\n' "$@" >&2; exit "$a"; }

e1="Permission strings should have 10 characters or less"
e2="Assuming first character is the file type"
e3="Permission strings must have at least 9 characters"
e4="Permission strings could only contain 'rwxsStT-'"

a=$1

((${#a}>10))  &&   SayError 1 "$e1"
((${#a}==10)) && { Say        "$e2"; a=${a#?}; }
((${#a}<9))   &&   SayError 2 "$e3"
a=${a//[^rwxsStT-]}
((${#a}<9))   &&   SayError 3 "e4"
b=${a//[ST-]/0}; b=${b//[!0]/1}; c=0
[[ $a =~ [sS]......$ ]] && c=$((c|4)) 
[[ $a =~    [sS]...$ ]] && c=$((c|2)) 
[[ $a =~       [tT]$ ]] && c=$((c|1))

printf '%04o\n' "$((2#$b|c<<9))"
1

Get the list of files with their string and hex permission values. Putting %N at the end so the output can be put into Excel easier.

stat -c "%A %a %N" *

-rw-r--r-- 644 `file2.txt'
-rw-r--r-- 644 `file3.txt'
-rw-r--r-- 644 `file4.txt'
-rw-r--r-- 644 `file.txt'
drwxr-xr-x 755 `hsperfdata_root'
-rw-r--r-- 644 `junk.txt'
drwx------ 700 `vmware-root'

This will find all files with a specific hex permission.

find /tmp1 -user root -perm 644

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .