4 ms·
I've had this function/alias in my {bash,zsh}rc files file years: function rm { for file in $@; do mv -t ~/.local/share/Trash/files/ -- "${file}"
by mmh0000 4y ago
I've had this function/alias in my {bash,zsh}rc files file years:
function rm {
for file in $@; do
mv -t ~/.local/share/Trash/files/ -- "${file}"
cat <<FROG > ~/.local/share/Trash/info/"$(basename ${file})".trashinfo
[Trash Info]
Path=$(realpath "${file}")
DeletionDate=$(date "+%FT%T")
FROG
done
}
- miduil 4y agoSounds useful, though I'd advice using a different alias - just to avoid surprises when for some reason your function wasn't loaded properly.
- ThePowerOfFuet 4y agoUsing rm as the alias is the whole point, so muscle memory doesn't work against you.
- BerislavLopac 4y agoI use trash-cli [0] for that. [0] https://github.com/andreafrancia/trash-cli https://github.com/andreafrancia/trash-cli
- Zamicol 4y ago>Can I alias rm to trash-put? You can but you shouldn't. In the early days I thought it was a good idea to do that but now I changed my mind. Although the interface of trash-put seems to be compatible with rm, it has different semantics which will cause you problems. For example, while rm requires -R for deleting directories trash-put does not.
- mekster 4y agoI have aliased "rm" with "trash-put" for years and I just can't believe anyone is living without a trash capability under CLI. What kind of ancient environment is it without a trash capability? That's pre Windows 95. How are people handling the mistake of "rm *" in a wrong folder? Added benefit of trash-put is, you don't need "-r" to delete a folder. It's just an annoyance from user's perspective. If you think there's a compatibility problem in a script, just use /bin/rm and stop wasting time pulling hairs on unrecoverable deletion. "@daily trash-empty 30" Put this in cron to purge trashes older than 30 days daily.
- kseistrup 4y agoAnother implementation is `garbage`: https://git.sr.ht/~mzhang/garbage https://git.sr.ht/~mzhang/garbage It is written in a compiled language and appears to be faster on my old computer.
- dmckeon 4y agoNo need for "$@" there? (Not many file names with spaces?)
- knorker 4y agoYou have a bug in your function. You need to use "$@", with the quotes, or wildcards expanding to a file with spaces will not delete properly. At least with bash.
- fsckboy 4y agoif you delete things things on a different volume (like an SD card), your function will move the files to your home volume, and if they're large files maybe not what you want.
- mmh0000 4y agoAll the comments here about how awful my script is were true! In Bash that didn't work on files with spaces or funky characters. But, I never noticed because in Zsh, my primary shell, it works fine with all filenames. Here's an updated one which incorperates the advice from several posts to also work in Bash: function rm { for file; do mv -t ~/.local/share/Trash/files/ -- "${file}" cat << FROG > ~/.local/share/Trash/info/"$(basename "${file}")".trashinfo [Trash Info] Path=$(realpath "${file}") DeletionDate=$(date "+%FT%T") FROG done }
- LinuxBender 4y agoThis gets even more quirky in busybox/ash especially if using `rm -Rf some_dir`. I think the function needs a more distinct name to protect rm use cases and avoid having to prefix wth \rm. maybe something like "function trash"