blrm is a wrapper of rm command with user-defined blacklist.

Intro

blrm is a wrapper of the rm command and can replace rm seamlessly. The difference is that blrm will look up a user-defined blacklist before deleting a file, and skips the file from deletion if there is a match. This can prevent accidental misdeletions and save important files.

Usage

  1. Write a blacklist file containing string patterns, glob patterns or regex patterns. For example:

    *.mp3
    ~
    ~/*
    ~/.*
    ~user
    ~user/*
    ~user/.*
    

    Recommended blacklist file path is ~/.config/blrm/blacklist.

  2. Write a config file with the following content:

    rm_bin = /bin/rm
    matcher = fnmatch
    blacklist_file = ~/.config/blrm/blacklist
    

    Config file path should be one of:

    • system config file: /etc/blrm/blrm.conf.

    • user config file: ~/.config/blrm/blrm.conf.

    If both system and user config files exist, only user config file will be used.

  3. Alias rm to blrm:

    alias rm='blrm'
    

    Now typing rm in the shell will run blrm.

  4. It’s recommended to keep the alias in ~/.bashrc for autoload.

  5. To run the builtin rm, type command rm or /bin/rm.

Source

https://github.com/cykerway/blrm

Install

To install via pip, run:

pip install blrm

To install from source, run:

python setup.py install --prefix=/usr

Matcher Implementation

There are three matchers available:

str
Match against string patterns.
fnmatch
Match against glob patterns.
re
Match against regex patterns.

Depending on the matcher being used, each line in the blacklist may be seen as a string pattern, a glob pattern or a regex pattern.

The matching process has the following steps:

  1. Each pattern in the blacklist is subject to Tilde Expansion.

    For example, ~/* will become /home/<user>/*.

  2. Each command-line argument is subject to Tilde Expansion and then converted to an absolute path string.

  3. If the expanded pattern is an absolute path, then the path string will be matched from its beginning (e.g. re.match).

    If the expanded pattern is not an absolute path, then the path string will be searched from any index (e.g. re.search).

The matcher implementation is summarized in the following table:

pattern/matcher str fnmatch re
abs pattern == path fnmatch(path, pattern) re.match(pattern, path)
non-abs pattern in path fnsearch(path, pattern) re.search(pattern, path)

License

The source code is licensed under the [GNU General Public License v3.0][GPLv3].

Copyright (C) 2016 Cyker Way

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Cautions

  1. Make sure you know which matcher you are using.

  2. Specify the patterns correctly in blacklist:

    • Take care of absolute and non-absolute patterns.

    • Take care of ~ in patterns if blrm is run as a different user (e.g. with sudo).

      Use ~<user> instead of ~ when necessary.

  3. Please remember: This software is WITHOUT ANY WARRANTY. Use it at your own risk.