[SOLVED] Shell Scripting for Recycle Bin. Delete,Trash,Restore
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Shell Scripting for Recycle Bin. Delete,Trash,Restore
Hi everyone,
This is what I have done so far, but can't get it to work somehow. Anyone can please help?
Delete:
Code:
#!/bin/bash
# Delete script will move the file from its Original Location to the Dustbin Directory.
echo "Are you sure you want to move this file to the Recycle Bin?" "(Yes/No)"
read ans
case "$ans" in
Y|y|yes|Yes|YES) echo "`readlink -f $1`" >> ~/Paths & mv $1 ~/Dustbin
N|n|no|No|NO) echo "File still exist"
esac
# END OF THE SCRIPT
Trash:
Code:
#!/bin/bash
# Trash Script performs 2 functions, if the user uses the command 'trash -a' then this will empty the Recycle Bin(Dustbin Directory). Although if user uses only "trash" command then that will list the number of files present in the Recycle Bin and will let the user to choose which file to delete.
if ["$1" == "-a"]
then
cd ~/Dustbin
rm ~/Dustbin/*
cd ~/
rm Paths
touch Paths
else
cd ~/Dustbin
ls -1
for line in `ls`
do
echo "Are you sure you want to permanently delete this file?" $line "Yes/No"
read ans
case "$ans" in
Y|y|yes|Yes|YES) rm $line
N|n|no|No|NO) echo "File still exist!"
esac
done
cd
rm Paths
touch Paths
fi
# END OF THE SCRIPT
Restore
Code:
#!/bin/bash
# Restore Script will move the file back to its Original Directory from the Dustbin Directory.
cd ~/Dustbin
restore =`grep $2 ~/Paths`
filename = `basename "$restore"`
echo "Where would you like to save this file?"
read location
location1 = `readlink -f $location`
mv -i $filename "$location1"/$filename
# END OF THE SCRIPT
I mean, I just want it to me simple and elegant and not looking forward to do some high level of scripting and stuff. Any help where am getting wrong?
Last edited by colucix; 11-27-2012 at 03:10 PM.
Reason: Original content restored
a) a case entry should end with ;;
b) variables should be in double quotes
c) I would also recommend using the "$()" over backticks
d) use $HOME instead of ~ in scripts
e) Also note the difference between & (forking to background) and && (running if previous command was successful)
a) prefer = to == in [ ]
b) spaces are needed between braces ([ and ] are both keywords that need to be read as separate tokens by bash)
c) unless you want backwards compatibility, prefer [[ ]] to []
5) What is the point of this?
Code:
cd ~/Dustbin
6)
Code:
rm ~/Dustbin/*
What about directories?
7)
Code:
for line in `ls`
don't do that. It will break if any of the file names contains spaces. use * instead:
Code:
for line in *
8)
Code:
echo "Are you sure you want to permanently delete this file?" $line "Yes/No"
While perfectly correct, there is no need to exclude the variable from the double quotes. It will get substituted just fine:
Code:
echo "Are you sure you want to permanently delete this file? $line Yes/No"
Let alone the fact that $1 should be in double quotes, didn't you mean $1?
Anyway, the Paths file probably doesn't exist anymore since you may have deleted it by the second script. What was it for again?
Delete Script is working smashing after your help. Another question, is there any way I can edit that script and that will let me delete the directories also?
THE SECOND SCRIPT:
7)
Code:
for line in *
The way I want it work is, when user types trash, it will give the list of all the files present in the Dustbin Directory. And then it will ask one by one from the user to choose which one does the user want's to delete it. You getting me?
So for instance,
Code:
#trash
1
2
3
Are you sure you want to delete this file permanently? "1" (Yes/No)
y
Are you sure you want to delete this file permanently? "2" (Yes/No)
n
Are you sure you want to delete this file permanently? "3" (Yes/No)
y
And this will delete 1 and 3 and keep 2. this is what I'm actually wanting to do. Now, the script I have just now, its going like this way...
Code:
cd $HOME/Dustbin
ls -1
for line in *ls
do
echo ".... $line .."
read ans
case "$ans" in
y) rm $line ;;
n) ehco "..." ;;
esac
done
cd ..
fi
Now the output for this is ..
Code:
# trash
1
2
3
... *ls (Yes/No)
y
rm: cannot remove '*ls': No such file or directory
n #for instance
File Still Exist.
Now how I can fix this please?
10)
Code:
rm Paths
Paths is keeping record of Orginal location of the file, so for instance if the user want's to restore a file from the Recycle Bin, the file will then sent back to its Original Location.
THE LAST SCRIPT:
The Paths file keeps hold of the Original Location of the Deleted file which is sitting in the Recycle Bin.
You're correct that it is getting deleted in Script 2 which it should be, because if user deletes a file, it goes to the bin and then he can later restore to it its original location but when the user deletes it using "trash", then it will remove the file as no point of keeping record of the deleted file and instead it will create a new empty file in the same location.(I hope it make sense?)
And how I can fix this now? as just now, restore isn't working at all! Thanks
Last edited by colucix; 11-27-2012 at 03:11 PM.
Reason: idem
Another question, is there any way I can edit that script and that will let me delete the directories also?
to remove a directory, use either rmdir, which only works for empty directories, or rm -r, which deletes the directory and everything it contains. Use with caution.
Quote:
Originally Posted by mohitnarula
The way I want it work is, when user types trash, it will give the list of all the files present in the Dustbin Directory. And then it will ask one by one from the user to choose which one does the user want's to delete it. You getting me?
Code:
for line in *ls
Yes, I get it. The asterisk is a glob. It is substituted by bash by the list of all file names that match the pattern (except for hidden files). The advantage is that the word splitting will not split file names that contain spaces and other unfriendly characters. So the correct form is
Code:
for line in *
do
...
if you're in the Dustbin directory, or
Code:
for line in Dustbin/*
do
...
Quote:
Originally Posted by mohitnarula
Paths is keeping record of Orginal location of the file, so for instance if the user want's to restore a file from the Recycle Bin, the file will then sent back to its Original Location.
THE LAST SCRIPT:
The Paths file keeps hold of the Original Location of the Deleted file which is sitting in the Recycle Bin.
You're correct that it is getting deleted in Script 2 which it should be, because if user deletes a file, it goes to the bin and then he can later restore to it its original location but when the user deletes it using "trash", then it will remove the file as no point of keeping record of the deleted file and instead it will create a new empty file in the same location.(I hope it make sense?)
The point is, if you use trash, you have a choice to either delete each file or to keep it. The Paths file gets deleted regardless. So for example, if I choose to delete one file, but decide to keep the rest, the Paths file gets deleted but there are still files in the Dustbin. Also, you are ignoring the original location of the file, so what you are basically doing is just checking whether the Paths file thinks the file is in the Dustbin. Did you remove the spaces from the assignments?
1. These look basic scripts, but you need to work on scripting little more. I assume that Paths file contains a simple list of files that were deleted. Codes could be short and simple, as:
Trash:
PHP Code:
#!/bin/bash if ["$1" == "-a"]; then rm ~/Dustbin/* else while read -r line do rm=$(rm -i) rm $line done < ~/Paths fi
Restore:
PHP Code:
#!/bin/bash while read -r filename do echo "Where would you like to save this file?:" read location mv -i $filename $location done < ~/Paths
2. Do not expand your codes unnecessory, when work can be finished in a single shot. like doing same thing in 2 steps is not needed:
Quote:
cd ~/Dustbin
rm ~/Dustbin/*
3. Instead of using case statement, you can use "rm -i" which works interactively and asks you before deleting the file. You can define rm=rm -i in script itself or can create it's alias in your working shell.
You're still missing the spaces within the brackets
Code:
rm=$(rm -i)
rm $line
I am pretty sure this won't do what you wanted to do. You're storing the output of the rm -i (which will fail due to missing operand) in a variable called rm. Anyway, it is not recomended to store commands in variables.
Thanks both of you for your help, now I have managed to get Delete and Trash working perfect, but restore still ain't working, I am posting my (current) scripts just now so that we can now have a clear look and see where I am getting wrong, as it is getting kinda messed up just now.
DELETE
Code:
#!/bin/bash
# Delete script will move the file from its Original Location to the Recycle Bin(Dustbin Directory)!
echo "Are you sure you want to move this file to the Recycle Bin? (Yes/No)"
read ans
case "$ans" in
Y|y|yes|Yes|YES) echo "$(readlink -f "$1")" >>"$HOME/Paths" && mv "$1" "HOME/Dustbin" ;;
N|n|no|No|NO ) echo "File Still Exist!" ;;
esac
# END OF THE SCRIPT
TRASH
Code:
#!/bin/bash
# Trash Script performs 2 functions, if the user uses the command 'trash -a' then this will empty the Recycle Bin(Dustbin Directory).
if ["$1" = "-a"]
then
rm ~/Dustbin/*
echo "All files have been deleted successfully!"
rm Paths
touch Paths
else
cd ~/Dustbin
ls -1
for line in *
do
echo "Are you sure you want to delete this file permanently? $line (Yes/No)"
read ans
case "$ans" in
Y|y|yes|Yes|YES ) rm $line ;;
N|n|no|No|NO ) echo "File Still Exist!" ;;
esac
done
cd ..
fi
# END OF THE SCRIPT
@millgates, as I wrote in previous comment, user can define an alias of rm in his working shell which will work. Else @mohitnarula, instead of these 2 line, you can use a single line to interactively remove a file as:
PHP Code:
#!/bin/bash if ["$1" == "-a"]; then rm ~/Dustbin/* else while read -r line do rm -i $line done < ~/Paths fi
Quote:
...but restore still ain't working
If ~/Paths contains a simple list of files, then your job could be done with:
PHP Code:
#!/bin/bash while read -r filename do echo "Where would you like to save this file?:" read location mv -i $filename $location done < ~/Paths
Did you try this? Or else share the error messages you're getting while using restore script.
Shivaa I appreciate your help, but the thing is right, it took me alot of time to get atleast those 2 scripts right. And I actually don't feel comfortable changing them unless it will make my Restore script to work. So I mean as I said, I do appreciate but because they are working fine just now I don't see the point of changing them to something else.
Whereas, the Restore Script is concerned, I am actually not getting any error message but it just doesn't seems to work! In clear words, It's not restoring the file back to it's original location.
That's why I again posted my Scripts after making all the changes, so that we can just have a look at it and see where it is getting wrong, as to me it should work and move the file from Dustbin Directory to back to its Original Location.
Thanks a ton!
Last edited by colucix; 11-27-2012 at 03:13 PM.
Reason: idem
...but because they are working fine just now I don't see the point of changing them to something else.
It's up to you. But changing rm=$(rm -i); rm $line to rm -i $line will not make any difference.
Quote:
...I am actually not getting any error message but it just doesn't seems to work!
In that case, use set -xv just below the interpreter line in restore script and then invoke the script again. It will show you the execution of the script line-by-line. You can then find out the wrong part of it.
PHP Code:
#!/bin/bash set -xv while read -r filename do echo "Where would you like to save this file?:" read location mv -i $filename $location done < ~/Paths
Last edited by shivaa; 11-24-2012 at 10:55 PM.
Reason: Typo
In that case, use set -xv just below the interpreter line in restore script and then invoke the script again. It will show you the execution of the script line-by-line. You can then find out the wrong part of it.
#!/bin/bash
set -xv
while read -r filename
do
echo "Where would you like to save this file?:"
read location
mv -i $filename $location
done < ~/Paths [/PHP][/QUOTE]
NOW after adding set -xv to the Restore Script..
I have attached the screenshot showing the Output.
Thanks!
Last edited by colucix; 11-27-2012 at 03:14 PM.
Reason: idem
Make sure which script you're running? The one I suggested or your own? And do not stick to an approach when it's not helping & there's another way availale to achive results. I don't understnad why you're using restore =`grep $1 $HOME/Paths` and ifilename=`basename $restore "` variables & getting same problem again & agian:
Code:
ls ~/Dustbin # What's need of using this?
abc def ghi
# restore abc
restore =`grep $1 $HOME/Paths`
ifilename=`basename $restore "`
+ ifilename=`basename $restore "`
location1=`readlink -f $location1`
+ location1=`readlink -f $location1`
# End
I again suggest to try this one:
Let's say ~/Path contains:
abc
def
ghi
Then you can try:
Code:
#!/bin/bash
while read -r filename
do
echo "Where would you like to save this file?:" ## Enter full path of restore location
read location
mv -i ~/Dustbin/$filename $location
done < ~/Paths
Paths is a name of a file which is keeping record of the "Original Location of a file" as then it can be used to Restore the file back to that location.
Have a look at this screenshot, it tells you what's happening just now.. and if you thinking changing my Restore script with your will help me get outta here then I ain't got any problem with that..
Thanks
Last edited by colucix; 11-27-2012 at 03:14 PM.
Reason: idem
Where's the first time posted question? Have you removed that? It's not admissible at all that you remove your original question!
And you're sharing output of your own code (which is incorrect and carries lot of misconception) again & again, so I'm not willing to give same answer again & again. If you can't try what other have suggested to you, then do not expect solution!
I didn't even had a look at my Original Question, I dunno where it is.. plus I was not able to connect to this website from last couple of minutes.. it was just coming up "PAGE NOT FOUND"...
Sorry Shiva, if you think I'm being mean, that's not my intention. I'm here to Learn Linux, and solve as much problems as I can, am not saying what you're saying is wrong or it won't work but I wasn't too sure if it will do what I want to do. (You getting me)
I'll just change it just now and see what it does what you are saying....
Thanks!
Last edited by colucix; 11-27-2012 at 03:14 PM.
Reason: idem
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.