Computing.Net > Forums > Unix > Returning file permissions in Octal

Returning file permissions in Octal

Reply to Message Icon

Original Message
Name: ICW Infrastructure
Date: March 31, 2003 at 03:12:34 Pacific
Subject: Returning file permissions in Octal
OS: Windows 95
CPU/Ram: Pentium II 350 MHz/ 64 MB
Comment:

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?


Report Offensive Message For Removal


Response Number 1
Name: Jimbo
Date: March 31, 2003 at 07:30:03 Pacific
Subject: Returning file permissions in Octal
Reply: (edit)

Adapt this as needed:

#! /bin/sh
FILE=/your/file

MODE=`perl -e 'printf "%04o", (stat("@ARGV"))[2] & 07777;' $FILE`

echo $MODE


-jim



Report Offensive Follow Up For Removal

Response Number 2
Name: nails
Date: March 31, 2003 at 12:39:28 Pacific
Subject: Returning file permissions in Octal
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 3
Name: Jimbo
Date: March 31, 2003 at 23:39:24 Pacific
Subject: Returning file permissions in Octal
Reply: (edit)

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



Report Offensive Follow Up For Removal

Response Number 4
Name: ICW Infrastructure
Date: April 1, 2003 at 01:07:05 Pacific
Subject: Returning file permissions in Octal
Reply: (edit)

That solution works fine - nice one!


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Returning file permissions in Octal

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software