Show chmod permissions as a number in Ubuntu/Linux Mint
A common problem for beginners working with Ubuntu or Linux Mint is the files/folders permissions. How to get the correct information and how to convert from one representation to another. One of the most popular tables is:
# | Permission | rwx | Binary |
---|---|---|---|
7 | read, write and execute | rwx | 111 |
6 | read and write | rw- | 110 |
5 | read and execute | r-x | 101 |
4 | read only | r-- | 100 |
3 | write and execute | -wx | 011 |
2 | write only | -w- | 010 |
1 | execute only | --x | 001 |
0 | none | --- | 000 |
The problem is that in the most articles you can see explanation like:
You also have the option to set the file permissions if you would like to change the defaults—by default, directories are 755 and files are 644.
On the other hand when you are using the Linux commands like:
ls -s
The result would be:
total 64
drwxr-xr-x 2 user user 4096 Jan 28 16:48 Desktop
drwxr-xr-x 10 user user 4096 Jan 28 11:11 Documents
Which is not always clear for the beginners and they need to do some extra effort in order to convert from:
drwxr-xr-x
to
755
So is there a way instead of getting drwxr-xr-x to get the number represenation of the permissions. Yes there is a way by using a command like:
stat -c "%a %n" *
which will result in:
755 Desktop
755 Documents
Example usage for a file or folder:
stat -c "%a %n" ./Documents/mytext.txt
result:
744 ./Documents/mytext.txt
folders:
stat -c "%a %n" ./Desktop/
result:
755 ./Desktop/
Another option which can used is the gnu command find. This command is useful when you are searching for something and would like to find the permissions in octal representation:
find ~ -maxdepth 1 -type d -printf "%m:%f\n"
result:
775:Documents
755:Desktop
Explanation:
- ~ - find in the home folder (or use . for the current folder)
- -maxdepth 1 - go only 1 level of depth
- -type d - search for folders ( -type f - for files )
- -printf "%m:%f\n" - print the output in number represented way with the folder name
The same command split on new lines for the parameters:
find ~ \
-maxdepth 1 \
-type d \
-printf "%m:%f\n"
Note: The equivalent for Mac is:
stat -f '%A %a %N' *