This is a replica of the recycle bin feature in Linux for command line.
How it works?
Say you want to remove a file from command line (non-permanently), you would run del (filename). The script would then move the file to the recycle bin folder and write it's location to a file called 'link2file' so it can remember where it was stored prior to deletion so that in the event we want to restore this file or folder then we can.
If we wanted to restore a file from command line, we would run restore (filename).
If we wanted to trash files then we have two options. If we have -a as a parameter then it will remove all files from the recycle bin without prompting the user if they definitely want to remove the file, trash (filename) will ask the user for each individual file within the recycle bin if they wish to delete.
del
#!/bin/sh
# This file should be stored in /usr/bin
#Checks if folder dustbin exists
if [ ! -e /root/dustbin ]
#If is doesn't then
then
#Check for write permissions
if [ -w . ]
#Make the directory dustbin
then mkdir /root/dustbin
#If not then continue silently
else :
fi
#If dustbin does exist continue silently
else
:
fi
#If there is a parameter and that paraemter exists then continue other display message
if [ -z "$1" ];
#Display error message
then echo "USAGE: del filename"
#If there is a parameter then continue with the following
else
#If parameter one file or folder doesn't exist
if [ ! -e "$1" ];
#Display error message
then echo "File or folder does not exist."
#else
else
#Set a to the current working directory
a=$(pwd)
#Read user input y or n (yes or no)
read -p "Are you sure you would like to delete $1? Please enter 'Y' for yes or 'N' for no." yn
#Set unique to time since 1970
unique=$(date +%s)
#Open case statement
case $yn in
#Note that the program accepts any string that begins with a y or an n.
#If user enters y or Y then move file to recycle bin and store path in url2file
[Yy]* ) mkdir /root/dustbin/$unique; echo $unique "|" $a"/"$1 >>/root/link2file; mv $1 /root/dustbin/$unique; echo ""; echo "File deleted."; echo "";;
#Old method
#[Yy]* ) mv $1 /root/dustbin; echo "File deleted";;
#If user enters n or N then do not move file and exit
[Nn]* ) echo "File not deleted"; exit;;
#If user enters an invalid characters then display error message
* ) echo "Invalid input!";;
#Close case statement
esac
#End parameter check if statement
fi
fi
restore
#!/bin/sh
# This file should be stored in /usr/bin
#If second parameters is present and is equal to -n then
#If there is a parameter and that paraemter exists then continue other display message
if [ -z "$1" ];
#Display error message
then echo "USAGE: restore filename"
#TEST TWO FILES WITH SAME NAME
else
for line in `ls /root/dustbin`; do
#Get whatever is in parameter 1 search for it in link2file and store it in variable restore
unique=`grep $1 /root/link2file | cut -d "|" -f 1`
restore=`grep $1 /root/link2file | cut -d "|" -f 2`
echo $restore
#location=`dirname "$restore"`
#echo $location
#Change directory to /root/dustbin
cd /root/dustbin/$unique
#If parameter one file or folder doesn't exist
if [ ! -e "$1" ]
#Display error message
then echo "File or folder does not exist in recycle bin."
#else
else
#Move file to the location, prompt if file already exists
mv -i $1 $location | echo "File" $1 "restored"
sed --in-place '/$unique/d' /root/link2file
done
fi
fi
#!/bin/bash
# This file should be stored in /usr/bin
#If the -a parameter is specified
if [ "$1" == "-a" ]
#Then
then
#Change directory to /root/dustbin
cd /root/dustbin
#Remove (force) all files and folders from the directory
rm -rf *
#Change directory to root
cd /root
#Remove file that stored locations of files prior to deletion
rm link2file
#Re-create the file that stored locations of files prior to deletion
touch link2file
#If -a parameter isn't specific
else
#Change directory to /root/dustbin
cd /root/dustbin
#List contents of the folder
ls
#For every file in the dustbin folder
for line in `ls /root/dustbin`
do
read -p "Are you sure you want to remove $line permanently?" yn
#Open case statement
case $yn in
#If the user enters Y then remove the file listed
[Yy]* ) rm $line; echo "File" $line "removed successfully";;
#If the user enters N then do nothing
[Nn]* ) :;;
#If the user enters anything other than Y or N then display error message
* ) echo "Invalid input!";;
esac
done
fi
Again, I hope this helps people. The scripts should be put into the /usr/bin (bin stands for binaries) folder.