Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit 2b8893f

Browse files
Merge pull request #571 from schezfaz/chmod_simplifier
Python script to convert Chmod permission to Symbolic/Numerical notations
2 parents 0cbdff9 + 4980208 commit 2b8893f

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed
4.93 KB
Loading[フレーム]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## Python CHMOD Simplifier
2+
3+
### What is CHMOD?
4+
'CHMOD' is a command used in Unix based systems that is used to change access permissions of files and directories. It is an abbreviation for 'Change Mode.'
5+
6+
The CHMOD numerical format is composed of 3 digits, each ranging between 0 and 7. From left to right, each digit represents the corresponding permissions for _user_, _group_ and _other_ respectively.
7+
8+
Each of these digits represents a binary value which controls the _read_, _write_ and _execute_ permissions pertaining to that group. A value of 1 indicates that the corresponding group is allowed an action, where as 0 indicates that the action is not allowed.
9+
10+
Thus, each digit can be represented as r (read), w (write) and x (execute) symbolically based on the binary value of the indicating digit.
11+
12+
For example, the chmod permission 755 can we interpreted as follows:
13+
14+
| NUMBER | CLASS | BINARY | PERMISSION | rwx |
15+
|--------|--------|--------|----------------------|-----|
16+
| 7 | USER | 111 | read, write, execute | rwx |
17+
| 5 | GROUP | 101 | read, execute | r-x |
18+
| 5 | OTHERS | 101 | read, execute | r-x |
19+
20+
Thus, 755 in the numerical format would translate to rwxr-xr-x.
21+
22+
23+
### This script converts symbolic representation of CHMOD permissions to it's numerical equivalent, and the Numerical representation of CHMOD permissions to its Symbolic equivalent as well.
24+
Example (numerical to symbolic representation):
25+
input: 777
26+
output: rwxrwxrwx
27+
28+
![Symbolic Representation](./SymbolicNotation.PNG)
29+
[Symbolic Representation](./SymbolicNotation.PNG)
30+
31+
Example (symbolic to numerical representation):
32+
input: rwxrwxrwx
33+
output: 777
34+
35+
![Numerical Representation](./NumericalNotation.PNG)
36+
[Numerical Representation](./NumericalNotation.PNG)
37+
38+
### How to use this script?
39+
40+
1. The script has 2 options:
41+
- Symbolic to Numerical (N)
42+
- Numerical to Symbolic (S)
43+
44+
Pass the desired mode of conversion while executing the script
45+
46+
2. Run the following command:
47+
python chmod_simplifier.py <representation> <mode>
48+
49+
Replace:
50+
- <representation> with 777 or rwxrwxrwx based on your preferred mode of conversion
51+
- <mode> with either 'S' or 'N' as per the desired mode
52+
53+
3. View output on console
54+
55+
### Author
56+
57+
[Schezeen Fazulbhoy](https://github.com/schezfaz)
4.93 KB
Loading[フレーム]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import sys
2+
3+
notation = {
4+
'---': '0',
5+
'--x': '1',
6+
'-w-': '2',
7+
'-wx': '3',
8+
'r--': '4',
9+
'r-x': '5',
10+
'rw-': '6',
11+
'rwx': '7'
12+
}
13+
14+
15+
def symb_to_num(symbolic):
16+
'''
17+
Convert symbolic permission notation to numeric notation.
18+
'''
19+
20+
if len(symbolic) == 9:
21+
group = (symbolic[:-6], symbolic[3:-3], symbolic[6:])
22+
try:
23+
numeric = notation[group[0]] + notation[group[1]] + notation[group[2]]
24+
except:
25+
numeric = "Invalid Symbolic Representation!"
26+
else:
27+
numeric = "Symbolic input should be of lengh 9!"
28+
29+
return numeric
30+
31+
32+
def num_to_symb(num):
33+
'''
34+
Convert number permission notation to symbolic notation.
35+
'''
36+
37+
num = str(num)
38+
if len(num) == 3:
39+
group = (num[0], num[1], num[2])
40+
symbolic = ""
41+
42+
for key, value in notation.items():
43+
for g in group:
44+
if int(g) > 8 or int(g) < 0:
45+
symbolic = "Invalid Numerical Representation!"
46+
elif g == value:
47+
symbolic = symbolic + key
48+
else:
49+
symbolic = "Number input should be of length 3!"
50+
51+
return symbolic
52+
53+
54+
def main():
55+
representation = sys.argv[1]
56+
mode = sys.argv[2]
57+
if mode == 'S':
58+
print(num_to_symb(representation))
59+
elif mode == 'N':
60+
print(symb_to_num(representation))
61+
else:
62+
print("Invalid Mode Selection. Please select 'S' for numerical --> symbolic conversion or 'N' for symbolic --> numerical conversion!")
63+
64+
65+
if __name__ == "__main__":
66+
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /