Warning: rm removes directories

I accidentally used the rm command on a directory, and it removed the entire directory. I figured that only rmdir should be able to do that.

Which command should I have used to have it fail on directories? And if it’s a command line argument of rm, how do I turn that into its default behavior?

man rm

NAME
       rm - remove files or directories
[...]
By default, rm does not remove directories.  Use the --recursive (-r or -R) option to remove each listed directory, too, along with all of its contents.
[...]

Do you have rm aliased ro something ?

Example here:

$ mkdir -p test
$ rm test
rm: cannot remove 'test': Is a directory

?? What the heck? Then why did my Manjaro Cinnamon remove the directory without any notice??

How can I check - and if necessary remove - any aliasses that I may have in the rm command?

I don’t know where the aliasses are stored. I myself never defined any; they are all as set by the installation and the subsequent updates.

I still dont know what actual command you used…

But to begin with you might check rm itself;

which rm

If that points to /usr/bin/rm then its probably fine as far as the package and filesystem go.
You can further check what package provides your rm with

pacman -Qo rm

Which should return coreutils.

For aliases they would probably be in your shells rc file.
So maybe something like

grep -i rm ~/.zshrc

or

grep -i rm ~/.bashrc

Depending on if you use zsh or bash.

(the grep commands above will search those files for the string rm)

/usr/bin/rm is owned by coreutils 9.4-3

~/.zshrc doesn’t exist, and neither ~/.bashrc nor /etc/bash.bashrc make any mention of rm.

btw, the command that I isssued was:

sudo rm /etc/shiarta

If that was the command then it should not have removed anything except for a file at that path.
A directory should have complained as in the above example, even if sudo is there.
Was it in a longer string and possibly malformed by a pipe or something?

1 Like

No, none of that, and shiarta definitely was a directory, not even a symlink to one.

I should add, perhaps, that this is not the first time that I run into this issue. It happened before, some years ago. On my Manjaro Cinnamon, it appears to be the default behavior of rm.

And it’s not the only weird thing either; even though my fstab has only a single swap partition defined, when I shut Manjaro down it reports shutdown jobs of 4 swap partitions, each ending with the extension .part1.

But that’s an issue to look into some other time, and in another thread.

The core is the same whichever version of Manjaro you use.

If rm removed a folder on your system - then you have an alias - you can check using the alias command

alias

Also see

2 Likes

systemd now automatically mounts all swap partitions it finds, unless you have them listed and commented out in /etc/fstab.

As for rm, if you issue “rm -rf” on a directory, then it will delete the directory and everything it contains. But a bare rm cannot do that.

2 Likes

This topic was automatically closed 36 hours after the last reply. New replies are no longer allowed.