Sunday, November 30, 2008

How to have Undelete on your Linux command line.

Almost all distro now has a Trash to undo your current deleted files when using the GUI file browsers.

When you spend most of your time on the shell you usually use the command "rm" to delete a file and this command doesn't support undelete once you issued the command.

First we need to learn not to use the command 'rm' to delete our files.

In your ~/.bashrc file add the following line:
alias del='mv -t ~/.local/share/Trash/files --backup=t'
Deleting files now is done via
del filename.txt
.

Then we need a script to clean up our trash folder occasionally.
Create a script and name it whatever you want, I named mine "cleantrash" and have the code below inside it.



#!/bin/bash
#
# This will delete all files in the trash directory
# that is older than the specified KEEPDAYS
#
# It can be run using a cron job or manually
#
TRASHDIR=~/.local/share/Trash/files/
KEEPDAYS=14
find $TRASHDIR -mtime +${KEEPDAYS} -exec /bin/rm -f {} \;


Make sure you do a chmod +x cleantrash so we can execute it.

No comments: