-
Notifications
You must be signed in to change notification settings - Fork 0
/
trash
executable file
·107 lines (100 loc) · 3.1 KB
/
trash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
#
# trash - Move files to the appropriate .Trash file on Mac OS X. (Intended
# as an alternative to 'rm' which immediately deletes the file.)
#
# v0.1 2007-05-21 - Morgan Aldridge <[email protected]>
# Initial version.
# v0.2 2010-10-26 - Morgan Aldridge
# Use appropriate .Trashes folder when trashing files
# on other volumes. Create trash folder(s) if necessary.
# v0.2.1 2010-10-26 - Morgan Aldridge
# No longer using bash built-in regexp support in hopes
# of support Mac OS X 10.4 and earlier.
# v0.3 2010-12-07 - Morgan Aldridge
# Correctly handle full volume path which is root volume.
# Now increments filename if filename already exists in
# trash folder (à la Finder).
#
# TO DO:
# - Don't use .Trashes folder on root volume (check for symlink of volume to /)
# - Append an incremented number to the end of a filename, but before the file
# extension, if a file with the same name already exists.
# global variables
verbose=false
help=false
user=$(whoami)
uid=$(id -u "$user")
v=''
# print usage instructions (help)
function usage() {
printf "Usage: trash [options] file ...\n"
printf " -v verbose output\n"
printf " -h print these usage instructions\n"
}
# see if any arguments were passed in
if [ $# -gt 0 ]; then
# if so, step through them all and process them
while [ $# -gt 0 ]; do
# see if the user intended us to run in verbose mode
if [ "$1" = "-v" ]; then
shift
verbose=true
# see if the user requested help
elif [ "$1" = "-h" ]; then
shift
help=true
usage
# handle remaining arguments as if they were files
else
# don't bother doing anything if the user requested help
if ! $help; then
#printf "argument: '%s'\n" $1
#printf "destination: '%s'\n" $TRASH
if $verbose; then v="-v"; fi
# determine whether we should be putting this in a volume-specific .Trashes or user's .Trash
IFS=/ read -r -d '' _ _ vol _ <<< "$1"
if [[ ("${1:0:9}" == "/Volumes/") && (-n "$vol") && ($(readlink "/Volumes/$vol") != "/") ]]; then
trash="/Volumes/${vol}/.Trashes/${uid}/"
else
trash="/Users/${user}/.Trash/"
fi
# create the trash folder if necessary
if [ ! -d "$trash" ]; then
mkdir $v "$trash"
fi
# move the file to the trash
if [ ! -e "${trash}$1" ]; then
mv $v "$1" "$trash"
else
# determine if the filename has an extension
ext=false
case "$1" in
*.*) ext=true ;;
esac
# keep incrementing a number to append to the filename to mimic Finder
i=1
if $ext; then
new="${trash}${1%%.*} ${i}.${1##*.}"
else
new="${trash}$1 $i"
fi
while [ -e "$new" ]; do
((i=$i + 1))
if $ext; then
new="${trash}${1%%.*} ${i}.${1##*.}"
else
new="${trash}$1 $i"
fi
done
#move the file to the trash with the new name
mv $v "$1" "$new"
fi
fi
shift
fi
done
else
printf "No files were specified to be moved to the trash.\n\n"
usage
fi