忽略.bashrc中的标志

时间:2016-04-15 14:09:22

标签: bash unix .bash-profile

我希望永远不会让我的日子被rm -f再次毁掉,但我没有自制力来制止。我想在我的bashrc中做一些事情,让“rm”忽略“-f”标志。

我当前的.bashrc技能(别名和功能)似乎无法解决此问题。如果我别名“rm”或用函数替换它,我会覆盖实际的“rm”并且不再能删除任何东西。我在.bashrc中可以做什么让“rm”忽略“-f”?

2 个答案:

答案 0 :(得分:0)

This gets the job done. It will prevent you from using -f, but should still allow you to delete files that contain "-f" in them, as long as that file's name isn't made only of valid "rm" options. If you have a file named as such, you can rename it before you "rm" it. This was written for OSX, and as such looks for exactly the options that OSX recognizes as rm's flags.

function rm() {
    for arg
    do
        if [[ $arg == *"f"* ]] && [[ $arg =~ ^-[dfiPRrvW]+$ ]]
        then
            echo "rm is trying to -f!"
            return 1
        fi
    done

    command rm "$@"
}

EDIT: changed return code to 1

答案 1 :(得分:0)

我想警告这种方法,因为你会习惯于保护 - 有一天你可能会在外国机器上工作,而不是你自己的机器,在那里没有进行这样的修改,然后麻烦很可能是更大。

我可能会建议采用不同的方法:
我通过每10分钟在homedirs(以及其他一些关键区域)上运行增量备份来适当地解决了这些问题(意外删除)。如果您主要进行开发或编辑工作(中小型文件),这种方法非常有效,如果处理大型媒体文件或类似文件,则无法正常工作。我已从备份中排除了Download and Media和.cache。我正在使用bacula,对初学者来说可能不是很容易配置,但也有其他工具。让这项工作得以正常工作是一项工作,但多年来它让我头疼不已,我觉得值得付出努力。
使这项工作所需的主要内容是存储备份的地方。这通常位于作为备份服务器的不同计算机上,但对于意外删除的问题,它也可能是同一台计算机上的某些受保护文件空间。今天它甚至可能在网上(但要注意安全问题)。