The safety expansion for FreeBSD rm(1)

Daichi GOTO (
First edition Wed Jan 4 17:20:11 2006
Last modified Thu Mar 10 17:58:27 JST 2011

1 Introduction

Do you have a experience that you remove some files or directories accidentally? In that case, our rm(1) safety expansion feature would be nice.

2 Download

Patch for FreeBSD 9-current.

Patch for FreeBSD 7-current.

3 How to patch and Install

prompt 3.1 shows how to apply the patch and install.

# cd /usr/src/bin/rm/
# patch < /anywhere/rm.c.diff-20070925
# make obj depend && make && make install
# make clean
prompt 3.1 how to patch and install

4 How to Use

The patch gives rm(1) an ability to add specific option for all files and directories under specific directory automatically and any deeply. That aims to prevent removing some files or directories accidentally. Patched rm(1) reads ${HOME}/.rm file as configuration file that owner just has write permission. If other user has write permission of ${HOME}/.rm, patched rm(1) cannot allow "-f" option.

By following ${HOME}/.rm file, patched rm(1) use '-i' option to all files that has any depthes under /bin, /boot, /etc, /root, /sbin, /var and /usr excluding /var/db/pkg, /var/tmp, /usr/local and /usr/ports. A "-" means non-option.

list 4.1 sample of root's ${HOME}/.rm configuration file - add '-i' option for all files and directories under specific directories
# ${HOME}/.rm: rm(1) configuration file

# options       directory

i               /bin
i               /boot
i               /etc
i               /root
i               /sbin
-               /var/db/pkg
-               /var/tmp
i               /var
-               /usr/local
-               /usr/ports
i               /usr

Have attention of directory order in ${HOME}/.rm file. It is under first-matching rule. So most times you should write subdirectories before parent directory. Well, a directory written in ${HOME}/.rm file is not targeted itself.

Have attention, too. The end line "/" of directory in ${HOME}/.rm file means something special. If you do not read source code of that patch, do not attach "/" end of directory. This strange behavior will be changes in near future. And directory must be absolute path. Patched rm(1) compares absolute path if it is symbolic link or not.

If you attach "-f" on command line, patched rm(1) does not read ${HOME}/.rm file.