Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Readers will be familiar with the idea of returning the permissions of a file using the ls -l command, which, amongst other things, returns file permission information as shown in this example:
-rwxr-xr-x *various info* someFile
Does anyone know a convenient way of returning the permissions of a file in Octal notation instead of the usual 'rwx..'?
e.g.:
755 *various info* someFile
I could use any of various filters to strip out the Octal permission from other input, but wouldn't it be nice to have a command returning JUST the Octal permission...if this is possible?

Adapt this as needed:
#! /bin/sh
FILE=/your/fileMODE=`perl -e 'printf "%04o", (stat("@ARGV"))[2] & 07777;' $FILE`
echo $MODE
-jim

Jim:
That's a good solution - definitely TOP DRAWER!
Since I'm a Perl newbie, how about an explanation of how it works? I take it 'stat' is an internal Perl function?
Regards,
Nails

Learning perl from O'reilly covers this in more detail. Basically, the stat function returns a 13-element list giving the status info for a file.
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($filename);
0 - dev - device number of filesystem
1 - ino - inode number
2 - mode - file mode (type and permissions)
3 - nlink - number of (hard) links to the file
4 - uid - numeric user ID of file's owner
5 - gid - numeric group ID of file's owner
6 - rdev - the device identifier (special files only)
7 - size - total size of file, in bytes
8 - atime - last access time since the epoch
9 - mtime - last modify time since the epoch
10 - ctime - inode change time since the epoch
11 - blksize - preferred block size for file system I/O
12 - blocks - actual number of blocks allocated
Do a google/CPAN search on perl stat function for more info.-jim

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |