File Stats
2001-11-11 01:54:02
Category: perl:files
Description: An example of file testing in perl.
Author: detour
Viewed: 6074
Rating: (24 votes)


#!/usr/bin/perl
#
# File stats written by detour@metalshell.com
#
# Example of file testing.
#
# -o $file    true if owned by EUID
# -e $file    exists
# -z $file    zero file
# -s $file    non-zero file, returns size
# -r $file    readable     
# -w $file    writeable
# -x $file    executable
# -f $file    plain file
# -d $file    directory
# -l $file    symbolic link
# -p $file    named pipe or FIFO
# -S $file    socket
# -b $file    block special file
# -c $file    character special file
# -T $file    text file
# -B $file    binary file
# -u $file    setuid
# -g $file    setgid
# -k $file    sticky
# -t $file    true if opened to a tty
# -M $file    age in days since modified
# -A $file    age in days since last accessed
# -C $file    age in days since inode changed
#
# http://www.metalshell.com
#
 
 
unless($ARGV[0]) {
  die "Usage: $0 filename\n";
}
 
 
print "File info for: $ARGV[0]\n";
print "---------------------------\n";
 
if (-f $ARGV[0]) { 
  print "File: Yes.\n";
} else {
  print "File: No.\n";
}
 
if (-d $ARGV[0]) {
  print "Directory: Yes.\n";
} else {
  print "Directory: No.\n";
}
 
if (-r $ARGV[0]) {
  print "Readable: Yes.\n";
} else {
  print "Reabable: No.\n";
}
 
if (-w $ARGV[0]) {
  print "Writable: Yes.\n";
} else {
  print "Writable: No. \n";
}
 
printf "Last modified: %f days ago\n", -M $ARGV[0];