从包含路径的日志文件将文件还原到其原始位置

时间:2016-11-25 15:29:21

标签: linux sh

我制作了一个脚本将用户指定的文件移动到垃圾箱并使用原始路径创建日志文件。现在我想创建一个脚本,用户只需输入文件的名称就可以将其恢复到以前的状态,我无法弄明白。 这是迄今为止的代码:

删除脚本:

#!/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 

恢复脚本: 有了这个,我需要在垃圾箱中搜索文件,将文件名与具有文件原始路径的路径文本文件相匹配,然后移动到所述路径。

#!/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

这部分我不知道该怎么做。我需要它从paths.txt读取$ file中的用户输入,并使用该存储的路径将$ file从垃圾箱移动到存储在paths.txt文件中的文件路径。

#restores the file to the original location
restore="grep -n '$file'  $paths"       #no idea how to do it
mv $file $restore

2 个答案:

答案 0 :(得分:0)

因此,我认为您需要将文件移回原来使用mv的位置。

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

这会将它从垃圾箱路径移动到originalPath。

编辑:

如果要为其路径文件grep,可以将变量设置为命令的输出,如:

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

执行此操作后,在上面的mv中适当地使用它(无论文本文件是否包含该文件名)将其移出。

您可以阅读有关将变量设置为命令here的输出的更多信息。如果有多行拥有它,你可能会遇到问题。

答案 1 :(得分:0)

在del脚本中,我将find更改为realpath,所以它看起来像这样:

#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

和恢复脚本我添加了一个新变量

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