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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Returning file permissions in Octal

Reply to Message Icon

Name: ICW Infrastructure
Date: March 31, 2003 at 03:12:34 Pacific
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?



Sponsored Link
Ads by Google

Response Number 1
Name: Jimbo
Date: March 31, 2003 at 07:30:03 Pacific
Reply:

Adapt this as needed:

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

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

echo $MODE


-jim



0

Response Number 2
Name: nails
Date: March 31, 2003 at 12:39:28 Pacific
Reply:

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


0

Response Number 3
Name: Jimbo
Date: March 31, 2003 at 23:39:24 Pacific
Reply:

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



0

Response Number 4
Name: ICW Infrastructure
Date: April 1, 2003 at 01:07:05 Pacific
Reply:

That solution works fine - nice one!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Returning file permissions in Octal

file permission problem www.computing.net/answers/unix/file-permission-problem/5441.html

File permissions from Irix to NT using NFS www.computing.net/answers/unix/file-permissions-from-irix-to-nt-using-nfs/1878.html

to see unix file system in windows www.computing.net/answers/unix/to-see-unix-file-system-in-windows/3206.html