LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Trash & Restore Scripting Issue (https://www.linuxquestions.org/questions/linux-newbie-8/trash-and-restore-scripting-issue-4175526493/)

Thomzin 11-25-2014 03:29 PM

Trash & Restore Scripting Issue
 
Hi there, new to the forums.

I'm currently working on two scripts. The first takes a file and sens it to a folder named "Trash", as a method of file deletion. This works fine. Note this script also takes the "original file path" and stores it into a file.

The 2nd script is the issue. I'm currently trying to take the file path from file and restore the file to it's original destination. I will attach both scripts for your input.

Code:

echo "Are you sure you want to move this file to the Recycle Bin?" "(Yes/No)"
read ans
case "$ans" in
Yes) echo "$(readlink -f "$1")" >> "$HOME/TrashLog" && mv "$1" "$HOME/my-documents/mydir01/Trash" ;;
No) echo "File was NOT deleted and remains. "
esac

this is working fine ^

Code:

#!/bin/bash

#Restore Script - this script will restore a previously deleted file (in trash). Back to it's original location.

#restore="$(grep "$1" "$HOME/Paths")"

#filename="$(basename "$restore")"

#location1="$(readlink -f "$location")"

mv -i "$1" "$(grep "$1" "$HOME/TrashLog")"

# End

Note i commented out a previous attempt.

Thankyou very much in advance if you do take the time to assist me. I understand it's a very simplistic issue, but i'm just starting out and a little confused!

szboardstretcher 11-25-2014 04:00 PM

With a couple of changes, and using only your original code, this works:

Code:

#!/bin/bash
echo "Are you sure you want to move this file to the Recycle Bin?" "(Yes/No)"
        read ans
        case "$ans" in
                Yes) echo "$(readlink -f $1)" >> "$HOME/TrashLog" && mv "$1" "$HOME/my-documents/mydir01/Trash" ;;
                No) echo "File was NOT deleted and remains. ";;
esac

Code:

#!/bin/bash
restore=$(find my-documents/* | grep $1)
filename=$(readlink -f $restore)
mv -i "$filename" "$(grep $1 $HOME/TrashLog)"

But, in the future, you will have a lot of problems with this script. There are a lot of ways to break it in its current form.

Thomzin 11-25-2014 04:07 PM

Code:

#!/bin/bash
restore=$(find my-documents/* | grep $1)
filename=$(readlink -f $restore)
mv -i "$filename" "$(grep $1 $HOME/TrashLog)"

But, in the future, you will have a lot of problems with this script. There are a lot of ways to break it in its current form.[/QUOTE]

I had a look at this, and tested it. I actually was returned with an error.

(There is a file in Trash currently called "Derp".)

I did.. restore derp. In the console. I was returned with..
"find: `my-document/*': No such file or directory
readlink: missing operand
Try 'readlink --help' for more information.
mv:cannot stat `/root/my-documents/mydir01/derp: no such file or directory

Super confused :/

Is this a permissions issue? or something not quite working with the original code which is causing issues later with my restore?

Thanks for your time.

Thomzin 11-25-2014 05:20 PM

Still very eager for any assistance someone could shed? Ripping my hair out currently!

Thomzin 11-25-2014 05:37 PM

A friend gave me this segment of script here, which looks like it could be useful, this also doesn't work. However the error i get is one of which it claims the file cannot be moved as there is no such file or directory. I think we're getting close?

Code:

mv -i "$1$ "$(grep "$1" "$HOME/TrashLog")"

Thomzin 11-25-2014 06:37 PM

update: after some help from other sources. I'm now at this stage..

#!/bin/bash

Code:

cd "$HOME/my-documents/mydir01/Trash" || exit 1
mv -i "$(grep "$1" "$HOME/TrashLog")"

again any help at this stage would be amazing.


All times are GMT -5. The time now is 12:45 PM.