#!/usr/bin/env python3 # This script moves files to the rubbish directory. The rubbish directory is # ~/rubbish if the file is on the home directory, or /rubbish # otherwise. The file is moved to the rubbish directory with a new name based # on the original name and the current date and time. import os import shutil import time import argparse import sys import subprocess import logging logger = logging.getLogger(__name__) def move_to_rubbish(*files, dry_run=False): """ Move files to the rubbish directory. The rubbish directory is ~/rubbish if The rubbish directory is ~/rubbish if the file is on the home directory, or /rubbish otherwise. The file is moved to the rubbish directory with a new name based on the original name and the current date and time. """ # Determine the rubbish directory based on the location of the first file first_file = files[0] try: findmnt_output = subprocess.check_output(["findmnt", "-n", "-o", "TARGET", "--target", first_file]) mount_point = findmnt_output.decode("utf-8").strip() logger.debug(f"Mount point of {first_file} is {mount_point}") except subprocess.CalledProcessError as e: logger.error(f"Could not determine mount point of {first_file}") return False # If the first file is on the home directory, use ~/rubbish # Otherwise, use /rubbish if mount_point == os.path.expanduser("~") or mount_point == "/": rubbish_dir = os.path.expanduser("~/rubbish") else: rubbish_dir = os.path.join(mount_point, "rubbish") # Create the rubbish directory if it does not exist os.umask(0o077) os.makedirs(rubbish_dir, exist_ok=True) os.chmod(rubbish_dir, 0o700) # Move each file to the rubbish directory with a new name based on the original name and the current date and time for file in files: file_name = os.path.basename(file) while True: new_name = f"{file_name}_{time.time():.6f}_{os.getpid()}" new_path = os.path.join(rubbish_dir, new_name) if not os.path.exists(new_path): break time.sleep(0.001) if dry_run: logger.info(f"Would move {file} to {new_path}") else: # be careful, don't clobber shutil.move(file, new_path) # shutil.move(file, new_path) logger.info(f"Moved {file} to {new_path}") return True def setup_logging(quiet=False, debug=False): if debug: logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)s %(message)s") elif quiet: logging.basicConfig(level=logging.ERROR, format="%(message)s") else: logging.basicConfig(level=logging.INFO, format="%(message)s") if __name__ == "__main__": parser = argparse.ArgumentParser(description="Move files to the rubbish directory") parser.add_argument("files", nargs="+", help="Files to move to the rubbish directory") parser.add_argument("--dry-run", action="store_true", help="Do not actually move the files") parser.add_argument("--debug", action="store_true", help="Enable debug logging") parser.add_argument("--quiet", action="store_true", help="Suppress warnings") args = parser.parse_args() setup_logging(quiet=args.quiet, debug=args.debug) okay = move_to_rubbish(*args.files, dry_run=args.dry_run) sys.exit(0 if okay else 1)