LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-09-2006, 05:27 AM   #1
rust8y
Member
 
Registered: May 2006
Posts: 100

Rep: Reputation: 15
Bash script to remove files older than 3 days


Can someone help me with a bash script to remove files older than 3 days in directory /u1/database/prod/arch?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 07-09-2006, 05:53 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

You could use a 'simple' one-liner for this:

find /u1/database/prod/arch -type f -mtime +3 -exec rm {} \;

Or as bash script:
Code:
#!/bin/bash

find /u1/database/prod/arch -type f -mtime +3 -exec rm {} \;
The only 2 commands used are find and rm.

Find looks for files (-type f), this to exclude directories, that are older then 3 days (-mtime +3). All it finds is given to rm (-exec rm {} \; ).

You could also place the rm statement outside of find, which is supposed to be faster:

find /u1/database/prod/arch -type f -mtime +3 | xargs rm

All the three examples do their searching recursively.

man find for details.

Hope this helps.
 
6 members found this post helpful.
Old 07-09-2006, 08:49 PM   #3
rust8y
Member
 
Registered: May 2006
Posts: 100

Original Poster
Rep: Reputation: 15
Thank you for your help.
 
Old 07-09-2006, 08:53 PM   #4
rust8y
Member
 
Registered: May 2006
Posts: 100

Original Poster
Rep: Reputation: 15
find /u1/database/prod/arch -type f -mtime +3 -exec rm {} \;

What does the ; at the end do?
 
Old 07-09-2006, 09:02 PM   #5
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
man is your friend. Try
Code:
man find
find /u1/database/prod/arch -type f -mtime +3 -exec rm {} \;

this command finds all the files under /u1/database/prod/arch and it's subfolders, that are "regular files" (-type f) not directories, device files or something like that, and that have been modified at least 3 days ago (-mtime +3) and then executes "rm <filename>" for those files.
 
1 members found this post helpful.
Old 09-04-2009, 09:19 AM   #6
tiekookeit
LQ Newbie
 
Registered: Jan 2009
Location: Brazil
Distribution: debian
Posts: 16

Rep: Reputation: 1
Wonderful

Thanks for your help!!!
 
Old 09-04-2009, 10:08 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by rust8y View Post
find /u1/database/prod/arch -type f -mtime +3 -exec rm {} \;

What does the ; at the end do?
The "\;" at the end tells find where the end of the -exec command is. It can't just be the end of the line because the find command syntax allows further tests and actions after the -exec and it can't be just ; because the shell would see it as the end of a shell command and remove it. The \ "escapes" it from being seen by the shell as the end of a shell command.
 
5 members found this post helpful.
Old 10-06-2009, 12:57 PM   #8
Photar
LQ Newbie
 
Registered: Oct 2009
Posts: 2

Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
The "\;" at the end tells find where the end of the -exec command is. It can't just be the end of the line because the find command syntax allows further tests and actions after the -exec and it can't be just ; because the shell would see it as the end of a shell command and remove it. The \ "escapes" it from being seen by the shell as the end of a shell command.
You can also do ';'
 
Old 11-09-2009, 08:09 AM   #9
trunikov
LQ Newbie
 
Registered: Jul 2005
Location: Kiev, Ukraine
Distribution: rh9
Posts: 6

Rep: Reputation: 2
Exclamation

I would like to say that you can use option -delete to remove files instead of tricks with rm and xargs.
Sample:

find /path/dir -name "*.bz2" -type f -Btime +30d -delete

Also keep in mind that file node actually has three times: created, last accessed, last modified.
 
2 members found this post helpful.
Old 12-02-2009, 07:43 AM   #10
ziggy25
Member
 
Registered: Aug 2005
Distribution: Debian 5.2
Posts: 56

Rep: Reputation: 15
Quote:
Originally Posted by catkin View Post
The "\;" at the end tells find where the end of the -exec command is. It can't just be the end of the line because the find command syntax allows further tests and actions after the -exec and it can't be just ; because the shell would see it as the end of a shell command and remove it. The \ "escapes" it from being seen by the shell as the end of a shell command.
Hi,

Can you explain what the {} \; characters are for.

thanks
 
Old 12-02-2009, 07:51 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

The \; part is already explained by catkin in the post you quoted.

The {} holds what is found by find and given to the executed command. -exec <command> {} is the general form.

Hope this helps.
 
1 members found this post helpful.
Old 12-02-2009, 07:52 AM   #12
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by ziggy25 View Post
Hi,

Can you explain what the {} \; characters are for.

thanks
The post you quoted already explained part of it
Code:
(\;)
"{}" is a placeholder---look at the man page for find--under the -exec command

Last edited by pixellany; 12-02-2009 at 09:09 PM.
 
Old 12-02-2009, 11:21 PM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
@trunikov

1. Unix does not a have file creation time
Quote:
-ctime n
File's status was last changed n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file status change times.
http://linux.die.net/man/1/find

2. If you've really got RH9 (Shrike), you should really update to a current distro. That one hasn't been updated in years and would be likely to be exploited.
http://en.wikipedia.org/wiki/Red_Hat_Linux
Try Fedora 12 or Centos 5.4
 
Old 01-07-2010, 03:48 PM   #14
will177
LQ Newbie
 
Registered: Nov 2009
Location: London
Distribution: Ubuntu
Posts: 11

Rep: Reputation: 4
Quote:
Originally Posted by trunikov View Post
I would like to say that you can use option -delete to remove files instead of tricks with rm and xargs.
Sample:

find /path/dir -name "*.bz2" -type f -Btime +30d -delete
I like this way, but -Btime does not exist on my version of find, and it's not +30d for me but just +30

So I ended up using:


find /path/dir -mtime +30 -delete


as I wanted to delete all files and all directories under /path/dir

Thanks!
 
2 members found this post helpful.
Old 10-21-2010, 06:01 PM   #15
dsoria
LQ Newbie
 
Registered: Oct 2010
Posts: 1

Rep: Reputation: 3
hello!

Hello

im new..

regars to all
 
3 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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculating age in days and month in a bash script jachba Programming 5 06-23-2006 01:37 PM
delete files older than 30 days using cronjob latheesan Linux - Newbie 5 06-14-2005 02:40 PM
delete files in server that is older than 30 days using cronjob latheesan *BSD 2 06-14-2005 12:37 PM
help with a script that deletes files more than X days old BrianK Linux - General 5 06-14-2004 09:05 PM
Tar files in a dir modified before 7 days in a shell script jayachristina Linux - Newbie 4 05-14-2004 02:49 AM

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

All times are GMT -5. The time now is 02:44 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