Bash 如何将保存在另一个文件中的文件还原到其原始路径?
我需要使用路径名将文件还原到其原始位置,路径名可以在我创建的.restore.info文件中找到 以下是删除脚本:Bash 如何将保存在另一个文件中的文件还原到其原始路径?,bash,shell,Bash,Shell,我需要使用路径名将文件还原到其原始位置,路径名可以在我创建的.restore.info文件中找到 以下是删除脚本: # did the user enter a filename if [ -z "$1" ]; then echo "No file was entered" exit fi # warns the user if a directory and not a file was entered if [ -d "$1" ]; then echo "Directory nam
# did the user enter a filename
if [ -z "$1" ]; then
echo "No file was entered"
exit
fi
# warns the user if a directory and not a file was entered
if [ -d "$1" ]; then
echo "Directory name entered. Please enter file name"
exit
fi
# if the correct file name is entered then display file deleted
if [ ! -f "$1" ]; then
echo "$1 Does not exist"
exit
fi
# finds inode of a file to avoid files with the same name
inode=$(stat -c '%i' $1) 2>/dev/null
# shows the path of the file
path=$(readlink -f "$1") 2>/dev/null
# prints name of the file and its path in the .restore.info file
echo "$1_$inode:$path" >> ~/.restore.info 2>/dev/null
# warns the user if they enter this script as an argument
if [ $1 = "remove123" ]; then
read -p "Are you sure you want to delete remove? y/n: " answer
#if yes, then remove the file otherwise do nothing
if [[ $answer = y ]] ; then
mv "$1" deleted/"$1"_"$inode" 2>/dev/null
echo "$1_$inode:$fixedPath" >> ~/.restore.info 2>/dev/null
else
exit
fi
fi
#moves to deleted dir
mv "$1" deleted/"$1_$inode" | cut -d " " -f 1 2>/dev/null
以下是到目前为止我在恢复脚本中的内容:
#!/bin/bash
#go to deleted directory
cd ~/deleted
# did the user enter the argument
if [ -z "$1" ]; then
echo "No file was entered"
exit
fi
# if the incorrect file name is entered then display file deleted
if [ ! -f "$1" ]; then
echo "$1 Does not exist"
exit
fi
#move restored file to the original path
original=$(grep "$1" .restore.info | cut -d ":" -f2 )
mv $1 $original
更新:我设法找到了如何从.restore.info中的文件中获取原始路径名。我使用了grep和cut命令,它们在命令行上工作,但在我的脚本中没有。我得到错误:
grep:.restore.info:没有这样的文件或目录
。你知道为什么吗?如果我正确阅读了你的第一个脚本,你要做的就是将完整的filespec(路径和文件名)写入.restore.info
文件,并在文件的文件名和inode前面加上前缀。然后将文件移动到“已删除”目录
要“取消删除”该文件,您需要根据.restore.info
文件中的filespec将该文件移回其来源的目录
在这种情况下,您不需要对.restore.info
文件进行读取链接;您只需要从中获取相关文件的正确路径。所以,像这样的东西会起作用:
restinfo=`grep "$1" ~/.restore.info`
echo $restinfo
filespec=${restinfo##*:}
echo $filespec
path=${filespec%/*}
echo $path
delname=${restinfo%:*}
echo $delname
第一行用还原文件中的整行加载restinfo
变量。然后我们抓取冒号(:
)后面的所有内容,并将其放入filespec中。这将是原始路径加上文件名。下一位获取该文件的路径部分,但由于您在“删除”文件时正在重命名该文件,因此我们需要使用完整的filespec。因此,我们并不真正需要“路径”,但见鬼的是。最后,我们获取已删除文件的名称(文件名加inode)。现在,我们有了“取消删除”该文件所需的一切:
mv deleted/$delname $filespec
我做了一些测试来检查这是否有效。首先,我创建了一个脚本来构建删除文件,然后创建了另一个脚本来解析恢复该文件所需的信息。以下是两个脚本:
tmp1()
{
# finds inode of a file to avoid files with the same name
inode=$(stat -c '%i' $1) 2>/dev/null
# shows the path of the file
path=$(readlink -f "$1") 2>/dev/null
# prints name of the file and its path in the .restore.info file
echo "$1_$inode:$path" >> test-restore.info 2>/dev/null
}
tmp2()
{
restinfo=`grep "$1" test-restore.info`
echo $restinfo
filespec=${restinfo##*:}
echo $filespec
path=${filespec%/*}
echo $path
delname=${restinfo%:*}
echo $delname
}
请注意,我为测试更改了还原文件的名称。以下是输出:
> rm test-restore.info
> tmp1 testin.txt
> tmp1 testout.txt
> cat test-restore.info
testin.txt_30412509:/home/sinasnet/test1/testin.txt
testout.txt_30412507:/home/sinasnet/test1/testout.txt
> tmp2 testin.txt
testin.txt_30412509:/home/sinasnet/test1/testin.txt
/home/sinasnet/test1/testin.txt
/home/sinasnet/test1
testin.txt_30412509
> tmp2 testout.txt
testout.txt_30412507:/home/sinasnet/test1/testout.txt
/home/sinasnet/test1/testout.txt
/home/sinasnet/test1
testout.txt_30412507
>
首先,我删除了还原文件,以确保它是空的。然后我“删除”了两个文件(刚刚加载了恢复文件),对文件执行了一个cat
,然后执行第二个脚本以获取“取消删除”文件所需的信息
顺便说一句,您的删除脚本有几个问题。首先,您将删除的文件移动到用户所在目录下的“已删除”目录。可能不存在这样的目录,如果存在,则假设用户在想要取消删除文件时将位于同一目录中。这是很多假设。最好使用特定的“已删除”目录,例如~/deleted
接下来,我不确定您在移动时使用cut
命令的意图是什么,但我认为它不会起什么作用
最后,加载一个path
变量,但不使用它。评论说你正在显示路径,但实际上它只是在加载它
无论如何,我希望这有帮助。我建议尝试
dirname
。dirname没有检索原始路径,而是检索.restore.info文件名。命令$cat.restore.info将列出我删除文件时发送到那里的文件及其原始路径,例如file1:home/user/file1。我只想要用户在“file1”中键入时冒号后面的路径。类似于sed的#[\:]*:\(.*/\).\\1#'“$1/.restore.info”
?请回答您的问题,并向我们展示还原文件中的几行内容,以及您在阅读它们时希望发生的情况。通过阅读您的代码来推断文件的外观并不容易。我找到了获取路径名的方法,但仍然无法使脚本正常工作。我已经更新了帖子以获取更多信息。