[X4U] Command to get permissions of a single item?

David Ledger dledger at ivdcs.demon.co.uk
Wed Nov 1 09:30:28 PST 2006


Once you've got the permissions string from `ls -l pathname` you can 
convert to the octal version for use with chmod, if you need to with 
my prots2mode function:
<http://www.ivdcs.co.uk/fun/prots2mode>
(written for ksh). It expects and ignores the first character which 
is the '-', 'd', 'b, or whatever.

Hint:
If you're using a shell that has array variables you can
   set -A lsline x $(ls -dl pathname)
or if not but you don't need the positional parameters any more
   set x $(ls -dl pathname)
and ${lsline[1]} or $2 will contain the permission string. The 'x' is 
only needed because the '-' on the front of a file listing would act 
as a flag and confuse it. 'lsline' can, of course, be any variable 
name. The 'd' flag to 'ls' makes it work on directories themselves as 
well.

David


>From: Jerry Krinock <jerry at ieee.org>
>For a shell script I'm writing.  I need a command which will get the
>permissions, either as a string or an octal number, for a single item
>(directory, regular file, or symlink).
>
>I thought this would be easy, but after an hour of searching I can't find
>such a thing.  The only command I can find for reading permissions is "ls",
>but of course that gives it for ITEMS IN the subject, which must be a
>directory.
>
>Isn't there a non-recursive command to simply "get item's permissions",
>(and/or other metadata)?
>
>We have chmod to CHANGE the permissions, but how can we READ them?

-- 
David Ledger - Freelance Unix Sysadmin in the UK.
Chair of HPUX SysAdmin SIG of hpUG technical user group (www.hpug.org.uk)
david.ledger at ivdcs.co.uk
www.ivdcs.co.uk


More information about the X4U mailing list