#!/bin/sh # No arguments --> clean the current directory PROG_NAME=$(basename $0) MAXDEPTH="-maxdepth 1" print_usage() { echo "Usage: ${PROG_NAME} [OPTION]... [SOURCE]" echo echo "Remove emacs backup files (ending with \`~') in SOURCE directory." echo "If no SOURCE directory is specified, the current directory is" echo "taken as SOURCE. Directory names ending with \`~' are not" echo "removed, only files." echo echo "Options:" echo " -h display this help and exit" echo " -r allow cleanning of subdirectories" exit 0 } while getopts "hr" flag ;do case ${flag} in h) print_usage ;; r) MAXDEPTH="" ;; ?) echo "${PROG_NAME}: Invalid option: ${OPTARG}." echo "Try \`${PROG_NAME} --help' for more information." exit 1 ;; esac done shift `expr "${OPTIND}" - 1` # `$#' now represents the number of arguments after the options. # `$1' is the first argument, etc. if [ $# -gt 1 ]; then echo "${PROG_NAME}: Too many arguments." echo "Try \`${PROG_NAME} --help' for more information." exit 1 fi if [ $# = 0 ]; then source_dir="." else source_dir="${1}" fi if [ ! -d ${source_dir} ]; then echo "${PROG_NAME}: ${source_dir}: No such directory." exit 1 fi # We remove the trailing `/' at the end of directory, if present. source_dir=$(echo ${source_dir} | sed 's/\/$//g') find ${source_dir} ${MAXDEPTH} -name '*~' -exec /bin/rm '{}' + exit $?