Editing How to create a Recycle Bin (or Trash can) for the CLI rm command
Jump to navigation
Jump to search
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 2: | Line 2: | ||
==Why should I create a Recycle Bin?== | ==Why should I create a Recycle Bin?== | ||
The ext2 and ext3 file systems used by linux operating systems (and used by Synology NAS's) is well known for its unfriendliness in recovering accidentally deleted files. Unlike FAT and NTFS once a ext2 ext3 file is deleted all pointers and information relating to where the file is/was on the disk are irreversibly destroyed. This makes the easy recovering of accidentally deleted files impossible, even if you imediately want to recover it after | The ext2 and ext3 file systems used by linux operating systems (and used by Synology NAS's) is well known for its unfriendliness in recovering accidentally deleted files. Unlike FAT and NTFS once a ext2 ext3 file is deleted all pointers and information relating to where the file is/was on the disk are irreversibly destroyed. This makes the easy recovering of accidentally deleted files impossible, even if you imediately want to recover it after mistakenly deleting it, i.e. you haven't written over the file. | ||
Without a recycle bin often the only way to recover the contents of a file is to sweep the hard disk for byte streams and load then into a file and then use a hex editor or similar to try to find the contents of your file | Without a recycle bin often the only way to recover the contents of a file is to sweep the hard disk for byte streams and load then into a file and then use a hex editor or similar to try to find the contents of your file, but unless you want to spend hours/days trying to recover a file, you will need a recycle bin. | ||
==Overview of the Mod== | ==Overview of the Mod== | ||
#Create a directory to put any deleted files/directories in | |||
#Create a directory to | #Create a direcotry to store your sript | ||
#Create a | |||
#Create a script to be called instead of the rm command which '''moves''' files or directories into the recycle bin directory | #Create a script to be called instead of the rm command which '''moves''' files or directories into the recycle bin directory | ||
#Set an alias to call your script every time the rm command is used | #Set an alias to call your script every time the rm command is used | ||
#Set a cronjob to regularly remove/purge (actually delete) files/directories older that X days old from the | #Set a cronjob to regularly remove/purge (actually delete) files/directories older that X days old from the RecycleBin (in this example I use 7 days but you could configure what you want) | ||
#If you have the knowledge and need you can modify the script to limit diskspace | #If you have the knowledge and need you can modify the script to limit diskspace, or perhaps purge based on a diskspace limit, rather than age. If you do please add it to this wiki. | ||
==Limitations== | ==Limitations== | ||
#This will only move to the | #This will only move to the RecycleBin files/directories that were deleted using the rm command in the Command Line Interface. | ||
#It will not recycle files/directories deleted via a Samba Share (i.e. when using a ftp client such as windows file explorer) | #It will not recycle files/directories deleted via a Samba Share (i.e. when using a ftp client such as windows file explorer) | ||
#It will not recycle files/directories deleted by the Synology "File Station" | #It will not recycle files/directories deleted by the Synology "File Station" | ||
#If you delete a file/directory with the same name twice or more, before the first has been purged from the RecycleBin, the second and subsequent files will have a version number tagged onto the end of the file/directory name. | |||
#If you delete a file/directory with the same name twice or more, before the first has been purged from the | #If you wish to restore a deleted file/directory, you will need to manually move/copy it from the RecycleBin directory back to where you want it within the 7 days before the file/direcotry is purged from the RecycleBin. You can change the default setting of 7 days if required. | ||
#If you wish to restore a deleted file/directory, you will need to manually move/copy it from the | |||
==The Mod== | ==The Mod== | ||
#Using the Synology web gui create a folder "RecycleBin" on volume1. If you want you can select that this directory is not | #Using the Synology web gui create a folder "RecycleBin" on volume1. If you want you can select that this directory is not visible. | ||
#Using the Synology web gui create a folder "my_scripts" on volume1. If you want you can select that this directory is not | #Using the Synology web gui create a folder "my_scripts" on volume1. If you want you can select that this directory is not visible. | ||
#Enable the Command Line Interface | #Enable the Command Line Interface | ||
#Login to the CLI as root | #Login to the CLI as root | ||
# | #Then change directory to your script folder, e.g. "cd /volume1/my_scripts" | ||
#Use vi to create a script file rm_replacement.sh, e.g. "vi rm_replacement.sh" | #Use vi to create a script file rm_replacement.sh, e.g. "vi rm_replacement.sh" | ||
#Press the "i" key to enter vi's input mode and then copy the contents of the script rm_replacement.sh below and paste it into vi | #Press the "i" key to enter vi's input mode and then copy the contents of the script rm_replacement.sh below and paste it into vi | ||
#Save changes to the file and close vi, e.g. press [ESC] and then type "ZZ". Note: capital Z's | #Save changes to the file and close vi, e.g. press [ESC] and then type "ZZ". Note: capital Z's | ||
#Now give the script file execution privileges, "chmod 700 rm_replacement.sh" | #Now give the script file execution privileges, "chmod 700 rm_replacement.sh" | ||
#Now add the alias command to | #Now add the alias command to your .profile file, e.g "vi ~/.profile", and then add at the bottom of the file "alias rm='sh /volume1/my_scripts/rm_replacement.sh | ||
#Close the CLI session and | #Close the CLI session and reopen it again to get ash to read the modified .profile file | ||
#Your script will now run instead of rm, try "rm -h" to get the help. | #Your script will now run instead of rm, try "rm -h" to get the help. You could also create a directory and put some files in it and then delete them using rm to see how it work. Check the content of the RecycleBin with "dir /volume1/RecycleBin". | ||
#Now we create a cronjob to daily check the RecycleBin and delete anything over 7 days old from it (you can change this period if you want). Enter "vi /etc/crontab" | #Now we create a cronjob to daily check the RecycleBin and delete anything over 7 days old from it (you can change this period if you want). Enter "vi /etc/crontab" | ||
#Add the last line given in the section crontab below as the last line in your crontab file. Note it is TAB characters that seperate values in crontab, not spaces. | #Add the last line given in the section crontab below as the last line in your crontab file. Note it is TAB characters that seperate values in crontab, not spaces. | ||
Line 81: | Line 77: | ||
"-f") FORCE="y" ;; | "-f") FORCE="y" ;; | ||
"--help" | "-h") | "--help" | "-h") | ||
echo "Usage rm [options] file1 file2 ..." | echo "Usage rm [options] file1 file2 ..." | ||
echo | echo | ||
Line 127: | Line 122: | ||
===="/etc/crontab" entry example==== | ===="/etc/crontab" entry example==== | ||
Below is an example crontab file. It is the last line you want to copy and paste into your existing /etc/crontab file | Below is an example crontab file. It is the last line you want to copy and paste into your existing /etc/crontab file | ||
<pre> | <pre> | ||
#minute hour mday month wday who command | #minute hour mday month wday who command | ||
53 0 * * 5 root /usr/sbin/ntpdate -b time.nist.gov | |||
57 0 * * * root find /volume1/RecycleBin/ -mtime +7 -type f -exec rm "{}" ";" | |||
</pre> | </pre> | ||