Advertisement
ZhenyaDudko

Untitled

Nov 26th, 2022
1,217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.57 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. filename=""
  4.  
  5. if [[ ! -f ~/.trash.log ]]
  6. then
  7.   echo "No files in trash"
  8.   exit
  9. fi
  10.  
  11. if [[ $# -ne 1 ]]
  12. then
  13.   echo "Expected one param: name of file to restore"
  14.   echo "Name of file to restore: "
  15.   read filename
  16. else
  17.   filename=$1
  18. fi
  19.  
  20. while [[ "$filename" == *\0* || "$filename" == *\/* ]]
  21. do
  22.   echo "Wrong filename"
  23.   echo "Name of file to restore: "
  24.   read filename
  25. done
  26.  
  27. linkname=""
  28. file_untrash=""
  29.  
  30. exec 3<&0
  31.  
  32. line_counter=0
  33. while read line
  34. do
  35.   line_counter=$((line_counter+1))
  36.   current_linkname=$(echo $line | awk '{print $NF}')
  37.   current_filename=${line% $current_linkname}
  38.  
  39.   if [[ $current_filename == */$filename ]]
  40.   then
  41.     echo "Do you want to restore this file: " $current_filename "? Print Y/N or another to exit"
  42.     read answer <&3
  43.     case $answer in
  44.     "Y")
  45.       file_untrash=$current_filename
  46.       linkname=$current_linkname
  47.       break
  48.       ;;
  49.     "N") continue ;;
  50.     *) exit ;;
  51.     esac
  52.   fi
  53. done < ~/.trash.log
  54.  
  55. if [[ $file_untrash == "" || ! -f ~/.trash/$linkname ]]
  56. then
  57.   echo "No files to restore found"
  58.   exit
  59. fi
  60.  
  61. file_dir=${file_untrash%$filename}
  62. if [[ ! -d "$file_dir" ]]
  63. then
  64.   echo "Original directory does not exist. Trying to restore in HOME directory"
  65.   file_dir=$HOME
  66. fi
  67.  
  68. while [[ -f $file_dir/$filename ]]
  69. do
  70.   echo "File with such a name already exists. Print another one for restoring file: "
  71.   read filename
  72. done
  73.  
  74. ln ~/.trash/"$linkname" "$file_dir"/"$filename"
  75. echo "File" $filename "restored"
  76.  
  77. rm ~/.trash/"$linkname"
  78. sed -i "${line_counter}d" ~/.trash.log
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement