A native shell alternative to the trash-cli
- Published at
- Updated at
- Reading time
- 2min
Wes Bos has advocated using the trash-cli
instead of the rm
command this week.
The reasoning: rm
is unrecoverable. Once you remove files or directories using rm
, they're gone. Ergo, you can bring yourself into real trouble using rm
.
I assume one could do some wild forensic stuff to restore bits and bytes on your hard drive, but the deleted files have to be very important to go this route.
trash
can help out here! The command doesn't delete files but moves them to your operating system's trash directory. When you then accidentally delete something important, head over to your lovely bin icon and recover files from there!
But here's the kicker: installing the global trash-cli package comes with 829 dependency files. Markdown, JavaScript, JSON and license files are all at your service, making up a whopping 4.3MB heavy node_modules
directory.
Sure, 4 MB isn't an issue for me on my developer machine. And the command runs fast enough to protect me and my fat fingers from self-destruction. But still, that's a lot of dependency code to move files into another directory, isn't it?
Liran Tal thought the same and shared a nifty four-liner giving you all of trash
's functionality.
trash() {
echo "[x] moving files to trash..."
mv "$@" "$HOME/.trash"
}
I played around with Liran's function and adjusted it also to support multiple files.
# โ
globs โ `trash file-*-.txt`
# โ
directories โ `trash directory`
# โ
multiple files - `trash file-1 dir-1 file-2`
function trash() {
echo "๐๏ธ Moving files to trash..."
for var in "$@"
do
mv "$var" "$HOME/.trash"
done
}
So far, my new trash
command has been working great. I'll let you know when I'll discover any issues!
Disclaimer: I only tested this shell function in my environment: macOS and ZSH.
Join 5.5k readers and learn something new every week with Web Weekly.