LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-10-2010, 10:05 AM   #1
rbalaa
Member
 
Registered: Mar 2010
Posts: 61

Rep: Reputation: 0
"Remove file if older than ..." Help with a script


Hello all,

I need an expert to help me with a script to check in "FolderA" if there are files older than "x number of days", if found to remove them.

Thanks in advance for any help.
 
Old 08-10-2010, 10:13 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Code:
find /some/folder -mtime +28 -exec rm -f {}\;
that'll delete anything older than 28 days under that directory
 
Old 08-10-2010, 11:26 PM   #3
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 676

Rep: Reputation: 137Reputation: 137
Quote:
Originally Posted by acid_kewpie View Post
Code:
find /some/folder -mtime +28 -exec rm -f {}\;
that'll delete anything older than 28 days under that directory
My version of find 4.4.2 (fedora 12) required space between {} and \;, i.e

Code:
find /some/folder -mtime +28 -exec rm -f {} \;
 
Old 08-11-2010, 12:37 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Quote:
Originally Posted by Valery Reznic View Post
My version of find 4.4.2 (fedora 12) required space between {} and \;, i.e

Code:
find /some/folder -mtime +28 -exec rm -f {} \;
yep, so does every version I've ever used... my bad.
 
Old 08-11-2010, 08:04 AM   #5
rbalaa
Member
 
Registered: Mar 2010
Posts: 61

Original Poster
Rep: Reputation: 0
Thanks for the replies. I havn't hada chance to test it, but I will soon.
 
Old 09-13-2010, 04:39 AM   #6
freeman118
LQ Newbie
 
Registered: Aug 2010
Posts: 2

Rep: Reputation: 0
Remove files out of 14 days

Hello All;

I used a code "find /some/folder -mtime +14 -exec rm -f {}\;" This code can been remove files out of 14 days. howerver, the output of file is different day.
You can see the file "20100830.txt" on 30, Aug
on other file "www20100830.tar.gz" on 31, Aug.

Please, see the further files as below:

I can not remove these two files on the same day.

Please, anyone can provide advise and deeply appreciated.

Thanks

[root@cron01]# ls -la
drwxr-xr-x 3 root root 3072 Sep 13 10:23 .
drwxr-xr-x 3 root root 80 Jun 22 11:31 ..
drwxr-xr-x 2 root root 2048 Sep 13 10:23 httpd
-rw-r--r-- 1 root root 10540589305 Aug 30 04:36 20100830.txt
-rw-r--r-- 1 root root 10674572196 Aug 31 04:37 20100831.txt
-rw-r--r-- 1 root root 10802965259 Sep 1 04:37 20100901.txt
-rw-r--r-- 1 root root 10924710265 Sep 2 04:38 20100902.txt
-rw-r--r-- 1 root root 11055977612 Sep 3 04:37 20100903.txt
-rw-r--r-- 1 root root 11184724742 Sep 4 04:38 20100904.txt
-rw-r--r-- 1 root root 11295158114 Sep 5 04:39 20100905.txt
-rw-r--r-- 1 root root 1860313830 Aug 31 04:41 www20100830.tar.gz
-rw-r--r-- 1 root root 1893674818 Sep 1 04:42 www20100831.tar.gz
-rw-r--r-- 1 root root 1888460332 Sep 2 04:42 www20100901.tar.gz
-rw-r--r-- 1 root root 1865787227 Sep 3 04:42 www20100902.tar.gz
-rw-r--r-- 1 root root 1995716158 Sep 4 04:43 www20100903.tar.gz
-rw-r--r-- 1 root root 1654560845 Sep 5 04:43 www20100904.tar.gz
-rw-r--r-- 1 root root 1853689856 Sep 6 04:37 www20100905.tar.gz
 
Old 09-13-2010, 04:43 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
don't actually understand what you want, but i am sure you can do something about the "www" files before you find and remove. maybe store them to some other directories?
 
Old 09-13-2010, 06:15 AM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
I think you want the daystart option?
Code:
       -daystart
	      Measure  times  (for  -amin,  -atime,  -cmin, -ctime, -mmin, and
	      -mtime) from the beginning of today rather than  from  24  hours
	      ago.   This  option only affects tests which appear later on the
	      command line.
??
 
Old 09-13-2010, 09:40 PM   #9
freeman118
LQ Newbie
 
Registered: Aug 2010
Posts: 2

Rep: Reputation: 0
Hi Acid and ghostdog74;

Thank you for your reply and kindly assistance.

Actullay I want to keep 14 days log files and remove log files out of 14 days.

The "*.txt" files and "*.gz" files are not create at the same day.

I have different to keep same files at same day.
(eg. Sep 5 04:39 20100905.txt
,,,Sep 6 04:37 www20100905.tar.gz )

Thank you
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help With A "Remove Shell User" Script carlosinfl Linux - Server 1 07-17-2009 02:44 PM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
how do i remove unwanted charaters at "script" command output? linuxzouk Linux - Newbie 4 05-15-2004 07:58 AM
remove the "Run or Display?" when I execute a script llee Programming 5 05-05-2003 11:21 AM
remove the "Run or Display?" when I execute a script llee Linux - Software 0 05-02-2003 02:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:01 PM.

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