I am working on an old linux learning project and one of the tasks is as outlined below:
"Create an alias that protects files from permanent deletion my moving them to a directory called .Trash. Make a command for crontab that will periodically delete the contents (even if they are directories) of .Trash that are older than 30 days."
I am expected to do this with an alias cli command. I am NOT allowed to use a function or write a small script that takes input.
First, is this possible? I assume yes because its in the book. Second, where to begin here? some of this is obvious, I have made the .Trash dir and can understand the concept of using an alias instead of rm but what cli actions could be placed in the alias to perform this action?
Alias trash=mv xxx .trash xxx
the above would require user input or variable information. What am I not seeing here?
Best Answer
Since it's a Linux thing, I'm assuming GNU
mv
is available. It has a-t
option that allows the target directory to be specified before the pathnames that should be moved.So, something simple:
This creates an alias called
trash
that you would use instead ofrm
(but it doesn't really accept any of the options ofrm
). When you usewhat actually would be executed is
This alias additionally creates the trash directory in the home directory if it does not already exist (or if it happens to have been deleted). It also uses
mv -b
(another GNUmv
only flag) to create backups in the trash folder in case a file with the same name already exists there.A cron job that cleans this trash directory of things that are old would execute something like
This would delete anything that is older than 30 days, avoiding the trash directory itself. Directories won't be deleted until all their contents is gone.