I have no idea what is wrong with my simple script.

I am doing a simple "recycle bin" script. It is to check and see if the directory .TRASH exists within the home directory. If not then it makes the directory then appends a date and time to file and then finally moves the file in. However, when I run this script, it is not making the directory as it should.

What am I missing?

#!/bin/bash
if [ -d "~/.TRASH" ]
then
        mkdir ~/.TRASH
fi

DATE=$(date +'_%Y%m%d_%H%M')
mv $PWD/"$1" "~/.TRASH/${1}.${DT}"

First, omit double quotes in the if condition. Second, you need to put negation in there to make it work. Try it like this:

if [ ! -d ~/.TRASH ]

Perfect. I have no idea why I quoted and could not see it. I am just getting my feet wet. I appreciate the help, it worked beautifully. Now I have a folder .TRASH and files are moved in with an appended date/time. Just need to alias it to RM now.

Thanks again!

EDIT: I spoke too soon. Upon looking in the directory created, it is not appending the date/time to the files uggh. I was doing this to avoid file collision, so this will not work.

---------- Post updated at 05:22 PM ---------- Previous update was at 05:18 PM ----------

Uggh, spoke to soon, dates not appending.

Try:

mv $PWD/"$1" "$HOME/.TRASH/${1}.${DATE}"

Wow, I have so much to learn and understand. That did do it.

---------- Post updated at 05:35 PM ---------- Previous update was at 05:34 PM ----------

Makes sense too, now that I see it.

Highly appreciate it.

Just a note (and my personal opinion) while this is a good exercise, YOU SHOULD NOT ALIAS RM. The last thing you want to do is forget how powerful it is, and more importantly how permanent it is. Seems like no big deal, but then that one day you carelessly delete something...

One thing you could do though if you still want to proceed is make it so that before it rm's whatever, it performs an ls, prints the output and asks you to confirm that you want to delete.