Moving files to 'trash'

Hi I'm new to Linux, and I'm trying to write a shell script where I remove a specific file (i.e file1 or file1.txt), but i don't actually remove it, i move it to a directory called 'trash' - in vi I created a file with the path /usr/bin/del and in it I have:

mv $file /home/trash, but I'm getting an error message that says missing argument.

I also tried mv *.* /home/trash, which sort of worked and I thought I had got it until I realised it moved ALL the files in the directory I was in to the 'trash' directory...

Any help would be awesome.

Jodi

Hi Jodi

Methinks you need to read and understand a book on Linux before you wreck your system.

A reminder about filenames:
Long after the creation of Unix came MSDOS from M$.
In MSDOS (which became M$ Windows) the suffix of the filename conveyed the file type. This does not apply to Unix and Linux though there are
informal conventions. The fullstop character is just another character in the filename.

Assuming your script /usr/bin/del contains the one line
mv $file /home/trash
This is not enough.
Unless there is a value in the variable $file , the command will fail.

Unfortunately because you appear to have no basic training I cannot continue to advise.

I don't care how much training you have, or if you destroy your system/server/etc..

So here we go! Put that in your .bashrc file. An alias (from what I can tell/find) can not have variables, but you can use a function and it acts just like an alias, except a lot more powerful.

The code below accepts one argument.. it tests if the argument provided is a normal file that exists, and if it is, moves it to ../trash (actually thats commented out), if the file is not a normal file, or does not exist, it will say no such file. This code can EASILY be modified, and expanded.. its not that great at all, but I am going to sleep. Have fun!

function delstuff() {
        if [ -f $1 ]; then
                echo "Moving file to trash..."
                # mv $1 ../trash
        else
                echo "No such file.";
        fi
}

Alright there guys, I have a question, obviously, and that would be -

So I don't completely screw up my system, I made a 'dustbin' directory, and made a script where instead of removing the files completely they moved the unwanted files to that directory. Sort of like Windows moving it to the Recycle bin, I guess. But I was wondering, if anyone knew of a script that I could use to restore the files back to their original directory - I tried to attempt this myself, but I got confused (it happens easily lol) - Any help would be smashing.

Jayden

It would basically be the same as the delete script..

function restfile() {
    if [ -f ../trash/$1 ]; then
        mv ../trash/$1 .
    else
        echo "That file is not in the trash.";
    fi
}

I would suggest an absolute path, but I am not sure exactly where you have the trash bin. This script can easily be improved.

I have the dustbin file located within the home directory, so /home/dustbin

I tried the code you suggested but it's giving me a list of restore options with different flags

restore 0.4b39 (using libext2fs 1.35 of 28-Feb-2004)
usage: restore -C [-cdlMvVy] [-b blocksize] [-D filesystem] [-f file]
[-F script] [-L limit] [-s fileno]
restore -i [-acdhlmMouvVy] [-A file] [-b blocksize] [-f file]
[-F script] [-Q file] [-s fileno]
restore -P file [-acdhlmMuvVy] [-A file] [-b blocksize]
[-f file] [-F script] [-s fileno] [-X filelist] [file ...]
restore -r [-cdlMuvVy] [-b blocksize] [-f file] [-F script]
[-s fileno] [-T directory]
restore -R [-cdlMuvVy] [-b blocksize] [-f file] [-F script]
[-s fileno] [-T directory]
restore -t [-cdhlMuvVy] [-A file] [-b blocksize] [-f file]
[-F script] [-Q file] [-s fileno] [-X filelist] [file ...]
restore -x [-acdhlmMouvVy] [-A file] [-b blocksize] [-f file]
[-F script] [-Q file] [-s fileno] [-X filelist] [file ...]

Which is double dutch to me.

Alright.. you need to make sure that you put that in your .bash_profile file, then login to the shell again.

You can't name the function restore() as that is an actual command in *nix (not sure which versions it is actually in), you can type: man restore

Name it restfile or undelete or something, I am not sure, but do not use restore. :slight_smile:

looks sheepish Ignore that above reply please - I realised my mistake

Thanks for your help Rhije I changed the name and it worked - I actually did figure it out lol, and felt completely stupid. But thanks for your help mate.

Oh no problem, we all make silly mistakes that we realize. haha, Have fun!

Sorry to bug you again, but I was wondering if it was possible to restore the file to a different directory, like of my choosing? Would that require a elif part in the code? with a mv flag?

Yeah you can just use another parameter... umm something like

if [ -z $2 ]; then
  mv $1 .$2
else 
  mv $1 .
fi

Or something like that. the -z tests the variable if it is NULL or not.

I changed my code to this:

if [ $1 ]; then
echo "Restoring File..."
mv /home/dustbin/$1 .

elif [$1 "-z" ] ; then
mv $1 .$2

elif
echo "That file doesn't exist.";
fi

And I get an error, any idea what i'm doing wrong?

Well, what error did you get?

Also, the last elif does not need to be an elif (in fact it makes no sense), use else

Elif needs something to test against, you are giving it nothing. Use else.

I got:

syntax error near unexpected token `then' and
` else [$1 "-z" ] ; then'

and thanks for the info about the last elif

Ok.. hold on.

If and Elif need to have something to test.

ELSE does not, do not put anything to test for, it is saying "If any of the other tests failed.. this is what will be done"

Please put your code in code tags..

Fixing your code, it will look like this:

if [ $1 ]; then
     echo "Restoring File..."
     mv /home/dustbin/$1 .
elif [$1 "-z" ] ; then
     mv $1 .$2
else
   echo "That file doesn't exist.";
fi

Don't know where I'm going wrong mate, first part works fine but the - z just ain't working for me, but anyways thanks for your help, at least i won't delete all my files lol - and sorry about not putting the code in the code thing.

[LEFT]ah its ok.

Just let us know if you need anymore help!!
[/LEFT]

Okay, so I tried:

if [ $1 ]; then
     echo "Restoring File..."
     mv /home/dustbin/$1 .
elif [$1 "-z" ] ; then
     mv $1 .$2
else
   echo "That file doesn't exist.";
fi

but it gave me:

Restoring File...
mv: cannot stat `/home/dustbin/-z': No such file or directory
mv: cannot stat `file': No such file or directory (the file is a file I have in my dustbin directory as a test)

It's just really bugging me lol

it is..

elif [ -z $1 ]; then

check out File test operators