A lightweight bash script that provides a safe alternative to rm by moving files to a trash directory instead of permanently deleting them.
- Move files to trash instead of permanent deletion
- Preserves original directory structure
- Easy file restoration
- Automatic cleanup of files older than 30 days
- Lightweight and fast
- No dependencies beyond standard Unix tools
# Download the script
curl -o ~/.t.sh https://raw.githubusercontent.com/mtognela/t/master/t
# Make it executable
chmod +x ~/.t.sh
# Add to your shell configuration
echo "source ~/.t.sh" >> ~/.bashrc
# Reload your shell
source ~/.bashrc
# Initialize trash directory
st- Clone or download this repository
- Copy
t.shto your home directory (or anywhere you prefer) - Add the following line to your
~/.bashrcor~/.zshrc:source /path/to/trash.sh - Reload your shell configuration:
source ~/.bashrc
- Initialize the trash directory:
st
Move files to trash (similar to rm)
t myfile.txt
t file1.txt file2.txt dir/Restore files from trash to current directory
rt myfile.txt
rt file1.txt file2.txtList all files in trash with details
ltClean trash - permanently delete ALL files in trash
ctWarning: This permanently deletes everything in trash!
Clean old trash - automatically delete files older than 30 days
coldtSetup/initialize trash directory
stWhen you trash a file, the script:
- Converts the file path to an absolute path
- Recreates the full directory structure inside
~/.t/ - Moves the file to preserve its original location
Example:
Original: /home/user/projects/myapp/file.txt
Trashed: /home/user/.t/home/user/projects/myapp/file.txt
This approach:
- Prevents filename conflicts
- Makes it easy to know where files came from
- Allows for straightforward restoration
The coldt function removes files that have been in trash for more than 30 days using the find command with -mtime +30.
Optional: Set up automatic cleanup with cron:
# Edit crontab
crontab -e
# Add this line to run cleanup daily at 3 AM
0 3 * * * source ~/.trash.sh && coldtEdit the TRASH_DIR variable at the top of the script:
TRASH_DIR="$HOME/.t" # Default
# or
TRASH_DIR="$HOME/.t"
# or
TRASH_DIR="/tmp/t"Modify the -mtime value in the coldt function:
find "$TRASH_DIR" -type f -mtime +30 -delete # 30 days (default)
find "$TRASH_DIR" -type f -mtime +7 -delete # 7 days
find "$TRASH_DIR" -type f -mtime +90 -delete # 90 daysThis script is not intended to be alias to rm, instead, train yourself to use t directly.
Remember to run coldt periodically or set up the cron job to prevent trash from consuming too much disk space.
If you accidentally run ct, your files are gone permanently. Always double-check before using this command.
Upstream git repo at:
git clone https://github.com/mtognela/t.gitThis project is licensed under the GNU General Public License v2.0 - see the LICENSE.md file for details.
Author: Mattia Tognela
Copyright: (C) 2025 Mattia Tognela
Created as a lightweight, dependency-free alternative to permanent file deletion for Unix-like systems. This tool was born from the need for a simple, transparent trash system that works entirely in bash without external dependencies.