Many Ways to See Numeric Permissions
Chanan Oren (among others) writes:
“You can see octal permissions by using
stat. For example, ‘stat testfile‘ outputs something
like this:File: 'testfile' Size: 0 Blocks: 0 IO Block: 4096 Regular File Device: 307h/775d Inode: 631410 Links: 1 Access: (0600/-rw-------) Uid: ( 1000/ orenc) Gid: ( 100/ users) Access: 2003-07-05 16:38:46.000000000 -0700 Modify: 2003-07-05 16:38:46.000000000 -0700 Change: 2003-07-05 16:38:46.000000000 -0700 The octal permission is in this part of the output "Access: (0600/-rw-------)"“For simpler output, use:
stat -c "%a %n" testfile“The above command will output the octal permissions followed by
the filename:
600 testfile“Or use:
stat -c "%a %n" *“To see every file in the current directory in the above format,
one file per line.”
Dee-Ann responds:
I can see how this one would be useful in a shell script, where
you just want to grab the numeric (octal) permissions and the
filename. It’s quicker than having to have the script grab the
letter permissions and then calculate the octal. stat seems to be a popular one.
David Cornish writes:
“The following
sedscript that I just hacked together (usels -l | sed -f scriptname) seems to
work:# # Directory listings numerical permissions sed script # By David Cornish (www.davidcornish.com) # # File type s/^([dl-])/1 / # First value - normal (0), sticky (1), sgid (2), suid (4) s/(^. )/10/ s/(^.) .(........)t/1 12x/ s/(^.) 0(..)s/1 42x/ s/(^.) 1(..)s/1 52x/ s/(^.) 0(.....)s/1 22x/ s/(^.) 1(.....)s/1 32x/ s/(^.) 4(.....)s/1 62x/ s/(^.) 5(.....)s/1 72x/ # Read (4)/write (2)/execute (1) permissions s/rwx/7/g s/rw-/6/g s/r-x/5/g s/r--/4/g s/-wx/3/g s/-w-/2/g s/--x/1/g s/---/0/g“It could probably be tidied up a bit, but it works.”
Dee-Ann responds:
I’m not a professional programmer, so my code can often stand to
be “tidied up a bit.” Still, it definitely works. See:
$ ls -l | sed -f ~/Documents/LockerGnome/numpermscript total 120 - 0644 1 dee dee 396 Apr 19 14:06 addressbook-sources.xml d 0700 3 dee dee 4096 Apr 19 14:07 cache - 0600 1 dee dee 3 Jul 8 21:23 camel-cert.db - 0600 1 dee dee 16384 Apr 19 14:07 cert7.db d 0700 2 dee dee 8192 Jul 9 18:24 config - 0600 1 dee dee 17832 Apr 23 10:08 config.xmldb
It even leaves the initial bit in place so we can see if we’re
looking at a directory, character device driver, file, or
whatever. If you wanted to make a command alias out of this, maybe
lso for ls with octal permissions, you would type the
following:
alias lso='ls -l | sed -f ~/Documents/LockerGnome/numpermscript'
I don’t recommend replacing ls with this option, but it’s a great addition to the
command toolset.
Sean A. Walberg writes:
[[email protected] root]# rpm -qf 'which kpsestat' tetex-1.0.7-47.1 [[email protected] root]# kpsestat = /etc/passwd 644“(BTW, I found this with ‘
apropos octal‘)”
Dee-Ann responds:
Funny, I thought I’d typed apropos. Either I did and I blinked at just the wrong time,
octal
or I’m starting to lose it… mind you, some have suggested the
latter to me before. 😉 Another great tool for shell scripting!
Jeffrey Ridout writes with this approach:
find -name "FILENAME.EXT" -printf "%mn" -maxdepth 1“
FILENAME.EXT, of course, has to be the requested file… ”
Dee-Ann responds:
And indeed, that one works too.
Yawar Amin writes:
“I’ve concocted a small ‘
awk‘ script that does just that,
called ‘perms.awk‘. I’m
attaching it here for your perusal.“The way it works is that you type something like
$ ls -lR | awk -f perms.awk | moreand voila, you get the permissions. If you want, you can wrap this
command inside a shell script which takes file/directory names as
arguments.“The content of
perms.awkis:BEGIN { owner = 0 group = 0 world = 0 } ($1 !~ /..*/) && ($1 !~ /total/) && ($0 != "") { if (substr($1, 2, 1) == "r") owner += 4 if (substr($1, 3, 1) == "w") owner += 2 if (substr($1, 4, 1) == "x") owner += 1 if (substr($1, 5, 1) == "r") group += 4 if (substr($1, 6, 1) == "w") group += 2 if (substr($1, 7, 1) == "x") group += 1 if (substr($1, 8, 1) == "r") world += 4 if (substr($1, 9, 1) == "w") world += 2 if (substr($1, 10, 1) == "x") world += 1 print $9 ": " owner group world owner = 0 group = 0 world = 0 }
Dee-Ann responds:
And, yes, another one works:
$ ls -lR | awk -f ~/Documents/LockerGnome/perms.awk | more addressbook-sources.xml: 644 cache: 700 camel-cert.db: 600 cert7.db: 600 config: 700 config.xmldb: 600 filters.xml: 600 filters.xml~: 644
Phew! That gives us solutions using awk, find, kpsestat, sed, and stat.
Not bad. Not bad at all.