Author image Leo Charre
and 1 contributors


File::Trash - move files to trash and restore as well


   use File::Trash qw/trash restore/;

   my $trashed_path = trash('~/this_is_boring.txt');
   # returns '/tmp/trash/home/username/this_is_boring.txt'
      or die($File::Trash::errstr);

   my $abs_trash = $File::Trash::ABS_TRASH;
   # returns '/tmp/trash' by default

   my $trash_count = trash('~/this_is_boring.txt', '~/this_is_boring_2.txt');
   # returns '2'


File::Remove apparently does something similar. I don't see example for using File::Remove in a simple manner alike unlink(). Thus, here it is.

The default abs trash dir

The default dir for trash has been chosen as /tmp/trash. The other common sense place would be $ENV{HOME}/.trash, so, why not? What if you are calling this from some script running under a used who does not have ~, like, maybe cron or maybe from some cgi? This is safer. If you want, you can set the dir to be something else..

   $File::Trash::ABS_TRASH = $ENV{HOME}/.trash

This is the same for default abs backup dir.


No subs are exported by default.


Ideally this should behave as unlink(). It does not at present.

Argument is a list of paths to files to delete. If more than one file is provided, returns number of files deleted. If one file is provided only, returns abs path to where the file moved. Returns undef in failure, check errors in $File::Trash::errstr.

If the trash destination exists, the file is appended with a dot digit number. So, this really makes sure you don't lose junk.


Same as trash(), but does not move the file, copies it instead.


Argument is abs path to file in trash. Attempts to move back to where it was originally. If the restore to destination exists, will not do it and return false. If you provide one argument, returns destination it restored. If you provide an array, returns number of files restored.

      or die($File::Trash::errstr);


Default is /tmp/trash


Default is /tmp/backup


Set to true to see some debug info.


May hold error string.


In development. Works great as far as I know. This is mean to be POSIX compliant only. That means no windoze support is provided. If you have suggestions, please forward to AUTHOR.




Leo Charre leocharre at cpan dot org


This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself, i.e., under the terms of the "Artistic License" or the "GNU General Public License".


This package is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the "GNU General Public License" for more details.