From: www.itworld.com
March 7, 2006 —
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