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...
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.
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.
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?
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.
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)