CGI Security : Better Safe than Sorry
Creating a Page Counter In Perl
Speed Thrills : CGI Please ... and Fast!
CGI Programming Made (Relatively) Easy Using Libraries
Server-Side Includes and its Extensions
Random and Recursive Crypting using Salt on Unix and Win32
Creating a mailing list using Perl
You are here: irt.org | Articles | CGI & Perl | Deleting Files in Perl [ previous next ]
Published on: Sunday 8th November 1998 By: Wing Yu
One of the first lessons that I learned in Perl was how to create files:
open MYFILE, ">myfile.txt"; print MYFILE "hello, world\n"; close MYFILE;
But what about deleting files? My first guess was to use the command delete, but it turned out that the delete command was already reserved for deleting values from specified associative arrays.
How about erase? How about remove? Either one of these would have been an obvious choice. However, it turns out that the command for this function is what seems at first to be a rather unintuitive unlink.
By using the unlink command, you can delete a whole group of files at a time:
@filelist = ("myfile1.txt","myfile2.txt","myfile3.txt"); unlink @filelist;
Keep in mind that there's a link command that lets you create links between two specified files. Hence, when you unlink a file, you're really telling the system that the specified file no longer links to any other valid files.
Be careful when you use unlink, because if you try to unlink a directory you may very well end up damaging your file system. Use rmdir to delete directories.
A question naturally arises: Can you "un-unlink" files in Perl? In other words, can you recover deleted files? Apparently not, but there's an easy way to get around this problem.
Don't use unlink. Instead, use the rename command, which simply changes the name of the file that you want to delete. While renaming the file, you can prefix the new name with a directory and a slash, hence dropping it into another directory. In our case, that directory would be one especially designated for deleted files.
To start off, create a trash directory. Let's call it "trash". Then you can delete the file as follows:
rename "myfile.txt","trash/myfile.txt";
Better yet, assign a timestamp to the deleted file:
$timenow = time; rename "myfile.txt","trash/$timenow.txt";
I prefer assigning a timestamp so that each deleted file gets a unique identifier, but you can decide on whatever naming convention you want to use. If you later inadvertently delete a file, you can immediately poke around the trash directory and quickly find the highest-numbered trashed file.
You can always delete the files permanently later on. Either use Perl again to search and unlink files in the trash directory, or launch an FTP client and select the files that you want deleted from the trash folder.
CGI Security : Better Safe than Sorry
Creating a Page Counter In Perl
Speed Thrills : CGI Please ... and Fast!
CGI Programming Made (Relatively) Easy Using Libraries
Server-Side Includes and its Extensions
Random and Recursive Crypting using Salt on Unix and Win32
Creating a mailing list using Perl
Reading and Writing to Files on the Server
Server Side Includes and CGI Security