recover files deleted with rm command

坚强是说给别人听的谎言 提交于 2019-12-24 08:26:19

问题


Please if I run the command

#  CREATE TRASH FOLDER

if ! [ -d "$HOME/deleted" ] ; then
     mkdir $HOME/deleted
fi

TRASH=$HOME/deleted
mv $@ $TRASH

To move file or directory to the trash created. what is the possible command i can run to recover same file to the original directory


回答1:


If you create a deleted directory like this, you will probably get some unexpected behavior. For example:

rm_script test.txt
cd ../other_directory
rm_script test.txt

will create a single file test.txt with the content of the one in other_dir. Even more fun when you start rm_scripting/moving directories.

Knowing this, and referring once more to trash-cli (see comment Aserre), you might:

if ! [ -d "$HOME/deleted" ] ; then
    echo "What a pity! No deleted directory."
    echo "Your file(s) is/are lost forever!"
    exit 361
fi

TRASH=$HOME/deleted
for file in  $@ ; do
    if [ -f "$TRASH/$file" ] ; then
        cp "$TRASH/$file" .
    else
        echo "Hmm,.. I cannot find $file."
    fi
done

This also may have some unwanted results, like removing the file from one directory and un-deleting it in another.




回答2:


I just do the reverse of the same command line

function undo () {
  echo -n "Do you want to recover $*? "
  read ANSWER
  if [ "$ANSWER" = "y" ]; then
    mv $TRASH/$@ $PWD
    echo "$@ successfully recovered"
  else
    echo "Oops, request denied"
  fi
}


来源:https://stackoverflow.com/questions/49029467/recover-files-deleted-with-rm-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!