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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
07-09-2006, 05:27 AM
|
#1
|
|
Member
Registered: May 2006
Posts: 100
Rep:
|
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.
|
07-09-2006, 05:53 AM
|
#2
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,868
|
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.
|
|
|
4 members found this post helpful.
|
07-09-2006, 08:49 PM
|
#3
|
|
Member
Registered: May 2006
Posts: 100
Original Poster
Rep:
|
Thank you for your help.
|
|
|
|
07-09-2006, 08:53 PM
|
#4
|
|
Member
Registered: May 2006
Posts: 100
Original Poster
Rep:
|
find /u1/database/prod/arch -type f -mtime +3 -exec rm {} \;
What does the ; at the end do?
|
|
|
|
07-09-2006, 09:02 PM
|
#5
|
|
Senior Member
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783
Rep:
|
man is your friend. Try 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.
|
09-04-2009, 09:19 AM
|
#6
|
|
LQ Newbie
Registered: Jan 2009
Location: Brazil
Distribution: debian
Posts: 16
Rep:
|
Wonderful
Thanks for your help!!!
|
|
|
|
09-04-2009, 10:08 AM
|
#7
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,367
|
Quote:
Originally Posted by rust8y
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.
|
|
|
4 members found this post helpful.
|
10-06-2009, 12:57 PM
|
#8
|
|
LQ Newbie
Registered: Oct 2009
Posts: 2
Rep:
|
Quote:
Originally Posted by catkin
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 ';'
|
|
|
|
11-09-2009, 08:09 AM
|
#9
|
|
LQ Newbie
Registered: Jul 2005
Location: Kiev, Ukraine
Distribution: rh9
Posts: 3
Rep:
|
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.
|
12-02-2009, 07:43 AM
|
#10
|
|
Member
Registered: Aug 2005
Distribution: Debian 5.2
Posts: 56
Rep:
|
Quote:
Originally Posted by catkin
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
|
|
|
|
12-02-2009, 07:51 AM
|
#11
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,868
|
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.
|
12-02-2009, 07:52 AM
|
#12
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
Quote:
Originally Posted by ziggy25
Hi,
Can you explain what the {} \; characters are for.
thanks
|
The post you quoted already explained part of it "{}" 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.
|
|
|
|
12-02-2009, 11:21 PM
|
#13
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,261
|
@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
|
|
|
|
01-07-2010, 03:48 PM
|
#14
|
|
LQ Newbie
Registered: Nov 2009
Location: London
Distribution: Ubuntu
Posts: 11
Rep:
|
Quote:
Originally Posted by trunikov
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!
|
|
|
1 members found this post helpful.
|
10-21-2010, 06:01 PM
|
#15
|
|
LQ Newbie
Registered: Oct 2010
Posts: 1
Rep:
|
hello!
Hello
im new..
regars to all
|
|
|
3 members found this post helpful.
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:34 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|