0

I have made 1 script to move user specified files to the dustbin and create a log file with their original paths. Now I want to create a script that the user would only have to input the name of the file to restore it to where it was before and I cannot figure that out. Here is the code so far:

delete script:

#!/bin/sh
#checks if the user has entered anything.
#if not displays message.
if [ $# -eq 0 ]; then               #reads the number of characters
        echo "Usage: del <pathname>" >&2
        exit 2;
fi
#location of the dustbin
dustbin="$HOME/dustbin"
paths="$HOME/Paths"
if [[ ! -d $dustbin ]]; then    #checks if the dustbin exists
mkdir $dustbin
    else
        [[ -p $dustbin ]]           #if dustbin exists does nothing
fi
#creates a Paths folder to store the original location of file(s)
if [[ ! -d $paths ]]; then  #checks if the Paths folder exists
mkdir $paths
    else
        [[ -p $paths ]]         #if Paths folder exists does nothing
fi
#gets just the file name 
for file in "$@"; do                #reads all the arguments
    if [[ -e $file ]]; then         #checks if the file name exists
#moves the file(s) to the dustbin and writes the orginal file path to the paths.txt file.
 find $file >> $HOME/Paths/paths.txt && mv "$file" "$dustbin"
        echo "Deleting file(s) $file"
    else 
        echo "The file $file doesn't exist." 
    fi
done 

restore script: With this I need to search for the file in the dustbin, match the file name to the paths text file that has the files original path and move to the said path.

#!/bin/sh
#checks if the user has entered anything.
#if not displays message.
if [ $# -eq 0 ]; then
        echo "Usage: restore <File name>" >&2
    exit 2;
fi

#checks if the file paths.txt exist
paths="$HOME/Paths/paths.txt"
    if [[ ! -f $paths ]]; then  #checks if the Paths file exists
        echo "The log file paths.txt doesn't exist. Nothing to restore"
fi

#takes the user input checks if the dustbin exists.
for file in "$@"; do
if [[ ! -d dustbin ]]; then
    echo "dustbin doesn't exist"
        else
            cd $HOME/dustbin
fi

#looks for the user specified file.
if [[ ! -e $file ]]; then
    echo "File $file doesn't exist"
        else
#restores the file to the original location
restore="grep -n '$file'  $paths"       #no idea how to do it
mv $file $restore
fi
done

this part I have no idea how to do. I need it to read the user input in $file from the paths.txt and use that stored path to move the $file from the dustbin to the file path stored in the paths.txt file.

#restores the file to the original location
restore="grep -n '$file'  $paths"       #no idea how to do it
mv $file $restore
darumpa
  • 3
  • 3
  • This question is a good start, but incomplete. Please read [MCVE](http://stackoverflow.com/help/mcve). What specifically are you stuck on, or what is producing errors that you don't understand? – ghoti Nov 25 '16 at 15:37
  • Also, you've tagged your question **[tag:bash]**, but your script starts with `#!/bin/sh`, which means you're either running a non-bash shell, or you're running bash in compatibility mode. Either update your shebang or your script (and tags) accordingly. – ghoti Nov 25 '16 at 15:39
  • yeah sorry about that, removed the bash tag – darumpa Nov 28 '16 at 09:15
  • Implementing an undelete might be a bit more complicated than just moving them to a directory. Files with same name and different paths would collide in this solution, so you would have to build a database-like structure with numerical file names in the end. Desktop environments already did that, and if your users are using them, you should have a look at `trash-cli` or `gvfs-trash`. – Karsten Koop Nov 28 '16 at 09:35
  • See also [this question on unix.stackexchange](http://unix.stackexchange.com/questions/42757/make-rm-move-to-trash) – Karsten Koop Nov 28 '16 at 09:42

2 Answers2

0

So, I think you will want to move the file back to where it originally was using mv.

mv "dustbinPath/$file" "orginalPath/$file"

This will move it from the dustbin path to the originalPath.

EDIT:

If you want to grep the path file for it, you can set a variable to the output of a command like:

originalPath=$(grep 'what_to_grep' file_to_grep.txt)

After you do that that use it in the mv above appropriately (whether the text file contains that file name or not) to move it out.

You can read more about setting a variable to the output from a command here. You might have problems if there are multiple lines that have it however.

Community
  • 1
  • 1
jumper
  • 74
  • 1
  • 9
0

In the del script I changed find to realpath so it looks like this:

#gets just the file name 
for file in "$@"; do                #reads all the arguments
    if [[ -e $file ]]; then         #checks if the file name exists
 realpath $file >> $HOME/Paths/paths.txt && mv "$file" "$dustbin"       
        echo "Deleting file 'basename $file'"
    else 
        echo "The file $file doesn't exist." 
    fi
done

and the restore script I added a new variable

rest="$(grep -e $file $paths)"          #looking in to the paths.txt file for filename match

#looks for the user specified file in the dustbin.
if [[ ! -e $dustbin/$file ]]; then
    echo "File $file doesn't exist"
    elif [[ -e $rest ]]; then       #if the $file exists in the original path adds an extension .bak
    mv $dustbin/$file $rest.bak
    else
    mv  $dustbin/$file $rest        #restores the file to the original location
    echo "$file restored"
fi
darumpa
  • 3
  • 3