Editing
How to create a Recycle Bin (or Trash can) for the CLI rm command
(section)
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.
Anti-spam check. Do
not
fill this in!
==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 "my_scripts" on volume1. If you want you can select that this directory is not a visible share. #Enable the Command Line Interface #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" #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 #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 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! #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". #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. #To change how long files/directories are left in the RecycleBin change the parameter -mtime from "+7" to meet your requirments. #Save the file and exit vi, e.g. [ESC]ZZ #You need to get cron to re-read the crontab file, to do this reboot the NAS, e.g. "reboot" #Finished ===Scripts=== ===="rm_replacement.sh" script==== Below is the /volume1/my_scripts/rm_replacement.sh script. <pre> #!/bin/bash # # This script is to be called by an alias of the rm command # e.g. using the entry... # alias rm='sh /volume1/my_scripts/rm_replacement.sh' # in your $HOME/.profile or $HOME/.bashrc file # # It will move rm files to /volume1/RecycleBin # You need to create the directory /volume1/RecycleBin if # it doesn't already exist # # If a file/directory with the same name already exists in the # RecycleBin, the file/directory already in the RecycleBin # will be renamed taging "_X" on to the end where # X is an incremental number and then the newly rm'd # file/directory will be copied into the RecycleBin # # Consequently the latest version of the file will be its normall # name, without any extension. RM_OPT="" FILES="" VERBOSE="n" FORCE="n" while [ "$1" ]; do case "$1" in "-v") VERBOSE="y" ;; "-f") FORCE="y" ;; "--help" | "-h") echo "You are running a replacement for rm called by an alias" echo "Usage rm [options] file1 file2 ..." echo echo "Moves the files to /volume1/RecycleBin. Files can be either files or" echo "directories." echo echo "Options:" echo " -v verbose mode" echo " -h, --help this help message" echo " --help-rm help message of /bin/rm" echo " -f force processing with the normal rm command (/bin/rm)" echo " All other options are ignored when moving files. When removing" echo " permanently, these options are passed to /bin/rm" echo exit 0 ;; "--help-rm") /bin/rm --help exit 0 ;; -*) RM_OPT=$RM_OPT" "$1 ;; *) if [ "$VERBOSE" = "y" ]; then echo $1 fi if [ "$FORCE" = "y" ]; then /bin/rm -f $RM_OPT $1 else if [ -e "$1" ]; then if [ -e "/volume1/RecycleBin/$(basename $1)" ]; then version=2 while [ -e "/volume1/RecycleBin/$(basename $1)_$version" ]; do let version=$version+1 done mv "/volume1/RecycleBin/$(basename $1)" "/volume1/RecycleBin/$(basename $1)_$version" fi mv "$1" "/volume1/RecycleBin" fi fi ;; esac shift done </pre> ===="/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 <pre> #minute hour mday month wday who command 0 0 * * 5 root /usr/sbin/ntpdate -b time.nist.gov 1 0 * * * root /opt/bin/find /volume1/RecycleBin/ -ctime +7 -type f -exec rm "{}" ";" </pre> [[Category:SynologyWiki]] [[Category:ToBeExported]]
Summary:
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)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
British English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information