[Solved] Delete files older than 10 years


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Delete files older than 10 years
# 1  
Old 07-08-2013
[Solved] Delete files older than 10 years

I need a command which delete files older than 10 years.

I got a command for 90 days and all commands I find are for days and nothing for years.

Code:
find file_name -mtime +90 -exec rm {} \;

# 2  
Old 07-08-2013
Hi eskay,

For an example to find 19 years old file:

Code:
y=19;t=`echo $y*365|bc`;find . -type f -mtime +"$t" -exec ls -l {} \;

-rwxrw-r-x   1 root       sys            695 May 10  1994 ./file1
-r-xr--r--   1 root       sys           2353 Oct 16  1993 ./file2



So your command would look like to delete 10 years old files:

Code:
find file_name -mtime +3650 -exec rm {} \;


Enjoy..
This User Gave Thanks to rveri For This Post:
# 3  
Old 07-08-2013
there will be a leap year in between so what then? so is there any command based on year and not on days
# 4  
Old 07-08-2013
Quote:
Originally Posted by rveri
Hi eskay,

For an example to find 19 years old file:

Code:
y=19;t=`echo $y*365|bc`;find . -type f -mtime +"$t" -exec ls -l {} \;

-rwxrw-r-x   1 root       sys            695 May 10  1994 ./file1
-r-xr--r--   1 root       sys           2353 Oct 16  1993 ./file2



So your command would look like to delete 10 years old files:

Code:
find file_name -mtime +3650 -exec rm {} \;


Enjoy..
Of course, if you can't multiply 10 * 365 in your head but you're using a standards conforming shell (such as bash or ksh), you could also use:
Code:
find file_name -mtime +$((10 * 365)) -exec rm {} \;

without needing to start us another process to run bc.

Since it is quite possible that this find will remove applications, libraries, and data files that are used every day but haven't changed in the last decade; be sure that you only run something like this after you have made a backup of every file that might be affected... Smilie (I would strongly suggest running this with "-exec rm {} \;" replaced by "-print" first as a sanity check on what will be removed!)

Are you really worried about 2 or 3 days when you're going back a decade?

Last edited by Don Cragun; 07-08-2013 at 03:27 PM.. Reason: Fix typo.
# 5  
Old 07-08-2013
I am not worried for 2 or 3 days. It was just a requirement and so I asked. I have already done 3650 but thought to ask if there is anything similar considering years. I use an ETL tool and coding in that would take lot of time and we can put one line unix commands easily so gave it a try. Who knows how things will be after 10 years. Smilie I asked just for my knowledge

Thank you Smilie
# 6  
Old 07-08-2013
With a POSIX conforming shell (such as bash or ksh), you could try:
Code:
touch -t $(date +"$(($(date +%Y) - 10))%m%d%H%M") __limit
find . -type f ! -newer __limit -exec ls -l {} +

to look for files 10 or more years old to the current minute. (Note that if you run it on February 29 in a leap year, the date given 10 years earlier will be on March 1.) If this does what you want, you can change the "ls -l" to "rm".

This will even work on a Solaris system where the date utility doesn't have a -d option.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to delete all the files older than a date?

Hi, I need a command for deleting all the compress files *.Z that are older than the current date - 5 days. Basically I have a directory where daily I meet some back up files and I want to remove automatically the ones 5 days (or more) older than the current date. How can I write a 'rm' command... (1 Reply)
Discussion started by: Francy
1 Replies

2. Shell Programming and Scripting

Delete files older than X days.

Hi All, I am using below code to delete files older than 2 days. In case if there are no files, I should log an error saying no files to delete. Please let me know, How I can achive this. find /path/*.xml -mtime +2 Thanks and Regards Nagaraja. (3 Replies)
Discussion started by: Nagaraja Akkiva
3 Replies

3. Shell Programming and Scripting

To delete files older than 24 hrs

I have to retain only 1 day files in my system an I have to delete all the other files which are older than 24 hrs. Please let me know the option I have to give in the find -mtime command. (3 Replies)
Discussion started by: rajesh8s
3 Replies

4. UNIX for Dummies Questions & Answers

Unix Command to separate this years files and last years?

Hello - I have a folder that contains files from 2003 till 2010. I am trying to figure out a command that would seperate each years file and show me a count? Even if i can find a command that would give me year by year count, thats good enough too. Thanks (8 Replies)
Discussion started by: DallasT
8 Replies

5. Shell Programming and Scripting

Delete files older than today

is it -mtime +1 as i need all files older than today to be deleted (6 Replies)
Discussion started by: dinjo_jo
6 Replies

6. Shell Programming and Scripting

delete files more than 15 days older

i have to delete files which are older than 15 days or more except the ones in the directory Current and also *.sh files i have found the command for files 15 days or more older find . -type f -mtime +15 -exec ls -ltr {} \; but how to implement the logic to avoid directory Current and also... (3 Replies)
Discussion started by: ali560045
3 Replies

7. UNIX for Dummies Questions & Answers

Delete files older than 30 days

This is driving me crazy. How can I delete files in a specifc directory that are over 30 days old? Thanks in advance. (3 Replies)
Discussion started by: tlphillips
3 Replies

8. Shell Programming and Scripting

how to delete the files which are the 30 min older...?

Hi all, i have a simple question that i want to find out the 30 minutes older files and delete those files from the particular location(Folder) Generally for this purpose used to retreive the files with "atime" command For example: find and delete the 2 days older log files use this below... (2 Replies)
Discussion started by: psiva_arul
2 Replies

9. UNIX for Dummies Questions & Answers

delete files older than 7 days

can anyone tell me how I would write a script in ksh on AIX that will delete files in a directory older than 7 days? (1 Reply)
Discussion started by: lesstjm
1 Replies
Login or Register to Ask a Question