Building dontrm: A safer alternative to rm

5 min read
#go #cli #tools #open-source

Building dontrm: A safer alternative to rm

We’ve all been there - accidentally running rm -rf on the wrong directory and losing important files. This happened to me one too many times, so I decided to build dontrm, a safer alternative to the rm command.

The problem

The standard rm command is powerful but dangerous:

  • No confirmation by default for destructive operations
  • Easy to accidentally delete important files
  • No built-in recovery mechanism
  • Muscle memory can lead to mistakes

The solution

dontrm adds several safety features:

Interactive confirmation

$ dontrm important-file.txt
Are you sure you want to delete 'important-file.txt'? (y/N):

Pattern protection

Built-in protection against dangerous patterns:

  • Won’t delete root directory
  • Warns about deleting entire directories
  • Protects common important files

Verbose output

Always shows what files are being processed:

$ dontrm -r old-project/
Deleting directory: old-project/
  Deleting: old-project/src/main.go
  Deleting: old-project/README.md
  Deleting: old-project/go.mod

Implementation details

The tool is written in Go for:

  • Cross-platform compatibility - Works on Linux, macOS, and Windows
  • Performance - Fast file operations
  • Easy distribution - Single binary deployment

Key features of the implementation:

Command-line parsing

Using the flag package for clean argument parsing:

var (
    recursive = flag.Bool("r", false, "Remove directories and their contents recursively")
    force     = flag.Bool("f", false, "Ignore nonexistent files, never prompt")
    verbose   = flag.Bool("v", false, "Explain what is being done")
)

Safety checks

Multiple layers of protection:

func isSafeToDelete(path string) bool {
    // Check if it's a protected system directory
    if isSystemDirectory(path) {
        return false
    }

    // Check if it's the root directory
    if path == "/" || path == "\\" {
        return false
    }

    return true
}

Testing and reliability

The project includes comprehensive tests:

  • Unit tests for core functionality
  • Integration tests for file operations
  • Cross-platform compatibility tests

Distribution

Using GoReleaser for automated releases:

  • Builds for multiple platforms
  • Creates GitHub releases automatically
  • Generates checksums for security

What’s next

Future improvements I’m considering:

  • Trash/recycle bin integration
  • Undo functionality
  • Configuration file support
  • Better error messages

Check out the project on GitHub and feel free to contribute!