Script that will delete files and folders older than 90 days

Don't miss a thing. Post your questions and discussion about other uncategorized NAS features here.
Post Reply
User avatar
mackconsult
Know my way around
Posts: 158
Joined: Tue Oct 13, 2009 10:52 am
Location: Vancouver, WA
Contact:

Script that will delete files and folders older than 90 days

Post by mackconsult »

I created the following script thinking it would look in the recycle bin and delete all files and folders that are older than 90 days.

#!/bin/sh
####################################
#
# simple script that will walk thru the
# recycle bins on NAS and delete everything
# older than 90 days
#
####################################

/usr/bin/find /share/HDA_DATA/"Network Recycle Bin"/*.* -mtime +90 -exec rm {} \;

when I run it ([/share/Data/templates] # ./cleanrecyclebin.sh) I get the following .....

BusyBox v1.01 (2012.12.04-18:29+0000) multi-call binary

Usage: find [PATH...] [EXPRESSION]

Search for files in a directory hierarchy. The default PATH is
the current directory; default EXPRESSION is '-print'

EXPRESSION may consist of:
-follow Dereference symbolic links.
-name PATTERN File name (leading directories removed) matches PATTERN.
-print Print (default and assumed).

-type X Filetype matches X (where X is one of: f,d,l,b,c,...)
-perm PERMS Permissions match any of (+NNN); all of (-NNN);
or exactly (NNN)
-mtime TIME Modified time is greater than (+N); less than (-N);
or exactly (N) days

[/share/Data/templates] #
QNAP TS259
QNAP TS421
QNAP TS649
mjhamilton
Know my way around
Posts: 185
Joined: Tue Oct 30, 2012 12:12 am

Re: Script that will delete files and folders older than 90

Post by mjhamilton »

There is already a setting for this on my nas in the gui
User avatar
pwilson
Guru
Posts: 22533
Joined: Fri Mar 06, 2009 11:20 am
Location: Victoria, BC, Canada (UTC-08:00)

Re: Script that will delete files and folders older than 90

Post by pwilson »

mackconsult wrote:I created the following script thinking it would look in the recycle bin and delete all files and folders that are older than 90 days.

Code: Select all

#!/bin/sh
####################################
#
# simple script that will walk thru the
# recycle bins on NAS and delete everything
# older than 90 days
#
####################################

/usr/bin/find /share/HDA_DATA/"Network Recycle Bin"/*.* -mtime +90 -exec rm {} \;
Busybox v1.01 does NOT support the "-exec" parameter in it's "find" command.

Solution:
  1. Install Optware IPKG (using the instructions in QNAPedia article: Install Optware IPKG)
  2. Install Optware version of "find":

    Code: Select all

    ipkg update
    ipkg install findutils
  3. Create your script:

    Code: Select all

    cat >/opt/bin/cleanrecyclebin <<EOF
    #!/bin/sh
    ####################################
    #
    # simple script that will walk thru the
    # recycle bins on NAS and delete everything
    # older than 90 days
    #
    # Dependancies:  This script requires Optware,  
    #                and "findutils" installed on NAS
    ####################################
    
    /opt/bin/find /share/HDA_DATA/"Network Recycle Bin"/*.* -mtime +90 -exec rm {} \;
    EOF
    chmod +x /opt/bin/cleanrecyclebin
    Click on "SELECT ALL" (above this code segment), and then cut&paste it into an SSH session on your NAS
  4. Run "/opt/bin/cleanrecyclebin" whenever you want to "purge" files over 90 days old in your recycling bin.

Patrick M. Wilson
Victoria, BC Canada
QNAP TS-470 Pro w/ 4 * Western Digital WD30EFRX WD Reds (RAID5) - - Single 8.1TB Storage Pool FW: QTS 4.2.0 Build 20151023 - Kali Linux v1.06 (64bit)
Forums: View My Profile - Search My Posts - View My Photo - View My Location - Top Community Posters
QNAP: Turbo NAS User Manual - QNAP Wiki - QNAP Tutorials - QNAP FAQs

Please review: When you're asking a question, please include the following.
DigitalOxygen.ca
Know my way around
Posts: 103
Joined: Wed Aug 13, 2008 1:39 am

Re: Script that will delete files and folders older than 90 days

Post by DigitalOxygen.ca »

I know this thread is very old but I recently needed a similar solution but had lots of trouble getting Optware or alike installed on an older QNAP NAS. I eventually found this command which did the trick without any special packages.

In my case I did not have a time requirement. Just wanted to remove all files that matched a certain filename pattern.

Code: Select all

find . -type f -name  <search criteria here> -print | while read LINE; do rm "$LINE" ; done
The key to getting a solution without Optware or alike was piping it to the while loop that took each line that the find command returned and executed the desired code on it. In this case the rm command.

Probably inefficient and may need tweaking, but it worked when nothing else would.
Post Reply

Return to “Miscellaneous”