File::Trash::FreeDesktop - Trash files
This document describes version 0.207 of File::Trash::FreeDesktop (from Perl distribution File-Trash-FreeDesktop), released on 2023-11-21.
use File::Trash::FreeDesktop; my $trash = File::Trash::FreeDesktop->new; # list available (for the running user) trash directories my @trashes = $trash->list_trashes; # list the content of a trash directory my @content = $trash->list_contents("/tmp/.Trash-1000"); # list the content of all available trash directories my @content = $trash->list_contents; # trash a file $trash->trash("/foo/bar"); # specify some options when trashing $trash->trash({on_not_found=>'ignore'}, "/foo/bar"); # recover a file from trash (untrash) $trash->recover('/foo/bar'); # untrash a file from a specific trash directory $trash->recover('/tmp/file', '/tmp/.Trash-1000'); # specify some options when untrashing $trash->recover({on_not_found=>'ignore', on_target_exists=>'ignore'}, '/path'); # empty a trash directory $trash->empty("$ENV{HOME}/.local/share/Trash"); # empty all available trashes $trash->empty;
This module lets you trash/erase/restore files, also list the contents of trash directories. This module follows the freedesktop.org trash specification [1], with some notes/caveats:
For home trash, $HOME/.local/share/Trash is used instead of $HOME/.Trash
This is what KDE and GNOME use these days.
Symlinks are currently not checked
The spec requires implementation to check whether trash directory is a symlink, and refuse to use it in that case. This module currently does not do said checking.
Currently cross-device copying is not implemented/done
It should not matter though, because trash directories are per-filesystem.
Keywords: recycle bin
The following is a short description of the trash structure.
A trash directory is a per-filesystem, per-user directory structure to allow files to be "trashed", i.e. to be put inside and to be recovered to its original location later should a user changes his/her mind and wants the files back. Otherwise, user can "empty" the trash to delete files permanently.
A trash directory, e.g. /home/USER1/.local/share/Trash, contains two subdirectories: info and files. The files contain the actual trashed files and their name must be unique. Thus if /home/USER1/foo.txt is trashed and then another /home/USER1/foo.txt is trashed again, the second file must be renamed to /home/USER1/foo (1).txt or something else.
/home/USER1/.local/share/Trash
info
files
/home/USER1/foo.txt
/home/USER1/foo (1).txt
The info subdirectory contains the metadata for each trashed file, with each metadata put in an INI of the same name of the correspoonding file in files with .trashinfo suffix, under the INI Trash Info section. Known INI parameters include: Path (the original name/path of the trashed file) and DeletionDate (date and time, in ISO8601 format).
.trashinfo
Trash Info
Path
DeletionDate
Weird scenario: /PATH/.Trash-UID is mounted on its own scenario? How about /PATH/.Trash-UID/{files,info}.
Constructor.
Known options:
home_only
Bool. If set to true, instruct the module to just look for trash directory under the home directory and not search other filesystem mountpoints for possible trash directories.
List user's existing trash directories on the system.
Return a list of trash directories. Sample output:
("/home/mince/.local/share/Trash", "/tmp/.Trash-1000")
List contents of trash director(y|ies).
If $trash_dir is not specified, list contents from all existing trash directories. Die if $trash_dir does not exist or inaccessible or corrupt. Return a list of records like the sample below:
$trash_dir
({entry=>"file1", path=>"/home/mince/file1", deletion_date=>1342061508, trash_dir=>"/home/mince/.local/share/Trash"}, {entry=>"file1.2", path=>"/home/mince/sub/file1", deletion_date=>1342061580, trash_dir=>"/home/mince/.local/share/Trash"}, {entry=>"dir1", path=>"/tmp/dir1", deletion_date=>1342061510, trash_dir=>"/tmp/.Trash-1000"})
The path key is the original path of the file before it is put into the trash.
path
suffix
Str.
path_wildcard
Wildcard pattern to be matched against path. Only matching entries will be returned.
path_re
Regexp pattern to be matched against path. Only matching entries will be returned.
Exact matching against path. Only matching entries will be returned.
filename_wildcard
Wildcard pattern to be matched against the filename part of path. Only matching entries will be returned.
filename_re
Regexp pattern to be matched against the filename part of path. Only matching entries will be returned.
filename
Exact matching against filename part of path. Only matching entries will be returned.
mtime
Int. Only return entries where the trashed file's modification time matches this <value.
Trash a file (move it into trash dir).
Will try to find a trash dir that resides in the same filesystem/device as the file and is writable. $home/.local/share/Trash is tried first, then $device_root/.Trash-$uid, then $device_root/tmp/.Trash-$uid. Will die if no suitable trash dir is found.
$home/.local/share/Trash
$device_root/.Trash-$uid
$device_root/tmp/.Trash-$uid
Will also die if moving file to trash (currently using rename()) fails.
Upon success, will return the location of the file in the trash dir (e.g. /tmp/.Trash-1000/files/foo).
/tmp/.Trash-1000/files/foo
If first argument is a hashref, it will be accepted as options. Known options:
on_not_found => STR (default 'die')
Specify what to do when the file to be deleted is not found. The default is 'die', but can also be set to 'ignore' and return immediately.
suffix => STR
Pick a suffix. Normally, file will be stored in files/ORIGNAME inside trash directory, or, if that file already exists, in files/ORIGNAME.1, files/ORIGNAME.2, and so on. This setting overrides this behavior and picks files/ORIGNAME.SUFFIX. Can be used to identify and restore particular file later. However, will die if file with that suffix already exists, so be sure to pick a unique suffix.
files/ORIGNAME
files/ORIGNAME.1
files/ORIGNAME.2
files/ORIGNAME.SUFFIX
Recover a file or multiple files from trash.
Unless $trash_dir is specified, will search in all existing user's trash dirs. Will die on errors.
You need to specify the original path of the file before it was trashed, but you can also specify unqualified filename (without path) and/or path patterns via options (see below) instead.
If no files are found, the method will simply return.
See list_contents().
list_contents()
on_target_exists => STR (default 'die')
Specify what to do when restore target already exists. The default is 'die', but can also be set to 'ignore' and return immediately.
mtime => INT
Only recover file if file's mtime is the one specified. This can be useful to make sure that the file we recover is really the one that we trashed earlier, especially if we trash several files with the same path.
(Ideally, instead of mtime we should use some unique ID that we write in the .trashinfo file, but I fear that an extra parameter in .trashinfo file might confuse other implementations.)
See also suffix, which is the recommended way to identify and recover particular file.
Only recover file having the specified suffix, chosen previously during trash().
Erase (unlink()) a file or multiple files in trash.
Unless $trash_dir is specified, will empty all existing user's trash dirs. Will ignore if file does not exist in trash. Will die on errors.
To erase multiple files based on wilcard or regexp pattern, use the options. See list_contents().
Return list of files erased.
Empty trash.
Unless $trash_dir is specified, will empty all existing user's trash dirs. Will die on errors.
Bool, if set to true will produce additional logging statements using Log::ger at the trace level.
trace
Please visit the project's homepage at https://metacpan.org/release/File-Trash-FreeDesktop.
Source repository is at https://github.com/perlancar/perl-File-Trash-FreeDesktop.
https://freedesktop.org/wiki/Specifications/trash-spec
App::TrashUtils
A set of CLI's written in Perl: trash-empty, trash-list, trash-list-trashes, trash-put, trash-restore, trash-rm.
trash-u (from App::trash::u)
An alternative CLI, with undo support.
trash-cli
A set of CLI's written in Python: trash-empty, trash-list, trash-put, trash-restore, trash-rm.
trash-empty
trash-list
trash-put
trash-restore
trash-rm
https://github.com/andreafrancia/trash-cli
Trash::Park
Different trash structure (a single CSV file per trash to hold a list of deleted files, files stored using original path structure, e.g. home/dir/file). Does not create per-filesystem trash.
home/dir/file
File::Trash
Different trash structure (does not keep info file, files stored using original path structure, e.g. home/dir/file). Does not create per-filesystem trash.
File::Remove
File::Remove includes the trash() function which supports Win32, but no undeletion function is provided at the time of this writing.
perlancar <perlancar@cpan.org>
Steven Haryanto <stevenharyanto@gmail.com>
To contribute, you can send patches by email/via RT, or send pull requests on GitHub.
Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:
% prove -l
If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.
This software is copyright (c) 2023, 2017, 2015, 2014, 2012 by perlancar <perlancar@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=File-Trash-FreeDesktop
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
To install File::Trash::FreeDesktop, copy and paste the appropriate command in to your terminal.
cpanm
cpanm File::Trash::FreeDesktop
CPAN shell
perl -MCPAN -e shell install File::Trash::FreeDesktop
For more information on module installation, please visit the detailed CPAN module installation guide.