LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell Scripting for Recycle Bin. Delete,Trash,Restore (https://www.linuxquestions.org/questions/linux-newbie-8/shell-scripting-for-recycle-bin-delete-trash-restore-4175438482/)

mohitnarula 11-23-2012 05:13 PM

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?

millgates 11-24-2012 03:51 AM

1) use [code][/code] tags when posting code

2) What exactly does "Can't get it to work" mean?

3)
Code:

Y|y|yes|Yes|YES) echo "`readlink -f $1`" >> ~/Paths & mv $1 ~/Dustbin
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)

Code:

Y|y|yes|Yes|YES) echo "$(readlink -f "$1")" >> "$HOME/Paths" && mv "$1" "$HOME/Dustbin" ;;
THE SECOND SCRIPT:

4)
Code:

if ["$1" == "-a"]
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"
9)
Code:

Y|y|yes|Yes|YES) rm $line
again, quote variable and end with ;;
10)
Code:

rm Paths
ehm, what was the point of the Paths file again?

THE LAST SCRIPT:

11)
Code:

restore =`grep $2 ~/Paths`
filename = `basename "$restore"`
location1 = `readlink -f $location`

No spaces should be around =

12)
Code:

grep $2 ~/Paths
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?

mohitnarula 11-24-2012 09:43 AM

Thanks for your help, much appreciated!
 
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

millgates 11-24-2012 11:31 AM

Quote:

Originally Posted by mohitnarula (Post 4836064)
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 (Post 4836064)
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 (Post 4836064)
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?

shivaa 11-24-2012 11:36 AM

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.

4. It's recommended to use $() over backticks while defining variables. Go through: http://mywiki.wooledge.org/BashFAQ/082

millgates 11-24-2012 01:14 PM

Code:

if ["$1" == "-a"];
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.

mohitnarula 11-24-2012 08:53 PM

Thanks for your help!
 
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

RESTORE

Code:

#!/bin/bash

# Restore


restore='grep $1 $HOME/Paths'

filename='basename "$restore"'

location1='readlink -f $location'

# End

Thanks for your help again.

shivaa 11-24-2012 09:06 PM

Code:

rm=$(rm -i)
rm $line

@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.

mohitnarula 11-24-2012 10:34 PM

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!

shivaa 11-24-2012 10:54 PM

Quote:

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


mohitnarula 11-24-2012 11:12 PM

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!

shivaa 11-24-2012 11:40 PM

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


mohitnarula 11-24-2012 11:55 PM

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

shivaa 11-25-2012 12:25 AM

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!

mohitnarula 11-25-2012 12:41 AM

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!


All times are GMT -5. The time now is 04:35 PM.