Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.51 KB | None | 0 0
  1. #!bin/bash
  2.  
  3. # removing excessive slashes
  4. filepath=$(echo "$1" | tr -s '/')
  5. first_character=$(echo "$filepath"| cut -c1-1)
  6. # could have also added removing the last symbol if it is the slash but did not want to
  7. echo "Filepath: $filepath"
  8.  
  9. # checking if filepath exists
  10. if [ -f "$filepath" ]; then
  11.     echo "File $filepath exists"
  12. else
  13.     echo "File $filepath does not exist"
  14.     exit
  15. fi
  16.  
  17. # checking if directory home/trash exists
  18. directory=${HOME}/.trash # directory of .trash
  19. if [ -d $directory ]; then
  20.     echo "Directory $directory exists"
  21. else
  22.     mkdir $directory
  23.     echo "Directory $directory was created"
  24. fi
  25.  
  26. # getting id for a new link
  27. counter_file="${HOME}/.counter_file"
  28. if ! [[ -f $counter_file ]]; then
  29.     echo "The file $counter_file was created"
  30.     echo "0" > $counter_file
  31. fi
  32. let link_id=$(tail -1 $counter_file)+0
  33. let link_id+=1
  34. echo "$link_id" > $counter_file
  35.  
  36. # creating a hard link and deleting filepath
  37. link_path=$directory/$link_id
  38. ln "$filepath" "$link_path"
  39. echo "The link $link_path was successfully created"
  40. rm -f "$filepath"
  41. echo "The file $filepath was deleted"
  42.  
  43. # adding the information of deleted filepath into trash.log
  44. log_file=${HOME}/.trash.log
  45. if ! [ -f $log_file ]; then
  46.     touch $log_file
  47.     echo "File $log_file was created"
  48. fi
  49.  
  50. if [[ "$first_character" == "/" ]]; then
  51.     echo "$link_id" "$filepath" >> $log_file
  52. else
  53.     echo "$link_id" "${PWD}"/"$filepath" >> $log_file
  54. fi
  55. echo "The information about deleted file $filepath was added to $log_file"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement