Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'This is what I use' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
This is what I use
Authored by: Paul Burney on Nov 13, '02 09:47:10PM

This is the script I wrote for the same effect. It creats a folder called .trash_can in your home directory and moves all files on the command line to the directory. It could use the .Trash directory, but I'd prefer not to mess with the Apple supplied folders.

The script uses the Bourne Shell and should work on platforms other than MacOSX. (I use it on RedHat Linux at work.) It works with spaces in file names and uses ditto rather than mv on darwin systems.

Comments are welcome.

#!/bin/sh
#
# trash - This file moves folders and files to a .trash_can directory in
# a user's home directory instead of deleting them.
#

# Make sure there is at least one file listed
if [ "$#" -eq 0 ]
then
echo "Usage: trash file-list";
exit 1;
fi

# Check for existance of a trash_can directory and create if not
if [ ! -d ${HOME}/.trash_can ]
then
echo "Creating Trash Can...";
mkdir ${HOME}/.trash_can;
chmod 700 ${HOME}/.trash_can;
fi

# Initialize a file counter
j=0;

# For each directory/file, see if it exists, if so, append a number
# and check again. Then move the files to the trash can
for this_file in "$@"
do
i=0;
if [ -s $this_file ]
then
new_file=`echo $this_file | tr '/' '_'`;
while [ -s ${HOME}/.trash_can/"${new_file}" ]
do
i=`expr $i + 1`;
new_temp=`echo $this_file | tr '/' '_'`;
new_file="${new_temp}_${i}";
done
if [ $OSTYPE = 'darwin' ]
then
ditto -rsrcFork "$this_file" ${HOME}/.trash_can/"${new_file}";
rm -rf "$this_file";
else
mv "$this_file" ${HOME}/.trash_can/"${new_file}";
fi
j=`expr $j + 1`;
fi
done

echo "$j files moved to the trash can.";



[ Reply to This | # ]