| |
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 = <STDIN>;
chop($user);
print "Files: "
$pattern = <STDIN>;
chop($pattern);
open(pass, '/etc/passwd')
|| die "Can't open passwd: $!\n";
while (<pass>) {
($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;
}
- 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.
- mkdir(FILENAME,MODE)
- Creates the directory specified by
FILENAME, with permissions specified by MODE (as modified
by umask). If it
succeeds it returns 1, otherwise it returns 0 and sets $! (errno).
- readlink(EXPR)
- readlink EXPR
- 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.
- rmdir(FILENAME)
- rmdir
FILENAME
- Deletes the directory specified by
FILENAME if it is empty. If it succeeds it returns 1,
otherwise it returns 0 and sets $! (errno). If FILENAME is omitted, uses $_.
- select(RBITS,WBITS,EBITS,TIMEOUT)
- This calls the select system call with
the bitmasks specified, which can be constructed using fileno() and vec(), along
these lines:
$rin = $win = $ein = '';
vec($rin,fileno(STDIN),1) = 1;
vec($win,fileno(STDOUT),1) = 1;
$ein = $rin | $win;
If you want to select on many
filehandles you might wish to write a subroutine:
sub fhbits {
local(@fhlist) = split(' ',$_[0]);
local($bits);
for (@fhlist) {
vec($bits,fileno($_),1) = 1;
}
$bits;
}
$rin = &fhbits('STDIN TTY SOCK');
The usual idiom is:
($nfound,$timeleft) =
select($rout=$rin, $wout=$win, $eout=$ein, $timeout);
or to block until something becomes
ready:
$nfound = select($rout=$rin, $wout=$win,
$eout=$ein, undef);
Any of the bitmasks can also be
undef. The timeout, if specified, is in seconds, which
may be fractional. NOTE: not all implementations are
capable of returning the $timeleft. If not, they always
return $timeleft equal to the supplied $timeout.
- 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
- Deletes a list of files. 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;
|
|
|