Unix Tip: Displaying File Attributes with the Perl stat Command

1 comment | I like it!
March 7, 2006, 11:12 AM —  ITworld.com — 

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

I like it!
Comments

permissions

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



| reply
peer-to-peer

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?

sjvn
64-bits of protection?

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

The Daily Tip

The Daily TipQuick, practical advice for IT pros. Made fresh daily.

Hot tips:

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.

Newsletters

Subscribe to ITWORLD TODAY and receive the latest IT news and analysis.

I would like to receive offers via email from ITworld partners.
By clicking submit you agree to the terms and conditions outlined in ITworld's privacy policy.
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

Marketplace