Advertisement
ZhenyaDudko

Untitled

Nov 26th, 2022
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.04 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 true
  21. do
  22.   if [[ "$filename" == *\0* || "$filename" == *\/* ]]
  23.   then
  24.     echo "Wrong filename"
  25.     echo "Name of file to restore: "
  26.     read filename
  27.     continue
  28.   fi
  29.  
  30.   linkname=""
  31.   file_untrash=""
  32.  
  33.   exec 3<&0
  34.  
  35.   line_counter=0
  36.   while read line
  37.   do
  38.     line_counter=$((line_counter+1))
  39.     current_linkname=$(echo $line | awk '{print $NF}')
  40.     current_filename=${line% $current_linkname}
  41.    
  42.     if [[ $current_filename == */$filename ]]
  43.     then
  44.       echo "Do you want to restore this file: " $current_filename "? Print Y/N or another to exit"
  45.       read answer <&3
  46.       case $answer in
  47.       "Y")
  48.         file_untrash=$current_filename
  49.         linkname=$current_linkname
  50.         break
  51.         ;;
  52.       "N") continue ;;
  53.       *) exit ;;
  54.       esac
  55.     fi
  56.   done < ~/.trash.log
  57.  
  58.   if [[ $file_untrash == "" || ! -f ~/.trash/$linkname ]]
  59.   then
  60.     echo "No files to restore found"
  61.     echo "Name of file to restore: "
  62.     read filename
  63.     continue
  64.   fi
  65.  
  66.   file_dir=${file_untrash%$filename}
  67.   if [[ -d "$file_dir" ]]
  68.   then
  69.     if [[ -f "$file_untrash" ]]
  70.     then
  71.       echo "File with such a name already exists"
  72.       echo "Name of file to restore: "
  73.       read filename
  74.       continue
  75.     fi
  76.     echo "Restored file in original directory"
  77.     ln ~/.trash/"$linkname" "$file_untrash"
  78.   else
  79.     echo "Original directory does not exist. Restoring file in home directory"
  80.     if [[ -f ~/"$filename" ]]
  81.     then
  82.       echo "File with such a name already exists"
  83.       echo "Name of file to restore: "
  84.       read filename
  85.       continue
  86.     fi
  87.     echo "Restored file in home directory"
  88.     ln ~/.trash/"$linkname" ~/"$filename"
  89.   fi
  90.  
  91.   rm ~/.trash/"$linkname"
  92.   sed -i "${line_counter}d" ~/.trash.log
  93.   break
  94. done  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement