Unix Tip: Displaying File Attributes with the Perl stat Command
Send in your Unix questions today!
See additional Unix tips and tricks
A lot of information is available about individual files on a Unix system. For example, the ls -l command will display the permissions matrix and ls -i will display a file's inode. But, if we want to list all three of the file's dates (atime, ctime and mtime) or retrieve one of the attributes in a script, one of the versatile tools is a command included in Perl -- the stat command.
The stat command retrieves thirteen different pieces of information about a file and is most often used like this command in which each of the file descriptors is assigned to a variable that reflects its value:
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, $blksize,$blocks) = stat($filename);
Numbered 0 through 12, each of these parameters is defined here: 0 dev the device number of the filesystem 1 ino the inode number 2 mode the file mode (type and permissions) 3 nlink the number of (hard) links to the file 4 uid the numeric user ID of file's owner 5 gid the numeric group ID of file's owner 6 rdev the device identifier (special files only) 7 size the total size of the file, in bytes 8 atime the last access time in seconds since the epoch 9 mtime the last modify time in seconds since the epoch 10 ctime the inode change time in seconds since the epoch (*) 11 blksize the preferred block size for file system I/O 12 blocks the actual number of blocks allocated
If you are only interested in using one of the file's attribute, on the other hand, you could use this syntax and avoid the "possible typo" messages that you would get from failing to make subsequent use of the other twelve values:
$mode = (stat($filename))[2];
What you're going to notice right away, however, when you use the stat command is that not all of the attributes are going to be extracted in a form that makes them easy to use. If we retrieve the mode value, using the command shown above, for example, and then print the value, we're going to see something like this:
33188
This is because the mode field contains the file type along with its permissions matrix, so you still need to extract the information you want. This can be done by logically anding the field with the number 07777 like this:
printf "Permissions are %04o\n", $mode & 07777;
The number 33188 is 0100644 in octal and that number ANDed with 07777 yields 644. This printf statement will, therefore, print the phrase "Permissions are 0644".
Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world
Esther Schindler
If the comments are ugly, the code is ugly
claird
SVG a graphics format for 21st century
pasmith
Take Chrome OS for a test spin
Sandra Henry-Stocker
Solaris Tip: Have Your Files Changed Since Installation?
jfruh
Android fragments vs. the iPhone monolith
mikelgan
What Gizmodo missed about the Pro WX Wireless USB disk drive
Sidekick: The Good News & the Bad News
Either way you look at it Microsoft Data Center management did not follow standards or best practices in this failure. In which case it makes me wonder more about the outsourcing of corporate data much less personal data.
- mburton325
Join the conversation here
Quick, practical advice for IT pros. Made fresh daily.
Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.














permissions
dude, isn't it 0777 (not 07777) like everywhere? :)printf "Permissions are %04o\n", $attrs[2] & 0777;