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

From wiki
Jump to navigation Jump to search

This wiki page gives step by step instructions on how to create a recycle bin (or Trash Can) for the rm (delete) command.

Why should I create a Recycle Bin?[edit]

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 mistakingly 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. Unless you want to spend hours/days trying to recover a file, you will need a recycle bin.

Overview of the Mod[edit]

  1. install the ipkg package findutils
  2. Create a directory to hold any deleted files/directories
  3. Create a directory to store your script which will effectively replace the rm command
  4. Create a script to be called instead of the rm command which moves files or directories into the recycle bin directory
  5. Set an alias to call your script every time the rm command is used
  6. 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)
  7. 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.


  1. This will only move to the recycle bin files/directories that were deleted using the rm command in the Command Line Interface.
  2. It will not recycle files/directories deleted via a Samba Share (i.e. when using a ftp client such as windows file explorer)
  3. It will not recycle files/directories deleted by the Synology "File Station"
  4. 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.
  5. 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.
  6. 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.
  7. 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[edit]

  1. 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.
  2. 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.
  3. Enable the Command Line Interface
  4. Login to the CLI as root
  5. Install ipkg, see
  6. install findutils, i.e. enter "ipkg install findutils"
  7. Change directory to your script folder, e.g. "cd /volume1/my_scripts"
  8. Use vi to create a script file, e.g. "vi"
  9. Press the "i" key to enter vi's input mode and then copy the contents of the script below and paste it into vi
  10. Save changes to the file and close vi, e.g. press [ESC] and then type "ZZ". Note: capital Z's
  11. Now give the script file execution privileges, "chmod 700"
  12. 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/'". Note the single quote at the end of this line!
  13. Close the CLI session and re-open it again to get ash to read the modified .profile file
  14. 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".
  15. 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"
  16. 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.
  17. To change how long files/directories are left in the RecycleBin change the parameter -mtime from "+7" to meet your requirments.
  18. Save the file and exit vi, e.g. [ESC]ZZ
  19. You need to get cron to re-read the crontab file, to do this reboot the NAS, e.g. "reboot"
  20. Finished


"" script[edit]

Below is the /volume1/my_scripts/ script.

#  This script is to be called by an alias of the rm command
#  e.g. using the entry...
#  alias rm='sh /volume1/my_scripts/'
#  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.


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 "Moves the files to /volume1/RecycleBin. Files can be either files or"
            echo "directories."
            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"
            exit 0
            /bin/rm --help
            exit 0
        -*) RM_OPT=$RM_OPT" "$1 ;;
            if [ "$VERBOSE" = "y" ]; then
                echo $1
            if [ "$FORCE" = "y" ]; then
                /bin/rm -f $RM_OPT $1
                if [ -e "$1" ]; then
                    if [ -e "/volume1/RecycleBin/$(basename $1)" ]; then
                        while [ -e "/volume1/RecycleBin/$(basename $1)_$version" ]; do
                        let version=$version+1
                        mv "/volume1/RecycleBin/$(basename $1)" "/volume1/RecycleBin/$(basename $1)_$version"
                    mv "$1" "/volume1/RecycleBin"

"/etc/crontab" entry example[edit]

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

#minute hour    mday    month   wday    who     command
0      0       *       *       5       root    /usr/sbin/ntpdate -b
1      0       *       *       *       root    /opt/bin/find /volume1/RecycleBin/ -ctime +7 -type f -exec rm "{}" ";"