This forum is in archive mode. You will not be able to post new content.

Author Topic: 'Recycle bin' feature in Linux  (Read 1666 times)

0 Members and 2 Guests are viewing this topic.

Offline dsme

  • /dev/null
  • *
  • Posts: 19
  • Cookies: 3
    • View Profile
'Recycle bin' feature in Linux
« on: January 12, 2014, 11:26:16 PM »
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
Code: [Select]
#!/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
Code: [Select]
#!/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


Code: [Select]
#!/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.

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: 'Recycle bin' feature in Linux
« Reply #1 on: January 13, 2014, 12:27:16 AM »
Am I missing something? What linux are you running where 'del' is the command to delete a file?
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline dsme

  • /dev/null
  • *
  • Posts: 19
  • Cookies: 3
    • View Profile
Re: 'Recycle bin' feature in Linux
« Reply #2 on: January 13, 2014, 12:31:55 AM »
Am I missing something? What linux are you running where 'del' is the command to delete a file?


I don't think you understand, by naming the file del and placing it into the /bin folder I can execute the command 'del' to delete files.

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: 'Recycle bin' feature in Linux
« Reply #3 on: January 13, 2014, 01:28:37 AM »
Oh wow. Jumped the gun. Been sick with a high fever.... Must be going to my brain.
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: 'Recycle bin' feature in Linux
« Reply #4 on: January 13, 2014, 08:09:35 AM »
Cool idea +1
Especially the option to restore files again.
Now you need a cronjob do delete files regularly from the recycle bin that are more than x days in there.

Offline j0lly

  • NULL
  • Posts: 1
  • Cookies: 0
    • View Profile
Re: 'Recycle bin' feature in Linux
« Reply #5 on: January 13, 2014, 03:18:32 PM »
and thinnk how to properly manage big files (iso, GBs Tar and so on)  ;)
in order not to move 10GB from a different partition (big risk  :( )

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: 'Recycle bin' feature in Linux
« Reply #6 on: January 13, 2014, 06:35:28 PM »
and thinnk how to properly manage big files (iso, GBs Tar and so on)  ;)
in order not to move 10GB from a different partition (big risk  :( )
I imagine the file format is not an issue. For the size, I think the solution would just be to set a limit how big a file could be to be moved. If it's above that line, it gets deleted right away.

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: 'Recycle bin' feature in Linux
« Reply #7 on: January 13, 2014, 07:15:23 PM »
Great project.

I am happy not to having one, just delete :)
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.