darumpa darumpa - 11 days ago 6
Linux Question

restoring a file to their original location from a log file with paths

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

Answer

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