Perl Delete Files: How to Clean Up Your File System

perl operatorsPerl is the Practical Extraction and Reporting Languages (not an official acronym) – designed for aiding report processing. Perl 5 has been in development for more than 25 years, and has evolved greatly during this time. It has a very large set of libraries that can be used for almost any programming task you could think of. It has also a big user community, so it’s possible to find support and learning resources for the language easily. If you’re interested in web development, Perl is an ideal language to learn. Not only can it do everything that Java can, it is open source – this means that you don’t have to pay a thing to use it, even on commercial projects. Because of its open-source nature, it’s possible to modify Perl modules and customize them where necessary. Perl is also a great language for a beginner to learn too. The Perl script is logical and easy to remember. It also has several in built features that make it easy to recover from mistakes. If you’re interested in learning Perl, you can sign up for our Perl course. We give you a thorough introduction to the language, so you have a solid foundation you can build upon.

We’re going to show you how you can delete files using Perl in this tutorial.

How to Delete Files in Perl

Deleting files using Perl is easy, but make sure you don’t try to delete directories. This may cause damage to your operating system and you may have to reinstall it – don’t say we didn’t warn you!

The unlink function lets you delete files in Perl. The syntax for the function is as follows:

unlink (“name_of_file”);

For example, if you had a file called mynote.txt, you could delete it with unlink like this:

unlink (“mynote.txt”);

On some operating systems, you might have trouble with the unlink function. In this case, just use an operating system command (on Windows):

‘del mynote.txt’

You can also use:

my $delete = “del mynote.txt”;
‘$delete’;

This second method will have to be used on some operating systems.

The exec (execute) command can also be used, if necessary:

exec (“del mynote.txt”);

Please note that these were system commands and that the unlink function should work in most cases. But how do you know if the unlink function worked correctly? You can check whether the files were deleted successfully with the file test operator –e. The –e operator is used to check if a certain file (or directory) exists.

For example, see this script:

my $filed = “mynote.txt”;
unlink $filed;
if (-e $filed)
{
print “File has NOT been deleted!”;
}
else
{
print “File has been deleted successfully”;
}

Output:

File has NOT been deleted!
or
File has been deleted successfully

Here, we created a variable “filed” to hold the file “mynote.txt”. Then, we used the unlink function on it. After that, we used the if…then statement, along with the –e operator, to check if the unlinked file is still present or not. The output will depend on whether the file is still present in the system or not.

You could also try opening the file to check if it’s still there:

open (MYFILE, ‘mynote.txt’);

Either the file will open or you will receive an error.

Another way to check if the file exists is to check the value returned by the unlink function. You can do that like this:

my $filed = “mynote.txt”;
my $number = unlink($filed);
print “The unlink function has deleted $number file(s).”;

Output:

The unlink function has deleted 1 file(s).

The unlink function returns an integer value that is equal to the number of files it has deleted. Because it has deleted one file, we got that output.

To learn more about creating variables in Perl , Perl functions and the if…then statement, you can take a look at our tutorials on the language. Alternatively, you can sign up for this hands on Perl training course. In it, we teach you about variables, funcitons, the if…then statement and the other important basics of the langauge.

Deleting Multiple Files with Unlink

You can, of course, use unlink to delete multiple files too. For example, let’s delete three text files with unlink:

my @filed = (‘mynote1.txt’, ‘mynote2.txt’, ‘mynote3.txt’);
my $number = unlink (@filed);
print “The unlink function has deleted $number file(s).”

Output:

The unlink function has deleted 3 files

Here, we stored three files in the variable array filed. Then, we unlinked (or removed) the three files with the help of this variable. The output tells us if we were successful or not.

You can also use a loop to delete three files. You can create an array and then loop through it, deleting one file at a time:

@filed = ("newtext.txt","moretext.txt","yetmoretext.txt");
foreach $file (@filed) {
unlink($file);
}

Here, we created an array called filed and stored the three text files inside it. Then, we used the foreach function to loop through every file in the array and unlinked each file one at a time.

Deleting a Directory

We don’t recommend that you try to delete directories. Like we said, if you delete the wrong directory, you will have problems. You can delete empty directories if you need to, however, using Perl with the rmdir command. The syntax for this is:

rmdir $directory;

Let’s take an example to help you understand it better:

my $dirdel = “mynote”;
rmdir $dirdel;
You can use the –e operator to check if the dir still exists:
my $dirdel = “mynote”;
rmdir $dirdel;
if(-e $dirdel)
{
print "The directory has NOT been deleted!";
}
else
{
print “The directory has been deleted successfully.";
}

This command function will only work for empty directories. If the directory is full, you will have to use rmtree command. To learn more system commands that can help you delete files or directories, you can take this Linux course. Alternatively you may want to try this course on Bash Scripts and Python to see how to do it with other scripting languages.