rm misbehavior

Paul Smith paul at mad-scientist.net
Sun Dec 29 22:58:44 UTC 2024


On Sun, 2024-12-29 at 14:25 -0800, MR ZenWiz wrote:
> I even created a temporary directory to check this, and it deleted
> the whole directory with 'rm <dirname>' (using the above function).

There must be something wrong with your function, because:

  $ mkdir /tmp/xxx

  $ \rm /tmp/xxx
  rm: cannot remove 'xxx': Is a directory

  $ \rm --version
  rm (GNU coreutils) 9.4
    ...

(the backslash is used to ensure there are no aliases etc. in use.)

I recommend you use:

  $ set -x
  $ rm /tmp/xxx

(with the "rm" function you defined) to see exactly what command is
being invoked.  I see a number of problems with your function but since
you said you found problems but didn't say what they were I don't know
what you actually ran.

If it were me I would write your function like this:

rm ()
{
  local force=false a ln
  for a in "$@"; do
    case $a in
      (--) break ;;
      (--force) force=true ;;
      (--*) : ;;
      (-*f*) force=true ;;
      (-*) : ;;
      (*)  break ;;
    esac
  done
  $force && read -p "Are you SURE??? " ln && test "$ln" != yes && return 1
  command rm -I -v --one-file-system --preserve=root=all "$@"
}

(note, this is untested)




More information about the ubuntu-users mailing list