Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl delete all files in a directory

Tags:

perl

How can I delete all the files in a directory, (without deleting the directory) in Perl?

My host only allows up to 250,000 "files" and my /tmp folder fills that 250,000 qouta fast with all the session cookies going on. I cannot delete the /tmp folder in this situation. I am only permitted to delete the files within.

EDIT:

FTP clients and File managers don't exactly want to open up the folder... I assume it's because of the massive amount of files in it..

like image 832
Ilan Kleiman Avatar asked Aug 02 '14 02:08

Ilan Kleiman


People also ask

How do I delete all files in a directory in Perl?

You need to use glob for removing files: unlink glob "'/tmp/*.

What is unlink in Perl?

unlink LIST unlink. Deletes a list of files. On success, it returns the number of files it successfully deleted. On failure, it returns false and sets $! (errno): my $unlinked = unlink 'a', 'b', 'c'; unlink @goners; unlink glob "*.bak"; On error, unlink will not tell you which files it could not remove.

How do I delete in Perl?

Delete() in Perl is used to delete the specified keys and their associated values from a hash, or the specified elements in the case of an array. This operation works only on individual elements or slices.

How do I delete a folder in pi?

From the left pane on the home page, select the folder and then click Delete PI Vision Folder .


2 Answers

my $errors;
while ($_ = glob('/tmp/* /tmp/.*')) {
   next if -d $_;
   unlink($_)
      or ++$errors, warn("Can't remove $_: $!");
}

exit(1) if $errors;
like image 90
ikegami Avatar answered Oct 19 '22 17:10

ikegami


You can use this. You need to use glob for removing files:

unlink glob "'/tmp/*.*'";

These extra apostrophes are needed to handle filenames with spaces as one string.

like image 40
user3356 Avatar answered Oct 19 '22 15:10

user3356