I need to set the same chmod, how to get number for -rw-r--r-- ?
6 Answers
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: -

- 6,326
-
19
-
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 09 '14 at 21:54
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.

- 16,682
This might be straightforward
-bash-3.2$ stat --format=%a sample_file
755

- 73,126

- 221
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))"
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

- 225
chmod
supports--reference
: “--reference=RFILE use RFILE's mode instead of MODE values” – man chmod. – manatwork May 31 '12 at 06:44