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?

1 Answer 1


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:

alias trash='mkdir -p "$HOME/.trash" && mv -b -t "$HOME/.trash"'

This creates an alias called trash that you would use instead of rm (but it doesn't really accept any of the options of rm). When you use

trash thisfile thatdir

what actually would be executed is

mkdir -p "$HOME/.trash" && mv -b -t "$HOME/.trash" thisfile thatdir

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 GNU mv 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

find "$HOME/.trash" -mindepth 1 -ctime +30 -delete 2>/dev/null

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.

  • yes the book is older than i would like and is heavily based on GNU. I didn't really understand how the alias took input until i read your example. That is simple as I expected. Thank you very much.
    – DanMan3395
    Jun 28, 2018 at 21:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .