Alternative to rm --recursive --interactive
Here's the deal: I get nervous using
When it comes to regular files I will usually remove only a few files interactively, and just once in a while I'll remove large amounts of files withthe
What I think it should do is ask like this:
instead of:
Is there a solution to this I don't know about? Google only gives me scripts for moving files to a trash dir which doesn't work for me at all.
I thought that if I can't find something better, I might alias
Will it behave badly in some odd situation I didn't think of? Is there something I should add (e.g. printing the working directory, like zsh's "rm -rf *" protection)?
Sorry for the big post. I'm looking forward to your input.
Edit: Added support for options.
Edit 2: Fixed a bug concerning long options. In case anyone is interested, I continue this script's development over here.
rm -rf and I don't think rm -ri and rm -rI handle directories the smartest way.When it comes to regular files I will usually remove only a few files interactively, and just once in a while I'll remove large amounts of files with
rm -f after double checking the list. For directories I always have to use --force, and a lot of times I remove one dir and a few files, making me drop the interactiveness for the files or run rm twice.What I think it should do is ask like this:
$ rm -ri folder/ file
rm: recursively remove directory `folder'? y
rm: remove regular file `file'? yinstead of:
$ rm -ri folder/ file
rm: descend into directory `folder'? y
rm: descend into directory `folder/subfolder'? y
rm: descend into directory `folder/subfolder/subfolder2'? nevermind!^CIs there a solution to this I don't know about? Google only gives me scripts for moving files to a trash dir which doesn't work for me at all.
I thought that if I can't find something better, I might alias
rm to a script like this:
#! /bin/bash
# v20090319
# Removes files and directories interactively, but doesn't ask about
# directories' contents like regular rm -ri.
# Perform a regular rm, unless this is rm -ri (so you can alias rm to this
# script)
if [ "$1" != "-ri" ]; then
rm -i "$@"
exit $?
else
shift
fi
opts="" # options that will be passed along to rm
for arg in "$@"; do
# look for options, stop looking if we got "--"
if [[ "$opts" != *\ -- && "$arg" == -* ]]; then
opts="$opts $arg"
continue
fi
# if it's a directory, ask whether to remove recursively
if [ -d "$arg" ]; then
echo -n "rm: recursively remove directory \`${arg%/}/'? "
read answer
if [ "$answer" == "y" ]; then
rm -rf $opts "$arg"
if [ "$?" != "0" ]; then exit 1; fi
fi
# if not, do what rm -i does
else
rm -i $opts "$arg"
if [ "$?" != "0" ]; then exit 1; fi
fi
done
exit 0Will it behave badly in some odd situation I didn't think of? Is there something I should add (e.g. printing the working directory, like zsh's "rm -rf *" protection)?
Sorry for the big post. I'm looking forward to your input.
Edit: Added support for options.
Edit 2: Fixed a bug concerning long options. In case anyone is interested, I continue this script's development over here.
