How to Make Your VPS Safer Against Accidental Deletions
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
- Update your package list:
sudo apt update
- Install
trash-cli
:sudo apt install trash-cli
- Verify the installation:
You should see version information if installed correctly.trash --version
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
ortrash 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
- Edit your
~/.bashrc
file:nano ~/.bashrc
- Add this line at the end:
alias rm='trash'
- Save and exit (Ctrl+X, Y, Enter).
- Reload the configuration:
source ~/.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
- Edit your crontab:
crontab -e
- Add this line for weekly deletion (e.g., every Sunday at 2 AM):
0 2 * * 0 /usr/bin/trash-empty
0 2 * * 0
: Sunday at 2:00 AM./usr/bin/trash-empty
: Clears all trash.
- Save and exit.
- Verify:
crontab -l
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!