Skip to content

Instantly share code, notes, and snippets.

@fgimian
Last active July 17, 2018 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fgimian/32ee87eb0d8835afe4f994df34ae091b to your computer and use it in GitHub Desktop.
Save fgimian/32ee87eb0d8835afe4f994df34ae091b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""
Trash Locations shows you all deleted files and their original location (sorted by original
location).
To run this script, you'll first need to install Python 3.6 or later and run:
pip3 install ds_store
Developed by Fotis Gimian (MIT license).
"""
import os
import sys
from ds_store import DSStore
# Colours
BOLD = '\x1b[1m'
GREEN = '\x1b[92m'
YELLOW = '\x1b[93m'
BLUE = '\x1b[94m'
RESET = '\x1b[0m'
def main():
trash_ds_store_path = os.path.join(os.environ['HOME'], '.Trash', '.DS_Store')
if not os.path.exists(trash_ds_store_path):
print('Your trash appears to be empty. Congratulations! :)')
sys.exit(1)
deleted_files = []
max_filename_length = 0
with DSStore.open(trash_ds_store_path) as trash_ds_store:
original_location = None
is_directory = False
for entry in trash_ds_store:
if entry.code == b'dscl':
is_directory = True
elif entry.code == b'ptbL':
original_location = f'/{entry.value}'
elif entry.code == b'ptbN':
filename = entry.value
max_filename_length = max(max_filename_length, len(filename))
deleted_files.append((original_location, is_directory, filename))
original_location = None
is_directory = False
format_string = f'{{:{max_filename_length}}}{{:^16}}{{}}'
print(
BOLD, format_string.format('Filename', 'Directory?', 'Original Location'), RESET, sep=''
)
for original_location, is_directory, filename in deleted_files:
if is_directory:
print(BLUE, format_string.format(filename, '✓', original_location), RESET, sep='')
else:
print(GREEN, format_string.format(filename, '', original_location), RESET, sep='')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment