Deleting files and directory's older than 3 months


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting files and directory's older than 3 months
# 1  
Old 12-17-2012
Wrench Deleting files and directory's older than 3 months

I have a qnap TS259 that is running ubuntu. Have successfully setup back scripts that are initiated by cron.

I would like to create a couple scrypts that would operate on the recycle bins for both drives. Just want to be able to run the script manually that would walk through both directories and delete everything that is older than 3 months or 90 days. Any help or indications on were to look would be great.

Regards,
# 2  
Old 12-17-2012
Read the man page of find
Code:
$man find

Especially the part about -printf

and this option
Code:
%Ak    File's last access time in the  format  specified  by  k,
                     which  is  either `@' or a directive for the C `strftime'
                     function.  The possible values for k  are  listed  below;
                     some  of  them might not be available on all systems, due
                     to differences in `strftime' between systems.

If you continue reading from this part, can find the K param
# 3  
Old 12-17-2012
Well I tried the following:

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

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

When I try and execute it though I get the following in putty:

Code:
[/share/Data/templates] # ./cleanrecyclebin.sh
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] # ./cleanrecyclebin.sh
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] #
[/share/Data/templates] # [/share/Data/templates] # ./cleanrecyclebin.sh
-sh: [/share/Data/templates]: No such file or directory
y/share/Data/templates] # BusyBox v1.01 (2012.12.04-18:29+0000) multi-call binar
-sh: syntax error near unexpected token `('
[/share/Data/templates] #

[/share/Data/templates] # Usage: find [PATH...] [EXPRESSION]
-sh: Usage:: command not found
[/share/Data/templates] #
t PATH isata/templates] # Search for files in a directory hierarchy.  The defaul
-sh: Search: command not found
[/share/Data/templates] # the current directory; default EXPRESSION is '-print'
-sh: the: command not found
-sh: default: command not found
[/share/Data/templates] #
[/share/Data/templates] # EXPRESSION may consist of:
-sh: EXPRESSION: command not found
[/share/Data/templates] #         -follow         Dereference symbolic links.
-sh: -follow: command not found
 removed) matches PATTERN.        -name PATTERN   File name (leading directories
-sh: syntax error near unexpected token `('
[/share/Data/templates] #         -print          Print (default and assumed).
-sh: syntax error near unexpected token `('
[/share/Data/templates] #
 one of: f,d,l,b,c,...) #         -type X         Filetype matches X (where X is
-sh: syntax error near unexpected token `('
); all of (-NNN);lates] #         -perm PERMS     Permissions match any of (+NNN
-sh: syntax error near unexpected token `('
[/share/Data/templates] #                         or exactly (NNN)
-sh: syntax error near unexpected token `('
(+N); less than (-N);s] #         -mtime TIME     Modified time is greater than
-sh: syntax error near unexpected token `('
[/share/Data/templates] #                         or exactly (N) days
-sh: syntax error near unexpected token `('
[/share/Data/templates] #
[/share/Data/templates] #


Last edited by Scott; 12-17-2012 at 04:10 PM.. Reason: Please use code tags
# 4  
Old 12-17-2012
The problem is because you have directory name with blank spaces. Wrap the path in single quotes and re-try:-
Code:
find '/share/HDA_DATA/Network Recycle Bin/'*.* -mtime +90 -exec rm {} \;

# 5  
Old 12-17-2012
Hmmmm still not working ....

Code:
[/share/Data/templates] # ./cleanrecyclebin.sh
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] #


Last edited by Scott; 12-17-2012 at 04:59 PM.. Reason: Code tags
# 6  
Old 12-17-2012
Put this line before the find command in your script and try:-
Code:
export PATH=/opt/bin:/opt/sbin:$PATH

# 7  
Old 12-17-2012
Still not seams to be working ......
Code:
[/share/Data/templates] # ./cleanrecyclebin.sh
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



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] #

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help deleting files one week older

Hi, I need to delete *.bad files which are 1 week old. How can I achieve that. I tried doing through below script but it deletes all the files. find ./ -mtime +7 -exec rm *.bad {} \; The below one works but i want to delete only files with .bad extension find . -mtime +7 | xargs rm (2 Replies)
Discussion started by: Gangadhar Reddy
2 Replies

2. Shell Programming and Scripting

Command to Count the files which is Older than 3 months?

Hi Gurus the count of files in a particular Directory... ls -lrth | grep -c ^- can any one share the command to Count the files which is Older than 3 months So please help me out in this Thanks in Advance (2 Replies)
Discussion started by: SeenuGuddu
2 Replies

3. Shell Programming and Scripting

need a code for moving the log files older than 2 months

Hi Viewer, I need logic code for moving the logs files from one directory to another directory. source :/xxxxxx/ xxxxxx / xxxxxx / xxxxxx / log --- under log directory we have so many files from last two years Here I need to check the files older than two months and I need to move in... (5 Replies)
Discussion started by: munna_su
5 Replies

4. Shell Programming and Scripting

Deleting files older than 6 hours

Hi All, I am using the below script to find all the files in a folder which are older than 6 hours and delete all those files, but some how I am not getting the required output. find $HOME/Log -type f -name "*.log" -amin +360 -exec rm *.* {} \ can any one please check and let me know... (13 Replies)
Discussion started by: subhasri_2020
13 Replies

5. Shell Programming and Scripting

gzip files older than 1 months

Hello, I want to gzip files in a folder in bulk i.e. all files older than 1 month should be in *.gz format. Could you please suggest how this can be achieved. Thanks. Regards, Alok (1 Reply)
Discussion started by: alok1301
1 Replies

6. UNIX for Advanced & Expert Users

Deleting older files of a particular type

hi This should be easy but i'm obviously missing something obvious. :) I'm looking to delete files from yesterday and older of extension .txt and there a range of subfolders with these files in them. The command runs but doesn't delete anything. SUSE 10. find /testfolder -maxdepth 2 -type f... (6 Replies)
Discussion started by: cmap
6 Replies

7. Shell Programming and Scripting

Delete files older than 3 months.(read date from the name of the file)

Guys, My log files stored in the date format format below(log_20080714072942): TIMESTAMP=`date +%Y%m%d%H%M%S` LOG=/log/log_${TIMESTAMP}.log I'm looking for a shell script which deletes all files which is older than 3 months from today. Regards, Bhagat (3 Replies)
Discussion started by: bhagat.singh-j
3 Replies

8. UNIX for Dummies Questions & Answers

deleting files with dates 3 months ago

please help me with this????? :confused: :confused: i need to create a program that will run in unix that will delete all files in a given directory that is at least 3 months old. first the program will need to automatically know what date it is right now to determine the files it will... (3 Replies)
Discussion started by: godalle
3 Replies

9. Shell Programming and Scripting

Deleting files older than a given date

Hi all, I want to delete all files in a directory which are older than a given date. I thought of doing it by creating a file by the required date by using touch command. And then i would use find command on that file and try to find files older than that. I searched the man and found a... (3 Replies)
Discussion started by: rajugp1
3 Replies

10. Shell Programming and Scripting

Listing files older than 2 months

A script or command to list files older than 2 months in a specified directory and remove it. (3 Replies)
Discussion started by: pbekal
3 Replies
Login or Register to Ask a Question