How to Make Your VPS Safer Against Accidental Deletions

If you ever deleted some important folder/file by mistake, this is the guide for you.

5 months ago   •   2 min read

By Aquasp
Table of contents

Introduction

Have you ever deleted a file or a folder by mistake in a VPS? That feeling sucks. Sometimes you are working fast and you delete a really important file/folder. This happened to me previously. Today I want to share an amazing tool with you guys: trash-cli . It adds a trash in the CLI to prevent these human mistakes.

Installing trash-cli

trash-cli is a lightweight, command-line tool available in Debian repositories, making it ideal for VPS environments with limited resources.

Installation Steps

Verify the installation:

trash --version

You should see version information if installed correctly.

Install trash-cli:

sudo apt install trash-cli

Update your package list:

sudo apt update

This process is quick and adds minimal overhead to your VPS.

Using trash-cli for Safer Deletions

Once installed, trash-cli provides commands to manage files safely. It moves items to ~/.local/share/Trash/ instead of deleting them.

Basic Commands

  • Trash a file or directory: trash file.txt or trash directory.
  • List trashed items: trash-list (shows files with deletion dates).
  • Restore items: trash-restore (interactive menu to select and recover files).
  • Empty the trash: trash-empty (permanently deletes all trashed items).
  • Empty old items: trash-empty 30 (deletes items older than 30 days).

Example workflow:

trash important_file.txt  # Move to trash
trash-list               # Check what's there
trash-restore            # Recover if needed

This replaces risky rm usage in daily operations.

Aliasing rm to Use trash-cli

To make rm safer by default, alias it to trash in your shell configuration. This ensures most deletions go to the trash bin.

Setting Up the Alias

  1. Save and exit (Ctrl+X, Y, Enter).

Reload the configuration:

source ~/.bashrc

Add this line at the end:

alias rm='trash'

Edit your ~/.bashrc file:

nano ~/.bashrc

Now, rm file.txt will use trash instead of permanent deletion.1

Automating Trash Emptying with Cron

To prevent the trash from accumulating indefinitely, automate emptying with a cron job.

Setting Up Weekly Emptying

    • 0 2 * * 0: Sunday at 2:00 AM.
    • /usr/bin/trash-empty: Clears all trash.
  1. Save and exit.

Verify:

crontab -l

Add this line for weekly deletion (e.g., every Sunday at 2 AM):

0 2 * * 0 /usr/bin/trash-empty

Edit your crontab:

crontab -e

Adjust the schedule as needed (e.g., change 0 to 1-6 for weekdays). For partial emptying, use trash-empty 30 to delete items older than 30 days.

Conclusion

By installing trash-cli, aliasing rm to trash, and setting up automated emptying, you can make your VPS much safer against accidental deletions. This approach adds a recoverable layer without sacrificing performance. Remember to combine it with regular backups and cautious command usage. If you're new to VPS management, start small and test thoroughly. For more advanced setups, explore integrating with monitoring tools. Stay safe out there!

Spread the word

Keep reading