Perl File Operations

by Geethalakshmi 2010-09-17 12:29:30

Perl File Operations


chmod(LIST)
chmod LIST
Changes the permissions of a list of files. The first element of the list must be the numerical mode. Returns the number of files successfully changed.

$cnt = chmod 0755, 'foo', 'bar';
chmod 0755, @executables;

chown(LIST)
chown LIST
Changes the owner (and group) of a list of files. The first two elements of the list must be the NUMERICAL uid and gid, in that order. Returns the number of files successfully changed.

$cnt = chown $uid, $gid, 'foo', 'bar';
chown $uid, $gid, @filenames;

Here's an example that looks up non-numeric uids in the passwd file:

print "User: ";
$user = ;
chop($user);
print "Files: "
$pattern = ;
chop($pattern);
open(pass, '/etc/passwd') || die "Can't open passwd: $!\n";
while () {
($login,$pass,$uid,$gid) = split(/:/);
$uid{$login} = $uid;
$gid{$login} = $gid;
}
@ary = <${pattern}>; # get filenames
if ($uid{$user} eq '') {
die "$user not in passwd file";
}
else {
chown $uid{$user}, $gid{$user}, @ary;
}

fcntl(FILEHANDLE,FUNCTION,SCALAR)
Implements the `fcntl(2)' function. You'll probably have to say:

require "fcntl.ph"; # probably /usr/local/lib/perl/fcntl.ph

first to get the correct function definitions. If `fcntl.ph' doesn't exist or doesn't have the correct definitions you'll have to roll your own, based on your C header files such as `'. (There is a perl script called `h2ph' that comes with the perl kit which may help you in this.) Argument processing and value return works just like ioctl below. Note that fcntl will produce a fatal error if used on a machine that doesn't implement `fcntl(2)'.
fileno(FILEHANDLE)
fileno FILEHANDLE
Returns the file descriptor for a filehandle. Useful for constructing bitmaps for select(). If FILEHANDLE is an expression, the value is taken as the name of the filehandle.
flock(FILEHANDLE,OPERATION)
Calls `flock(2)' on FILEHANDLE. See manual page for `flock(2)' for definition of OPERATION. Returns true for success, false on failure. Will produce a fatal error if used on a machine that doesn't implement `flock(2)'. Here's a mailbox appender for BSD systems.

$LOCK_SH = 1;
$LOCK_EX = 2;
$LOCK_NB = 4;
$LOCK_UN = 8;

sub lock {
flock(MBOX,$LOCK_EX);
# and, in case someone appended
# while we were waiting...
seek(MBOX, 0, 2);
}

sub unlock {
flock(MBOX,$LOCK_UN);
}

open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
|| die "Can't open mailbox: $!";

do lock();
print MBOX $msg,"\n\n";
do unlock();

link(OLDFILE,NEWFILE)
Creates a new filename linked to the old filename. Returns 1 for success, 0 otherwise.
lstat(FILEHANDLE)
lstat FILEHANDLE
lstat(EXPR)
lstat SCALARVARIABLE
Does the same thing as the stat() function, but stats a symbolic link instead of the file the symbolic link points to. If symbolic links are unimplemented on your system, a normal stat is done.
readlink(EXPR)
readlink EXPR
readlink
Returns the value of a symbolic link, if symbolic links are implemented. If not, gives a fatal error. If there is some system error, returns the undefined value and sets `$!' (errno). If EXPR is omitted, uses `$_'.
rename(OLDNAME,NEWNAME)
Changes the name of a file. Returns 1 for success, 0 otherwise. Will not work across filesystem boundaries.
stat(FILEHANDLE)
stat FILEHANDLE
stat(EXPR)
stat SCALARVARIABLE
Returns a 13-element array giving the statistics for a file, either the file opened via FILEHANDLE, or named by EXPR. Returns a null list if the stat fails. Typically used as follows:

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat($filename);

If stat is passed the special filehandle consisting of an underline (`_'), no stat is done, but the current contents of the stat structure from the last stat or filetest are returned. Example:

if (-x $file && (($d) = stat(_)) && $d < 0) {
print "$file is executable NFS file\n";
}

(This only works on machines for which the device number is negative under NFS.)
symlink(OLDFILE,NEWFILE)
Creates a new filename symbolically linked to the old filename. Returns 1 for success, 0 otherwise. On systems that don't support symbolic links, produces a fatal error at run time. To check for that, use eval:

$symlink_exists = (eval 'symlink("","");', $@ eq '');

truncate(FILEHANDLE,LENGTH)
truncate(EXPR,LENGTH)
Truncates the file opened on FILEHANDLE, or named by EXPR, to the specified length. Produces a fatal error if truncate isn't implemented on your system.
unlink(LIST)
unlink LIST
unlink
Deletes a list of files. If EXPR is not specified, deletes file specified by `$_'. Returns the number of files successfully deleted.

$cnt = unlink 'a', 'b', 'c';
unlink @goners;
unlink <*.bak>;

Note: unlink will not delete directories unless you are superuser and the `-U' flag is supplied to perl. Even if these conditions are met, be warned that unlinking a directory can inflict damage on your filesystem. Use rmdir instead.
utime(LIST)
utime LIST
Changes the access and modification times on each file of a list of files. The first two elements of the list must be the NUMERICAL access and modification times, in that order. Returns the number of files successfully changed. The inode modification time of each file is set to the current time. Example of a "touch" command:

#!/usr/bin/perl
$now = time;
utime $now, $now, @ARGV;

Tagged in:

1018
like
0
dislike
0
mail
flag

You must LOGIN to add comments