minor updates

This commit is contained in:
Nathaniel Landau
2015-12-24 10:46:52 -05:00
parent 17fb25e0b1
commit fe9d6eb000
2 changed files with 57 additions and 51 deletions

105
bin/trash
View File

@@ -94,9 +94,9 @@ function mainScript() {
#################################################### ####################################################
# global variables # global variables
user=$(whoami) local user=$(whoami)
uid=$(id -u "$user") local uid=$(id -u "$user")
finder_pid=$(ps -u "$user" | grep /System/Library/CoreServices/Finder.app | grep -v grep | awk '{print $1}') local finder_pid=$(ps -u "$user" | grep /System/Library/CoreServices/Finder.app | grep -v grep | awk '{print $1}')
v='' v=''
# determine whether we can script the Finder or not # determine whether we can script the Finder or not
@@ -190,27 +190,27 @@ function listTrash() {
if [ -d "$file" ]; then if [ -d "$file" ]; then
folder="${file}/.Trashes/${uid}" folder="${file}/.Trashes/${uid}"
if [ -d "${folder}" ]; then if [ -d "${folder}" ]; then
if find "$folder" -depth 1 ! -depth 0; then if find "${folder}" -depth 1 ! -depth 0; then
num_volumes=$(( $num_volumes + 1 )) num_volumes=$(( ${num_volumes} + 1 ))
blocks=$(du -cs "$folder" | tail -n 1 | cut -f 1) blocks=$(du -cs "${folder}" | tail -n 1 | cut -f 1)
total_blocks=$(( $total_blocks + $blocks )) total_blocks=$(( ${total_blocks} + ${blocks} ))
fi fi
fi fi
fi fi
done done
# convert blocks to human readable size # convert blocks to human readable size
size=0 size=0
if (( $total_blocks >= 2097152 )); then if (( ${total_blocks} >= 2097152 )); then
size=$(bc <<< "scale=2; $total_blocks / 2097152") size=$(bc <<< "scale=2; ${total_blocks} / 2097152")
size="${size}GB" size="${size}GB"
elif (( $total_blocks >= 2048 )); then elif (( ${total_blocks} >= 2048 )); then
size=$(bc <<< "scale=2; $total_blocks / 2048") size=$(bc <<< "scale=2; ${total_blocks} / 2048")
size="${size}MB" size="${size}MB"
else else
size=$(bc <<< "scale=2; $total_blocks / 2") size=$(bc <<< "scale=2; ${total_blocks} / 2")
size="${size}K" size="${size}K"
fi fi
info "$size across $num_volumes volume(s)." info "${size} across ${num_volumes} volume(s)."
safeExit safeExit
} }
@@ -231,14 +231,14 @@ function emptyTheTrash() {
if is_confirmed; then if is_confirmed; then
notice "Emptying trash..." notice "Emptying trash..."
# delete the contents of user's .Trash folder # delete the contents of user's .Trash folder
if [[ $verbose == "1" ]]; then v="-v"; fi if [[ ${verbose} == "1" ]]; then v="-v"; fi
find "/Users/${user}/.Trash" -depth 1 ! -depth 0 -print0 | xargs -0 rm $v -r find "/Users/${user}/.Trash" -depth 1 ! -depth 0 -print0 | xargs -0 rm $v -r
# delete the contents of the volume-specific .Trashes folders # delete the contents of the volume-specific .Trashes folders
for file in /Volumes/*; do for file in /Volumes/*; do
if [ -d "$file" ]; then if [ -d "${file}" ]; then
folder="${file}/.Trashes/${uid}" folder="${file}/.Trashes/${uid}"
if [ -d "$folder" ]; then if [ -d "${folder}" ]; then
find "$folder" -depth 1 ! -depth 0 -print0 | xargs -0 rm $v -r find "${folder}" -depth 1 ! -depth 0 -print0 | xargs -0 rm $v -r
fi fi
fi fi
done done
@@ -260,7 +260,7 @@ function secureEmptyTheTrash() {
fi fi
# if Finder isn't scriptable, we'll manually empty the trash ourselves # if Finder isn't scriptable, we'll manually empty the trash ourselves
else else
if [[ $verbose == "1" ]]; then v="-v"; fi if [[ ${verbose} == "1" ]]; then v="-v"; fi
# confirm that the user wants to securely empty the trash # confirm that the user wants to securely empty the trash
seek_confirmation "Are you sure you want to securely empty the trash (this REALLY cannot be undone)?" seek_confirmation "Are you sure you want to securely empty the trash (this REALLY cannot be undone)?"
if is_confirmed; then if is_confirmed; then
@@ -270,8 +270,8 @@ function secureEmptyTheTrash() {
for file in /Volumes/*; do for file in /Volumes/*; do
if [ -d "$file" ]; then if [ -d "$file" ]; then
folder="${file}/.Trashes/${uid}" folder="${file}/.Trashes/${uid}"
if [ -d "$folder" ]; then if [ -d "${folder}" ]; then
find "$folder" -depth 1 ! -depth 0 -print0 | xargs -0 srm $v -r find "${folder}" -depth 1 ! -depth 0 -print0 | xargs -0 srm $v -r
fi fi
fi fi
done done
@@ -282,7 +282,7 @@ function secureEmptyTheTrash() {
} }
function trashAFile() { function trashAFile() {
if [[ $verbose == "1" ]]; then v="-v"; fi if [[ ${verbose} == "1" ]]; then v="-v"; fi
# Iterate over all files passed by user # Iterate over all files passed by user
for userFile in "${args[@]}"; do for userFile in "${args[@]}"; do
@@ -295,7 +295,7 @@ function trashAFile() {
if have_scriptable_finder; then if have_scriptable_finder; then
# determine whether we have an absolute path name to the file or not # determine whether we have an absolute path name to the file or not
if [ "${userFile:0:1}" = "/" ]; then if [ "${userFile:0:1}" = "/" ]; then
file="${userFile}" local file="${userFile}"
else else
# expand relative to absolute path # expand relative to absolute path
verbose "Determining absolute path for '${userFile}'... " verbose "Determining absolute path for '${userFile}'... "
@@ -314,14 +314,14 @@ function trashAFile() {
fi fi
# Finder isn't available for this user, so don't rely on it (we'll do all the dirty work ourselves) # Finder isn't available for this user, so don't rely on it (we'll do all the dirty work ourselves)
else else
trash="/Users/${user}/.Trash/" local trash="/Users/${user}/.Trash/"
# create the trash folder if necessary # create the trash folder if necessary
if [ ! -d "${trash}" ]; then if [ ! -d "${trash}" ]; then
mkdir $v "${trash}" mkdir ${v} "${trash}"
fi fi
# move the file to the trash # move the file to the trash
if [ ! -e "${trash}${userFile}" ]; then if [ ! -e "${trash}${userFile}" ]; then
mv $v "${userFile}" "${trash}" mv ${v} "${userFile}" "${trash}"
else else
# determine if the filename has an extension # determine if the filename has an extension
ext=false ext=false
@@ -330,32 +330,32 @@ function trashAFile() {
esac esac
# keep incrementing a number to append to the filename to mimic Finder # keep incrementing a number to append to the filename to mimic Finder
i=1 local i=1
if $ext; then if $ext; then
new="${trash}${userFile%%.*} ${i}.${userFile##*.}" new="${trash}${userFile%%.*} ${i}.${userFile##*.}"
else else
new="${trash}$userFile $i" new="${trash}${userFile} ${i}"
fi fi
while [ -e "$new" ]; do while [ -e "${new}" ]; do
((i=$i + 1)) ((i=$i + 1))
if $ext; then if ${ext}; then
new="${trash}${userFile%%.*} ${i}.${userFile##*.}" new="${trash}${userFile%%.*} ${i}.${userFile##*.}"
else else
new="${trash}${userFile} $i" new="${trash}${userFile} ${i}"
fi fi
done done
#move the file to the trash with the new name #move the file to the trash with the new name
mv $v "${userFile}" "$new" mv ${v} "${userFile}" "${new}"
fi fi
fi fi
done done
} }
# run functions # run functions
if $list; then listTrash; fi if ${list}; then listTrash; fi
if $emptyTrash; then emptyTheTrash; fi if ${emptyTrash}; then emptyTheTrash; fi
if $secureEmpty; then secureEmptyTheTrash; fi if ${secureEmpty}; then secureEmptyTheTrash; fi
trashAFile trashAFile
#################################################### ####################################################
@@ -367,25 +367,30 @@ trashAFile
# Print usage # Print usage
usage() { usage() {
echo -n "${scriptName} [OPTION]... [FILE]... echo -n "
${scriptName} [OPTION]... [FILE]...
${bold}Trash${reset} allows trashing of files instead of tempting fate with ${bold}rm${reset}. Correctly handles ${bold}Trash${reset} allows MacOS trashing of files instead of tempting fate with ${bold}rm${reset}.
trashing files on other volumes, uses the same filename renaming scheme as Finder Anything deleted with Trash will be moved to the native MacOS trash folder.
for duplicate file names, can list trash contents w/disk usage summary, and empty
trash (including securely) w/confirmation. Does not require Finder to be running. This script:
- Correctly handles ${bold}trashing files on other volumes${reset}
- Uses the ${bold}same filename renaming scheme as Finder${reset} for duplicate file names
- Can ${bold}list trash contents${reset} w/disk usage summary
- ${bold}Empty trash${reset} (including securely) w/confirmation.
- Does not require Finder to be running.
${bold}Options:${reset} ${bold}Options:${reset}
-l , --list list trash contents -l , --list List trash contents
-e, --empty empty trash contents -e, --empty Empty trash contents
-s, --secure secure empty trash contents -s, --secure Secure empty trash contents
--force Skip all user interaction. Implied 'Yes' to all actions.
-q, --quiet Quiet (no output)
--force Skip all user interaction. Implied 'Yes' to all actions. -v, --verbose Output more information. (Items echoed to 'verbose')
-q, --quiet Quiet (no output) -d, --debug Runs script in BASH debug mode (set -x)
-v, --verbose Output more information. (Items echoed to 'verbose') -h, --help Display this help and exit
-d, --debug Runs script in BASH debug mode (set -x) --version Output version information and exit
-h, --help Display this help and exit
--version Output version information and exit
" "
} }

View File

@@ -251,4 +251,5 @@ set -o pipefail
# Run your script # Run your script
mainScript mainScript
safeExit # Exit cleanly # Exit cleanlyd
safeExit