Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!bin/bash
- # removing excessive slashes
- filepath=$(echo "$1" | tr -s '/')
- first_character=$(echo "$filepath"| cut -c1-1)
- # could have also added removing the last symbol if it is the slash but did not want to
- echo "Filepath: $filepath"
- # checking if filepath exists
- if [ -f "$filepath" ]; then
- echo "File $filepath exists"
- else
- echo "File $filepath does not exist"
- exit
- fi
- # checking if directory home/trash exists
- directory=${HOME}/.trash # directory of .trash
- if [ -d $directory ]; then
- echo "Directory $directory exists"
- else
- mkdir $directory
- echo "Directory $directory was created"
- fi
- # getting id for a new link
- counter_file="${HOME}/.counter_file"
- if ! [[ -f $counter_file ]]; then
- echo "The file $counter_file was created"
- echo "0" > $counter_file
- fi
- let link_id=$(tail -1 $counter_file)+0
- let link_id+=1
- echo "$link_id" > $counter_file
- # creating a hard link and deleting filepath
- link_path=$directory/$link_id
- ln "$filepath" "$link_path"
- echo "The link $link_path was successfully created"
- rm -f "$filepath"
- echo "The file $filepath was deleted"
- # adding the information of deleted filepath into trash.log
- log_file=${HOME}/.trash.log
- if ! [ -f $log_file ]; then
- touch $log_file
- echo "File $log_file was created"
- fi
- if [[ "$first_character" == "/" ]]; then
- echo "$link_id" "$filepath" >> $log_file
- else
- echo "$link_id" "${PWD}"/"$filepath" >> $log_file
- fi
- echo "The information about deleted file $filepath was added to $log_file"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement