Editing How to create a Recycle Bin (or Trash can) for the CLI rm command

Jump to navigation Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

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 7: Line 7:


==Overview of the Mod==
==Overview of the Mod==
#install the ipkg package findutils
#Create a directory to hold any deleted files/directories
#Create a directory to hold any deleted files/directories
#Create a directory to store your script which will effectively replace the rm command
#Create a directory to store your sript which will effectively replace the rm command
#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 recycle bin (in this example I use 7 days but you could configure what you want)
#Set a cronjob to regularly remove/purge (actually delete) files/directories older that X days old from the recycle bin (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 used by the recycle bin, or perhaps purge it based on a diskspace limit, rather than age. If you do add this functionality please add it to this wiki.
#If you have the knowledge and need you can modify the script to limit diskspace used by the recycle bin, or perhaps purge it based on a diskspace limit, rather than age. If you do please add this functionality please add it to this wiki.


==Limitations==
==Limitations==
Line 19: Line 18:
#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"
#It will not recycle files/directories deleted using the rm command whilst inside some subshell programs such as screen or mc unless you have configured them to load alias definitions on starup.
#If you delete a file/directory with the same name twice or more, before the first has been purged from the recycle bin, the previous versions of the file 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 recycle bin, the previous versions of the file will have a version number tagged onto the end of the file/directory name.
#If you wish to restore a deleted file/directory, you will need to manually move/copy it from the recycle bin directory back to where you want it. This you will have to do within the 7 days before the file/directory is purged from the recycle bin. 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 recycle bin directory back to where you want it. This you will have to do within the 7 days before the file/directory is purged from the recycle bin. You can change the default setting of 7 days if required.
#Aliases are not followed in scripts. If you want to use your recycle bin script in another script you will have to call it directly.
 
==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 a visible share.
#Using the Synology web gui create a folder "RecycleBin" on volume1. If you want you can select that this directory is not a visible share.
Line 28: Line 26:
#Enable the Command Line Interface
#Enable the Command Line Interface
#Login to the CLI as root
#Login to the CLI as root
#Install ipkg, see http://www.synology.com/wiki/index.php/Overview_on_modifying_the_Synology_Server%2C_bootstrap%2C_ipkg_etc#How_to_install_ipkg
#install findutils, i.e. enter "ipkg install findutils"
#Change directory to your script folder, e.g. "cd /volume1/my_scripts"
#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"
Line 35: Line 31:
#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 the /etc/profile file, e.g "vi /etc/profile", and then add at the bottom of the file "alias rm='sh /volume1/my_scripts/rm_replacement.sh'". Note the single quote at the end of this line!
#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 re-open it again to get ash to read the modified .profile file
#Close the CLI session and re-open 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. If you want to check its operation by creating a directory and some files and delete them using rm. Check the content of the RecycleBin with "dir /volume1/RecycleBin".
#Your script will now run instead of rm, try "rm -h" to get the help. If you want to check its operation by creating a directory and some files and delete them using rm. Check the content of the RecycleBin with "dir /volume1/RecycleBin".
Line 81: Line 77:
         "-f") FORCE="y" ;;
         "-f") FORCE="y" ;;
         "--help" | "-h")
         "--help" | "-h")
            echo "You are running a replacement for rm called by an alias"
             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. The example below clears the RecycleBin directory of files older than 7 days at 1minute past midnight everyday. Change these setting as needed
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
0     0      *      *      5      root    /usr/sbin/ntpdate -b time.nist.gov
53     0      *      *      5      root    /usr/sbin/ntpdate -b time.nist.gov
1     0      *      *      *      root    /opt/bin/find /volume1/RecycleBin/ -ctime +7 -type f -exec rm "{}" ";"
57     0      *      *      *      root    find /volume1/RecycleBin/ -mtime +7 -type f -exec rm "{}" ";"
</pre>
</pre>
[[Category:SynologyWiki]]
[[Category:ToBeExported]]
Please note that all contributions to wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see Wiki:Copyrights for details). Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)