Skip to content

Instantly share code, notes, and snippets.

@ianlewis
Created March 31, 2014 01:31
Show Gist options
  • Save ianlewis/9883410 to your computer and use it in GitHub Desktop.
Save ianlewis/9883410 to your computer and use it in GitHub Desktop.
A drop-in replacement for rm that copies files to the Gnome trash rather than deleting them.
#!/bin/bash
# A drop-in replacement for rm that copies files to the Gnome trash rather than
# deleting them.
# Doesn't quite support all of the rm options yet.
readonly progname="$(basename $0)"
function printHelpAndExit {
exitCode=$1
printf "Try \`rm --help' for more information.\n"
exit $exitCode
}
function printLongHelpAndExit {
exitCode=$1
printf "Usage: rm [OPTION]... FILE...\n"
printf "Remove (unlink) the FILE(s).\n"
printf "\n"
printf " -f, --force ignore nonexistent files, never prompt\n"
printf " -i prompt before every removal\n"
printf " -I prompt once before removing more than three files, or\n"
printf " when removing recursively. Less intrusive than -i,\n"
printf " while still giving protection against most mistakes\n"
printf " --interactive[=WHEN] prompt according to WHEN: never, once (-I), or\n"
printf " always (-i). Without WHEN, prompt always\n"
printf " --one-file-system when removing a hierarchy recursively, skip any\n"
printf " directory that is on a file system different from\n"
printf " that of the corresponding command line argument\n"
printf " --no-preserve-root do not treat \`/' specially\n"
printf " --preserve-root do not remove \`/' (default)\n"
printf " -r, -R, --recursive remove directories and their contents recursively\n"
printf " -v, --verbose explain what is being done\n"
printf " --help display this help and exit\n"
printf " --version output version information and exit\n"
printf "\n"
printf "By default, rm does not remove directories. Use the --recursive (-r or -R)\n"
printf "option to remove each listed directory, too, along with all of its contents.\n"
printf "\n"
printf "To remove a file whose name starts with a \`-', for example '-foo',\n"
printf "use one of these commands:\n"
printf " rm -- -foo\n"
printf "\n"
printf " rm ./-foo\n"
printf "\n"
printf "Note that if you use rm to remove a file, it is usually possible to recover\n"
printf "the contents of that file. If you want more assurance that the contents are\n"
printf "truly unrecoverable, consider using shred.\n"
printf "\n"
printf "Report rm bugs to bug-coreutils@gnu.org\n"
printf "GNU coreutils home page: <http://www.gnu.org/software/coreutils/>\n"
printf "General help using GNU software: <http://www.gnu.org/gethelp/>\n"
exit $exitCode
}
function printErrorMessage {
printf "%s: %s\n" "$1" "$2" 1>&2
}
function printErrorHelpAndExit {
printErrorMessage "$progname" "$1"
printHelpAndExit $2
}
if [ "$1" == "" ]; then
printLongHelpAndExit 0
fi
# Check for --help special case
for i in $*
do
case $i in
--help)
printLongHelpAndExit 0
;;
esac
done
SHORTOPTS="fiIrRv"
LONGOPTS="force,interactive:,one-file-system,no-preserve-root,preserve-root,recursive,verbose,help,version"
OPTS=$(getopt --options $SHORTOPTS --longoptions $LONGOPTS -n "$progname" -- "$@")
if [ $? != 0 ]; then
printHelpAndExit $?
fi
echo "$OPTS"
#echo "prefix: $PREFIX"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment