while getopts 'lp' opt; do
case "$opt" in
l) do_list=true ;;
p) do_purge=true ;;
printf 'Usage: %s [-l|-p] [ name ... ]\n' "$0" >&2
shift "$(( OPTIND - 1 ))"
if [ ! -d "$junkdir" ] && ! "$do_purge"; then
printf '%s: Missing junk directory "%s"\n' "$0" "$junkdir" >&2
printf '%s: Re-run with -p to create it\n' "$0" >&2
if "$do_list"; then
printf 'Current junk in "%s":\n' "$junkdir"
ls -l "$junkdir"
if "$do_purge"; then
printf 'Emptying junk directory "%s"\n' "$junkdir"
rm -rf "$junkdir"
if [ "$#" -eq 0 ]; then
echo 'Junking the following things:'
printf '%s\n' "$@"
mv "$@" "$junkdir"
This script implements what it is you're trying to do. It uses
getopts to parse the command line flags
do_purge in the while loop that parses the command line option because I want to control the order in which the
ls and the
rm takes place further down. If a user uses
-pl I want them to see the contents of the junk directory before it is removed.
Removing the junk directory with
-p also recreates it. I do this with
rm -rf followed by
mkdir for simplicity.
After the command line parsing loop, I adjust the positional parameters so that they only contain non-option arguments (names of files and/or directories).
If there are no positional arguments (
$# is zero), I don't try to run
mv to move files to the junk directory.