rm misbehavior

Jeffrey Walton noloader at gmail.com
Sun Dec 29 23:26:48 UTC 2024


On Sun, Dec 29, 2024 at 5:28 PM MR ZenWiz <mrzenwiz at gmail.com> wrote:
>
> I can't believe I have to ask this.
>
> I always thought that rm would only remove a directory if the -r
> option was included on the command line, that it would refuse to
> remove a directory otherwise.
>
> Today I was cleaning off some files on a backup disk. I ran a command
> to see if there were any duplicates, using locate to identify
> duplicates (probably not the best idea). One of the entries it found
> was a directory that held 62 files. That directory was sent to my rm
> function, which is supposed to intercept inadvertent directory
> deletion, but the function deleted the whole directory.
>
> Here is the function:
>
> rm ()
> {
>     typeset ln="no";
>     for i in $*;
>     do
>         if [[ "$i" == "-f" || "$1" == "-fr" || "$1" == "-rf" ]]; then
>             read -p "Are you SURE??? " ln && [[ "$ln" != "yes" ]] && return 1;
>             RF="$i" && shift;
>         fi;
>     done;
>     /usr/bin/rm -I -v --one-file-system --preserve-root=all $RF $*
> }
>
> I even created a temporary directory to check this, and it deleted the
> whole directory with 'rm <dirname>' (using the above function).
>
> I (now) know that the function is not quite correct (the parameter
> handling is incorrect). However, if rm is given a list of paths that
> include a directory, shouldn't it refuse to delete the directory
> absent -f or -r?
>
> Mighty confused here...

Please post the output of `command -v rm`.

Jeff



More information about the ubuntu-users mailing list