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

Craig A. Finseth fin at finseth.com
Tue Oct 31 05:50:31 PST 2006


   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.

"ls -d XXX" will give the permissions for XXX if XXX is a directory.

   We have chmod to CHANGE the permissions, but how can we READ them?

Perl is your friend:

------------------------------------------------------------
#!/usr/bin/perl

foreach $a (@ARGV) {
	( $device, $inode, $mode, $nlinks, $uid, $gid, $rdev, $size, $atime,
		$mtime, $ctime, $blksize, $blocks ) = stat($a);
	print "file $a:\n";
	printf "\tmode\t%o\n", $mode;
	# other information you may want...
	print "\tuid\t$uid\n";
	print "\tgid\t$gid\n";
	print "\tsize\t$size\n";
	}

exit 0;
------------------------------------------------------------

(Your path to Perl may vary.)

Craig


More information about the X4U mailing list