Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MacOS: restore all files from Trash folder to their original location
#!/usr/bin/env perl
# Copyright (c) 2015 Sergey Lyubka
# All rights reserved
use Encode;
my $dir = "/Users/$ENV{USER}/.Trash";
sub read_file($) { local $/; open FD, $_[0] or die $_[0]; binmode FD; <FD>; }
sub u32($$) { unpack('N', substr($_[0], $_[1], 4)) }
sub str($$$) { my $s = substr($_[0], $_[1], $_[2]); Encode::from_to($s,'UCS-2BE','utf8'); $s; }
sub read_block($$) {
my ($p, $o) = @_;
my $cnt = u32($p, $o + 8);
$o += 12;
while ($cnt--) {
my $name_len = u32($p, $o) * 2;
my $name = str($p, $o + 4, $name_len);
my $tag = substr($p, $o + 4 + $name_len, 4);
my $type = substr($p, $o + 8 + $name_len, 4);
print "[$o] [$name] [$name_len] [$tag] [$type]\n";
$o += $name_len + 12;
if ($type eq 'bool') { $o += 1 }
elsif ($type =~ /shor|long|type/) { $o += 4; }
elsif ($type =~ /comp|dutc/) { $o += 8; }
elsif ($type eq 'ustr') {
my $n = 2 * u32($p, $o);
if ($tag eq 'ptbL') {
my $original_location = str($p, $o + 4, $n);
my $cmd = "mv \"$dir/$name\" \"/$original_location\"";
print "$cmd\n";
system($cmd);
}
$o += $n + 4;
}
elsif ($type eq 'blob') { $o += u32($p, $o) + 4; }
else { last; }
}
return $cnt;
}
my $store = read_file("$dir/.DS_Store");
my $off = u32($store, 0x14) & ~15;
while (1) {
last if read_block($store, $off) == 0;
$off = ($off & 0xfffff000) + 0x1000;
};
@mihaha

This comment has been minimized.

Copy link

commented Mar 29, 2015

Script didn't work on my mac because it couldn't move file to a directory that doesn't exist yet (I accidentally deleted a lot of files with nested directories).
I solved the problem adding "mkdir -p" to the script:

here's the code:

        my $original_location = str($p, $o + 4, $n);
        my $cmd0 = "mkdir -p \"/$original_location\"";
        my $cmd = "mv \"$dir/$name\" \"/$original_location\"";
        print "$cmd0\n";
        print "$cmd\n";
        system($cmd0);
        system($cmd);
@mihaha

This comment has been minimized.

@falsefalse

This comment has been minimized.

Copy link

commented Jun 17, 2015

Perl FTW :)

@pablosalgadom

This comment has been minimized.

Copy link

commented Nov 13, 2017

this save me a lot, thanks!

@waisie

This comment has been minimized.

Copy link

commented Apr 14, 2018

I empty my folder and files from my mac trash can.
i download the code and type it in terminal "perl restore_mac_trash.pl"
After i do that, will my whole folder go back to original location? I try it but i don't see my files go back to the original folder. Does it take long to go back? because my folder is super large. It is my iTunes whole folder. I have a lot of mp3 files there.

@waisie

This comment has been minimized.

Copy link

commented Apr 14, 2018

IS there a way just pick the date to recover from trash can? I just want to restore the files/ folder from that day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.