_learning perl_, ch13, 11.I.2002, nh FILE AND DIRECTORY MANIPULATION removing a file ~~~~~~~~~~~~~~~ `unlink` -- removes a file | `-> unlink("fred.txt",$file); # removes fred.txt unlink<*.txt> # removes all .txt files (glob evaluated in a list context) * just like unix `rm` * can take a series of names * can take variable names * return value is number of files successfully deleted -- but can't tell which of many, if one fails: for (<*.txt>){ # step through each file, using $_ unlink || warn "having trouble deleting $file: $!\n"; } renaming a file ~~~~~~~~~~~~~~~ `rename` -- renames a file | `-> takes two arguments -- old, new rename("fred","barney") || die "can't rename: $!\n"; * just like unix `mv` * but can't just stick it in new directory magically like mv: rename("file","some-directory/file"); # must specify name * _doesn't_ copy if moved from one mounted device to another linking ~~~~~~~ /--> hard links es gibt * \--> soft links (=~ symbolic links; symlinks) hard links ---------- - virtually identical to file - must be on same mounted filesystem - restricted for directories -- one from root, its own ./, and ../s from its subdirectories symbolic links -------------- - contains a pathname as data - contents of a symlink do not have to refer to an existing file or directory! - can point off the current filesystem, onto another mounted one creating hard & sort links with perl ==================================== hard: `ln` in shell `link` in perl: link("fred","maincharacter") || die "dying! $!\n"; | `-> as with rename, must specify full path soft: `ln -s` in shell `symlink` in perl: symlink("barney","neighbor") || die "can't!: $!\n"; `readlink` tells where a link points ^^^^^^^^ making & removing directories ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ `mkdir` ^^^^^ | `-> takes name of directory and permissions: mkdir("foo",0777) || die "can't mkdir foo: $!\n"; `rmdir` ^^^^^ | `-> takes directory name rmdir("foo") || die "can't remove dir foo: $!\n"; other modfying commands: ~~~~~~~~~~~~~~~~~~~~~~~ * chmod(0666,"filename"); # -rw-rw-rw- filename * chown -- takes a UID, a GID, & a list of filenames -- is like `chown` AND `chgrp` * utime -- measured in seconds since January 1, 1970 -- takes