#!/bin/sh # # File: trash # Version: 1.1 # Author: Damien Bobillot (damien.bobillot.2002+trash@m4x.org) # Licence: GNU GPL # # This command allow you to put a file to the trash from the terminal. # Only work on Mac OS X. You also need to install the file2dev command. # # 1.0 - First version # 1.1 - Add support for files not in the root disk # Do not overwritte files in the trash anymore # ### init ### commandeName=$(basename $0) if [ $# -eq 0 ]; then echo "usage: $commandeName file1 .. fileN" exit 1 fi if [ "x$SUDO_UID" = "x" ]; then uid=`id -u` else uid="$SUDO_UID" fi ### move each file to trash ### i=1 while [ $i -le $# ]; do fileToTrash="${!i}" if [ -e "$fileToTrash" ]; then # find the best trash (on the same volume) for this file rootfs=`file2dev -m "$fileToTrash"` if [ $rootfs = `file2dev -m "$HOME"` ]; then trashDir="$HOME/.Trash" else trashDir="$rootfs/.Trashes/$uid" fi # create the trash directory if it did not exists if [ ! -e "$trashDir" ]; then mkdir -m go-rwx "$trashDir" fi # find an unused file name for the trashed file fileInTrash="$trashDir/$(basename $fileToTrash)" if [ -e "$fileInTrash" ]; then j=1 while [ -e "$fileInTrash $j" ]; do j=$(($j+1)) done fileInTrash="$fileInTrash $j" fi # move file to trash mv "$fileToTrash" "$fileInTrash" else echo "$commandeName: $fileToTrash does not exists" fi i=$(($i+1)) done