LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Is there something that can turn file permissions display into numeric? (https://www.linuxquestions.org/questions/linux-software-2/is-there-something-that-can-turn-file-permissions-display-into-numeric-4175667004/)

Usalabs 01-03-2020 04:50 AM

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.

pan64 01-03-2020 05:09 AM

https://www.linuxquestions.org/quest...etters-811890/
https://askubuntu.com/questions/5509...ers-to-numbers
https://askubuntu.com/questions/1520...m-command-line

by the way, it is not crap and it is definitely understandable, probably better to learn it.

Basslord1124 01-03-2020 03:50 PM

r=read
w=write
x=execute

Pretty easy to understand to me.

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.

HappyTux 01-03-2020 06:36 PM

Quote:

Originally Posted by Usalabs (Post 6073881)
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}'

Found in this third hit of a google search.


https://askubuntu.com/questions/2954...ormat-i-e-0755
https://www.google.com/search?ei=hto...4dUDCAs&uact=5

Gives you a result like this when used.

Code:

seeder1@haswell:~$ ls -l
total 12
drwx------  2 seeder1 seeder1 4096 Nov  4 22:01 bin
drwxr-xr-x 12 seeder1 seeder1 4096 Jul  9 00:44 rtorrent
drwxr-xr-x  2 root    root    4096 Dec 11 17:17 src
seeder1@haswell:~$ 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}'
total 12
700 drwx------  2 seeder1 seeder1 4096 Nov  4 22:01 bin
755 drwxr-xr-x 12 seeder1 seeder1 4096 Jul  9 00:44 rtorrent
755 drwxr-xr-x  2 root    root    4096 Dec 11 17:17 src


frankbell 01-03-2020 07:29 PM

This tutorial explains how both sets of permissions (numeric and alpha) work. https://www.linux.com/tutorials/unde...e-permissions/

Here's an excerpt:

Quote:

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.

r = 4
w = 2
x = 1

ondoho 01-05-2020 05:36 AM

The answer is in post #2, last link therein.
I hope OP can muster the energy to click it, read it, and reply here.

ehartman 01-05-2020 04:18 PM

Quote:

Originally Posted by HappyTux (Post 6074244)
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.

chrism01 01-09-2020 11:31 PM

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.

jmgibson1981 01-10-2020 04:22 AM

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.

ehartman 01-10-2020 04:26 AM

Quote:

Originally Posted by chrism01 (Post 6076567)
so the numeric format is superfluous for humans,

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).

Soadyheid 01-10-2020 11:36 AM

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! :D

Play Bonny!

:hattip:

ehartman 01-10-2020 01:04 PM

Quote:

Originally Posted by chrism01 (Post 6076567)
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.


All times are GMT -5. The time now is 02:21 PM.