Manipulating files and directories
Manipulating files and directories
The action of opening a file for writing creates it. Perl also provides functions to manipulate files without having to ask the operating system to do it.
unlink(filename)
Delete an existing file. Unlink can take a list of files, or wildcard as an argument as well: unlink(<*.bak>)
rename(oldname, newname) This function renames a file. It is possible to move files into other directories by specifying a path as part of the new name.
Directories also have some special function associated with them
mkdir(dirname, mode)
Create a new directory. The “mode” specifies the permissions (set this to 0777 to be safe).
rmdir(dirname)
Removes (empty) directories
chdir(dirname)
Change current working directory to dirname
File and directory attributes can be modified as well:
chmod(permission, list of files)
Change the permissions of files or directories:
666 = read and write
444 = read only
777 = read, write, and executable
utime(atime, mtime, list of files)
Modify timestamps on files or directories. “atime” is the time of the most recent access, and “mtime” is the time the file/directory was last modified.