Created
June 21, 2021 16:52
-
-
Save laytan/25c6a357fee3f596ad366ba9168d645a to your computer and use it in GitHub Desktop.
Safe rm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Laytan Laats - 1012512 - INFD 20-1 | |
# Check if file is being sourced, which will need to add the alias | |
if [[ "${BASH_SOURCE[0]}" != "${0}" ]] | |
then | |
echo "Adding safe delete alias." | |
alias rm="$(pwd)/1012512.sh" | |
return | |
fi | |
# Need at least one argument | |
if [[ $# -lt 1 ]] | |
then | |
echo "Please enter the file to be deleted." | |
exit | |
fi | |
# Parse arguments: https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash | |
POSITIONAL=() | |
while [[ $# -gt 0 ]] | |
do | |
key="$1" | |
case $key in | |
-u) | |
UNDELETE=YES | |
shift | |
;; | |
-r) | |
RECURSIVE=YES | |
shift | |
;; | |
-p) | |
PASSWORD="$2" | |
shift | |
shift | |
;; | |
*) # unknown option | |
POSITIONAL+=("$1") # save it in an array for later | |
shift # pass argument | |
;; | |
esac | |
done | |
set -- "${POSITIONAL[@]}" # restore positional parameters | |
TARGET="$1" | |
REGISTER="$HOME/trash/register" | |
ZIPPED="$HOME/trash/zipped" | |
# check if ~/trash does not exist | |
if [ ! -d $HOME/trash ] | |
then | |
# create it | |
mkdir $HOME/trash | |
# create zipped folder | |
mkdir $HOME/trash/zipped | |
# create trash register file | |
touch $HOME/trash/register | |
fi | |
# if -u | |
if [ "$UNDELETE" == "YES" ] | |
then | |
# find the target in the register | |
lineNumber=0 | |
while IFS= read -r line | |
do | |
lineNumber=$((lineNumber+1)) | |
seperated=($line) # turn string into array seperated by spaces | |
if [ "${seperated[0]}" == "$TARGET" ] | |
then | |
found=YES | |
hash="${seperated[1]}" | |
break | |
fi | |
done < "$REGISTER" | |
if [ "$found" != "YES" ] | |
then | |
echo "File not found in trash" | |
exit | |
fi | |
# if password protected | |
if [ $hash ] | |
then | |
# ask for password 3 times | |
for i in {1..3} | |
do | |
echo "Please enter the password for this file/directory:" | |
read password | |
# verify password hash | |
hashedInput=$(openssl passwd -salt "$TARGET" "$password") | |
if [ "$hash" != "$hashedInput" ] | |
then | |
echo "Try again" | |
else | |
echo "Password correct" | |
correct="YES" | |
break | |
fi | |
done | |
# Make sure password has been correctly filled in | |
if [ "$correct" != "YES" ] | |
then | |
echo "Password incorrect" | |
exit | |
fi | |
fi | |
# unzip the file | |
unzip "$ZIPPED/$TARGET.zip" | |
# remove zip file | |
rm "$ZIPPED/$TARGET.zip" | |
# remove lines starting with target from ~/trash/register | |
sed -i "/^${TARGET}/d" "$REGISTER" | |
# done | |
echo "File undeleted" | |
else | |
# confirm delete | |
echo "Type \"yes\" to delete $TARGET" | |
read confirm | |
if [ "$confirm" != "yes" ] | |
then | |
echo "Not deleting" | |
exit | |
fi | |
# check argument is a directory | |
if [ -d $TARGET ] | |
then | |
echo "Target is a directory" | |
# check -r argument passed, error if not | |
if [ "$RECURSIVE" != "YES" ] | |
then | |
echo "Not deleting a directory without -r passed" | |
exit | |
fi | |
fi | |
# check -p is given | |
if [ $PASSWORD ] | |
then | |
# hash password | |
hashedPassword=$(openssl passwd -salt "$TARGET" "$PASSWORD") | |
fi | |
# enter line into ~/trash/register | |
echo "$TARGET $hashedPassword" >> "$REGISTER" | |
# zip file/directory | |
zip -r "${TARGET}.zip" "$TARGET" | |
# move to ~/trash/zipped | |
mv "${TARGET}.zip" "$ZIPPED/${TARGET}.zip" | |
# remove original file/directory | |
rm -rf "$TARGET" | |
echo "File/Directory deleted" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment