LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 04-18-2021, 10:44 AM   #1
1s440
Member
 
Registered: Mar 2018
Posts: 266

Rep: Reputation: Disabled
delete files older than a year


Hi all,

We have monthly backups and yearly backups, and wanted to keep files for the 12 months and delete the older months. Is it similar to delete files older than x days for ex:

Code:
find /your/file -mtime +365 -exec rm {} +
 
Old 04-18-2021, 02:08 PM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
Yes, and you can run it on the directory that holds the files.
Exclude directories (at least the start directory)!
Code:
find /your/dir/ \! -type d -mtime +365 -exec rm {} +
Code:
find /your/dir/ -type f -mtime +365 -exec rm {} +
 
Old 04-18-2021, 09:12 PM   #3
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Quote:
Originally Posted by MadeInGermany View Post
Yes, and you can run it on the directory that holds the files.
Exclude directories (at least the start directory)!
Code:
find /your/dir/ \! -type d -mtime +365 -exec rm {} +
Code:
find /your/dir/ -type f -mtime +365 -exec rm {} +
This works too
Code:
-delete
Code:
find /your/dir/ -type f -mtime +365 -delete
I would definitely advise running it without the action to make sure it's affecting the correct files

Code:
find /your/dir/ -type f -mtime +365
Code:
       -delete
              Delete files; true if removal succeeded.  If the removal failed,
              an  error message is issued.  If -delete fails, find's exit sta‐
              tus will be nonzero (when it eventually exits).  Use of  -delete
              automatically turns on the `-depth' option.

              Warnings:  Don't  forget that the find command line is evaluated
              as an expression, so putting -delete first will make find try to
              delete everything below the starting points you specified.  When
              testing a find command line that you later intend  to  use  with
              -delete,  you should explicitly specify -depth in order to avoid
              later surprises.  Because -delete  implies  -depth,  you  cannot
              usefully use -prune and -delete together.

              Together  with the -ignore_readdir_race option, find will ignore
              errors of the -delete action in the case  the  file  has  disap‐
              peared  since  the parent directory was read: it will not output
              an error diagnostic, and the return code of the  -delete  action
              will be true.
 
Old 04-19-2021, 02:34 PM   #4
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,818

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by 1s440 View Post
Hi all,

We have monthly backups and yearly backups, and wanted to keep files for the 12 months and delete the older months. Is it similar to delete files older than x days for ex:

Code:
find /your/file -mtime +365 -exec rm {} +
logrotate(8) would help keep the older log files under control without you having to manually find/delete them. It's not limited to the files under /var/log and non-root users may institute their own logrotate function via cron. Recommendation: use the "-d/-v" (debug and verbose) switches when testing it to show you what's liable to be deleted without actually deleting the files. Remove the "-d" switch when you're satisfied it's configured to do what you require.

HTH...
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cron Question - Delete files in a dir older than 48 hours? t3___ Linux - Newbie 5 10-11-2011 03:42 AM
[SOLVED] Delete regular files (not hidden files) with find + rm in one line older than 15 Virtuose Linux - Newbie 1 01-08-2011 05:24 PM
Script help - delete files older than 45 days but exclude the system files jojothedogboy Linux - Software 3 06-13-2008 03:43 PM
delete files older than 30 days using cronjob latheesan Linux - Newbie 5 06-14-2005 02:40 PM
Script for compressing files older than one year Pengu010011 Linux - Newbie 2 10-26-2004 07:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 11:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration