I need to set the same chmod, how to get number for -rw-r–r– ?
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
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
Method 2
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: -
Method 3
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.
Method 4
This might be straightforward
-bash-3.2$ stat --format=%a sample_file 755
Method 5
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 '%04on' $((2#$b))
0644
In one line:
$ b=${a//[ST-]/0}; b=${b//[!0]/1}; printf '%04on' $((2#$b))
0644
Error correction and detecting the other 3 bits 1000, 2000 or 4000 require some more code:
#!/bin/bash
Say (){ printf '%sn' "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82a6c2">[email protected]</a>"; }
SayError(){ a=$1; shift; printf '%sn' "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="032743">[email protected]</a>" >&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 '%04on' "$((2#$b|c<<9))"
Method 6
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
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