Is there something that can turn file permissions display into numeric?
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Is there something that can turn file permissions display into numeric?
OK, when I use ls -l I get the list of files and folders but also a list of permissions, unless I have a printed out table of what they mean when choosing a chmod value, I have absolutely no idea what they are, is there a command that I can use that will show the permissions in numeric form?
For Example this bash file in my user directory
-rwxr--r-- 1 usalabs usalabs 237 Aug 24 06:20 start-irc.sh
I would like it to show a number instead of all that xyz crap, that is not understandable.
1st space on the left is designated if it's a directory (d) or not...the rest are in groups of 3 for: owner, group, and everyone else.
So what you listed is just a file (no d for directory was indicated),
-usalabs is the owner who can read,write, and execute the file
-Next, anyone in the usalabs group can read it'
-Finally, everyone else can read it.
OK, when I use ls -l I get the list of files and folders but also a list of permissions, unless I have a printed out table of what they mean when choosing a chmod value, I have absolutely no idea what they are, is there a command that I can use that will show the permissions in numeric form?
For Example this bash file in my user directory
-rwxr--r-- 1 usalabs usalabs 237 Aug 24 06:20 start-irc.sh
I would like it to show a number instead of all that xyz crap, that is not understandable.
They have the value of 4 = r, 2 = w, 1 = x so it can add up to a 7 the highest number in the numeric numbering. So in your example it is 0744 as there is nothing in front of the first r.
Code:
ls -l | awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/) \
*2^(8-i));if(k)printf("%0o ",k);print}'
The first number represents the Owner permission; the second represents the Group permissions; and the last number represents the permissions for all other users. The numbers are a binary representation of the rwx string.
So in your example it is 0744 as there is nothing in front of the first r.
The first dash is NOT a permission mode, it means "a normal file".
The permission word represents both the (12) bits of the actual file mode (chmod can modify those) and the (4) bits of what type of entry it is: like "normal file", "directory", "symbolic link", "device special file" etc.
The 0 in your example means: there are no setuid, setgid or sticky bits and thus has nothing to do with this
Quote:
- in front of the first r
the file mode always is a set of 9 characters, even though there are 12 bits represented in it.
For instance
-rwsr-s--- means 6750 as an octal value.
Personally I prefer symbolic anyway - it's more intuitive as to what perms are set.
Also, you can use symbolic representations in cmds eg chmod etc, so the numeric format is superfluous for humans, although I expect the underlying C code uses the numeric form.
It took me a week or two of trying to focus it out, now all I see are octals when I look at permissions. I had a real hard time with it at first I admit. It will come. I have no clue how that is implemented but I'd guess changing it to numbers is not a simple task.
It is useful to set the mode to a known default:
instead of
chmod a+r,go-w,a-x <some file(s)>
(which for normal users will clear set?id and sticky bits too, for the superuser you
would need ,a-s,-t to be added to the mode string too);
just use
chmod 644 <those files>
to do all of those at once (or i.e. 755 for directories and/or executables).
Distribution: Cinnamon Mint 20.1 (Laptop) and 20.2 (Desktop)
Posts: 1,672
Rep:
Quote:
For Example this bash file in my user directory
-rwxr--r-- 1 usalabs usalabs 237 Aug 24 06:20 start-irc.sh
I would like it to show a number instead of all that xyz crap, that is not understandable.
You know nothing John Snow! Do people with computers not know about binary or Octal numbers anymore (or am I just a wee bit tooooo old?)
As mentioned by Basslord1124 in post #3 above:
Quote:
1st space on the left is designated if it's a directory (d) or not...the rest are in groups of 3 for: owner, group, and everyone else.
I'd have named the groups, Owner, Group and World though. Each group has Read, Write and EXecute permission flags, in your case you have:
User: Read, Write and Execute or in Binary 111. Translated to Octal gives 7
Group: Read, no Write, no Execute. Binary 100. Translated to Octal gives 4
World: Read, no Write, no Execute. Binary 100. Translated to Octal gives 4
So, the number you want to show the permissions of your file is 744! Ta Da!
Play Bonny!
Last edited by Soadyheid; 01-10-2020 at 11:41 AM.
Reason: Text formatting
although I expect the underlying C code uses the numeric form.
Yes, from the man page for chmod(2) - the system call, not the command:
Code:
The new file mode is specified in mode, which is a bit mask created by
ORing together zero or more of the following:
S_ISUID (04000) set-user-ID (set process effective user ID on execve(2))
S_ISGID (02000) set-group-ID (set process effective group ID on execve(2);
mandatory locking, as described in fcntl(2); take a new
file's group from parent directory, as described in
chown(2) and mkdir(2))
S_ISVTX (01000) sticky bit (restricted deletion flag, as described in unlink(2))
S_IRUSR (00400) read by owner
S_IWUSR (00200) write by owner
S_IXUSR (00100) execute/search by owner ("search" applies for directories,
and means that entries within the directory can be accessed)
S_IRGRP (00040) read by group
S_IWGRP (00020) write by group
S_IXGRP (00010) execute/search by group
S_IROTH (00004) read by others
S_IWOTH (00002) write by others
S_IXOTH (00001) execute/search by others
The words with (2) behind it are references to other man pages (in man section 2)
So on the programming level you specify a single value (int) of which the lower 12 bits are significant.
The chmod command translates symbolic changes into the right 12-bits mask (see man 1 chmod).
And as far as I know ls -l always shows the 9-character mode string.
Last edited by ehartman; 01-10-2020 at 01:10 PM.
Reason: addition about chmod(1)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.