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".




Similarly, the dates included in the file's attributes, all being stored in "seconds since the epoch" format are not the most human-friendly.

@attrs=stat($filename);
print "$attrs[4]\n";
1141601610

However, you can translate these date fields fairly easily into useful values like this:



$atime = localtime($attrs[8]);



Here's a simple script that retrieves and prints several of the attributes, printing them in a meaningful format:



#!/usr/bin/perl -w


$filename = "$ARGV[0]";



@attrs = stat($filename);



#!/usr/bin/perl -w



$filename = "$ARGV[0]";



@attrs = stat($filename);

print "Inode = $attrs[1]\n";
printf "Permissions are %04o\n", $attrs[2] & 07777;
print "Time of the last access (atime) = " . localtime($attrs[8]) . "\n";
print "Time of last inode change (ctime) = " . localtime($attrs[9]) . "\n";
print "Time of the last modification (mtime) = ". localtime($attrs[10]) . "\n";

Perl also can be set up to use a module (File::stat) that facilitates use of the stat command, providing by-name access. Here's an example of a script that uses File::stats:


#!/usr/bin/perl -w

use File::stat;
$filename="/var/adm/messages";


$attrs = stat($filename); printf "File is %s, size is %s, perm %04o, mtime %s\n", $filename, $attrs->size, $attrs->mode & 07777, scalar localtime $attrs->mtime;

Notice how the attributes are referred to with hash names like $attrs->size. The output from this script will look something like this:

File is var/adm/messages, size is 582, perm 0644, mtime Sun Mar  5 19:06:13 2005

 

ITworld.com

I like it!
Comments

permissions

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



| reply
Free books

Essential JavaFX
Get started building rich Web apps quickly with an introduction to the power of JavaFX key features -- scene node graphs, nodes as components, the coordinate system, layout options, colors and gradients, custom classes with inheritance, animation, binding, and event handlers.Enter now!

The Nomadic Developer
Consulting can be hugely rewarding, but it's easy to fail if you are unprepared. To succeed, you need a mentor who knows the lay of the land. Aaron Erickson is your mentor, and this is your guidebook. Enter now!

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