Perl File/Dir Operations

File Test Operators

-r -w -x File is readable/writable/executable by effective uid/gid.

next unless -f $file;
if(-r “test.pl”) { print “readable\n” }

-R -W -XFile is readable/writable/executable by real uid/gid.
-o -OFile is owned by effective/real uid.
-e -zFile exists/has zero size.
-sFile exists and has non-zero size. Returns the size.
-f -dFile is a plain file/a directory.
-l -S -pFile is a symbolic link/a socket/a named pipe (FIFO).
-b -cFile is a block/character special file.
-u -g -kFile has setuid/setgid/sticky bit set.
-tTests if filehandle (STDIN by default) is opened to a tty.
-T -B File is a text/non-text (binary) file.
-T and -B return true on a null file, or a file at EOF when testing a filehandle.
-M -A -CFile modification / access / inode-change time.
Measured in days. Value returned reflects the file age at the time the script started.
search on $^T in the Special Variables
next unless -M $file > .5; # older then 12 hours

File Operations

chmod LISTChanges the permissions of a list of files.
The first element of the list must be the numerical mode.
chown LISTChanges the owner and group of a list of files.
The first two elements of the list must be the numerical uid and gid.
truncate FILE, SIZETruncates FILE to SIZE. FILE may be a filename or a filehandle.
link OLDFILE, NEWFILECreates a new filename linked to the old filename.
lstat FILELike stat, but does not traverse a final symbolic link.
mkdir DIR, MODECreates a directory with given permissions. Sets $! on failure.
readlink EXPRReturns the value of a symbolic link.
rename OLDNAME, NEWNAMEChanges the name of a file.
rmdir NAMEDeletes the directory if it is empty. Sets $! on failure.
stat FILEReturns a 13-element array
(0: $dev, 1: $ino, 2: $mode, 3: $nlink, 4: $uid, 5: $gid,
6: $rdev, 7: $size, 8: $atime, 9: $mtime, 10: $ctime,
11: $blksize, 12: $blocks).
FILE can be a filehandle, an expression evaluating to a filename,
or _ to refer to the last file test operation or stat call. Returns a null list if the stat fails.
symlink OLDFILE, NEWFILECreates a new filename symbolically linked to the old filename.
unlink LISTDeletes a list of files.
utime LISTChanges the access and modification times.
The first two elements of the list must be the numerical access and modification times.

The following filename conventions apply when opening a file.

“FILE”    open FILE for input. also “<FILE”.
“>FILE”    open FILE for output, creating it if necessary.
open(LOG,”>$access_log”) || die “Can’t open User Access Log: $!\n”;
“>>FILE”    open FILE in append mode.   open(LOG,”>>$access_log”);
“+<FILE”    open FILE with read/write access (file must exist).
“+>FILE”    open FILE with read/write access (file truncated).
“|CMD”    opens a pipe to command CMD; forks if CMD is. open(LOG,”| ausgabe”);
“CMD|”    opens a pipe from command CMD; forks if CMD is.
open(LOG,”eingabe |”);
open(DATUM,”date |”);
open(WORT,”| wc > wort.dat”); # wc : counter

Leave a Reply

Your email address will not be published. Required fields are marked *

*