LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash script to remove files older than 3 days (https://www.linuxquestions.org/questions/linux-general-1/bash-script-to-remove-files-older-than-3-days-462290/)

rust8y 07-09-2006 05:27 AM

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?

druuna 07-09-2006 05:53 AM

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.

rust8y 07-09-2006 08:49 PM

Thank you for your help.

rust8y 07-09-2006 08:53 PM

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

What does the ; at the end do?

perfect_circle 07-09-2006 09:02 PM

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.

tiekookeit 09-04-2009 09:19 AM

Wonderful
 
Thanks for your help!!!

catkin 09-04-2009 10:08 AM

Quote:

Originally Posted by rust8y (Post 2327172)
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.

Photar 10-06-2009 12:57 PM

Quote:

Originally Posted by catkin (Post 3669731)
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 ';'

trunikov 11-09-2009 08:09 AM

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.

ziggy25 12-02-2009 07:43 AM

Quote:

Originally Posted by catkin (Post 3669731)
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

druuna 12-02-2009 07:51 AM

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.

pixellany 12-02-2009 07:52 AM

Quote:

Originally Posted by ziggy25 (Post 3777052)
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

chrism01 12-02-2009 11:21 PM

@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

will177 01-07-2010 03:48 PM

Quote:

Originally Posted by trunikov (Post 3750269)
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!

dsoria 10-21-2010 06:01 PM

hello!
 
Hello

im new..

regars to all

druuna 10-22-2010 02:25 AM

Quote:

Originally Posted by dsoria (Post 4135263)
Hello

im new..

regars to all

Don't post in an old thread if you do not have anything substantial to add or have a related question. LinuxQuestions.org Member Intro is a good place to introduce yourself and get your first post.

Dirty 04-13-2011 10:15 AM

Hello all. Great thread so far, really helpful. I do have a question though.

I want to take this script
Code:

#!/bin/bash

/usr/bin/find /insert/path/here -mtime +3 -exec rm {} \;

and add logging to it.

I want to see if there is a way to get a list of what is found before it's deleted, and after it's deleted. I know there is a verbose option for rm, but I can't seem to find one for find. Any help would be greatly appreciated.

Thanks in advance!

chrism01 04-13-2011 11:31 PM

Please don't necro-post ie dig up threads so old. Instead, start a new thread.
Thank you.

In brief, find doesn't have a logging option; one approach is to use -print (instead of -exec rm ...) at the end of a copy of the same find cmd and re-direct to a file, then run the find ... rm version.

Dirty 04-14-2011 11:07 PM

After reading the thread, and particularly post 16, I figured it was a safe bump. I would've linked to this thread anyway as a reference, because people would've told me to "Search n00b" anyway. Anyway, thanks for the info.

fredie_barron 05-31-2011 05:06 AM

find /path/dir -mtime +30 -delete
this is not working for me.

crts 05-31-2011 06:55 AM

Quote:

Originally Posted by chrism01 (Post 4324104)
Please don't necro-post ie dig up threads so old. Instead, start a new thread.
Thank you.

In brief, find doesn't have a logging option; one approach is to use -print (instead of -exec rm ...) at the end of a copy of the same find cmd and re-direct to a file, then run the find ... rm version.

There is a way to run 'find' only once if use 'exec' to redirect:
Code:

exec 3>&1 1>/path/to/logfile ; find . -print -delete ; exec 1>&3
or as a subshell version:
Code:

( exec 1>/path/to/logfile ; find . -print -delete )

azvampyre 03-04-2012 02:58 AM

Quote:

Originally Posted by druuna (Post 4135599)
Don't post in an old thread if you do not have anything substantial to add or have a related question. LinuxQuestions.org Member Intro is a good place to introduce yourself and get your first post.

I'm posting in an old thread!

It's quite easy to tell who sits on their fat butt on a computer all day and who works in construction by the way some of you jerks act towards others on the internet.
If people aren't supposed to post in "an old thread", then it needs to be removed from the sitemap.xml instead of being refreshed every week for Google to keep on the first page of search so if you have a problem, take it up with the Admin who could have easily removed this page from the sitemap long ago.

You had a "newbie" introducing themselves and for that you have to act like a total bloke?

Hello dsoria. I'm azvampyre, and I'm a building contractor who works for a living, but like to program in my sparsely spare time.
Do you see how much friendlier people who work out in the nice sunlight are towards others, drunna? Maybe you should get some sun and "lighten up". I'm sorry you're fat and pale, but don't take it out on the innocent, okay.

druuna 03-04-2012 03:22 AM

Hi,
Quote:

Originally Posted by azvampyre (Post 4618014)
I'm posting in an old thread!

Yes you are, and that is against the LQ Rules.

Quote:

It's quite easy to tell who sits on their fat butt on a computer all day and who works in construction by the way some of you jerks act towards others on the internet.
If people aren't supposed to post in "an old thread", then it needs to be removed from the sitemap.xml instead of being refreshed every week for Google to keep on the first page of search so if you have a problem, take it up with the Admin who could have easily removed this page from the sitemap long ago.
No need to be condescending. It seems I'm not the one that has a problem, you seem to have one. Take your own advise and contact the LQ site owner if you want to address your issues related to LQ.

Quote:

You had a "newbie" introducing themselves and for that you have to act like a total bloke?
Yes I pointed out that digging up old threads just to introduce oneself isn't the correct thing to do (it is actually against the LQ rules). I also pointed out that there is a specific sub-forum for people that want to introduce themselves.

Quote:

Hello dsoria. I'm azvampyre, and I'm a building contractor who works for a living, but like to program in my sparsely spare time.
Do you see how much friendlier people who work out in the nice sunlight are towards others, drunna? Maybe you should get some sun and "lighten up". I'm sorry you're fat and pale, but don't take it out on the innocent, okay.
Again: Don't be so condescending (and learn to spell)

BTW: Reported.

lithos 03-04-2012 06:05 AM

Quote:

Originally Posted by azvampyre (Post 4618014)
...

Do you see how much friendlier people who work out in the nice sunlight are towards others, drunna? Maybe you should get some sun and "lighten up". I'm sorry you're fat and pale, but don't take it out on the innocent, okay.

I need to reply to this insults

azvampyre
I think your kind of breed needs to be investigated further, as I don't know of an individual that can walk out on the sun wihtout any protection. Do you have it?

My best Regards to all LQ

crts 03-04-2012 06:35 AM

Quote:

Originally Posted by azvampyre (Post 4618014)
I'm posting in an old thread!

It's quite easy to tell who sits on their fat butt on a computer all day and who works in construction by the way some of you jerks act towards others on the internet.
If people aren't supposed to post in "an old thread", then it needs to be removed from the sitemap.xml instead of being refreshed every week for Google to keep on the first page of search so if you have a problem, take it up with the Admin who could have easily removed this page from the sitemap long ago.

You had a "newbie" introducing themselves and for that you have to act like a total bloke?

Hello dsoria. I'm azvampyre, and I'm a building contractor who works for a living, but like to program in my sparsely spare time.
Do you see how much friendlier people who work out in the nice sunlight are towards others, drunna? Maybe you should get some sun and "lighten up". I'm sorry you're fat and pale, but don't take it out on the innocent, okay.

Hm, I have known people who work on construction and others who sit on their butt all day. I can assure you that there are "jerks" on both sides.

Judging on who is a "jerk" and who is not by the work they do is a rather simple-minded point of view.

onebuck 03-04-2012 08:22 AM

Moderator response
 
Hi,

Quote:

Originally Posted by azvampyre (Post 4618014)
I'm posting in an old thread!

It's quite easy to tell who sits on their fat butt on a computer all day and who works in construction by the way some of you jerks act towards others on the internet.
If people aren't supposed to post in "an old thread", then it needs to be removed from the sitemap.xml instead of being refreshed every week for Google to keep on the first page of search so if you have a problem, take it up with the Admin who could have easily removed this page from the sitemap long ago.

You had a "newbie" introducing themselves and for that you have to act like a total bloke?

Hello dsoria. I'm azvampyre, and I'm a building contractor who works for a living, but like to program in my sparsely spare time.
Do you see how much friendlier people who work out in the nice sunlight are towards others, drunna? Maybe you should get some sun and "lighten up". I'm sorry you're fat and pale, but don't take it out on the innocent, okay.

Enough!
Quote:

LQ Rules
By becoming a member you agreed to adhere to these guidelines. These guidelines are subject to change and a current version is always available in the forums. If you come across any violations to these guidelines or have any problems navigating the site, do not hesitate to let us know via email or the forums.
LQ is a moderated forum and members do help by providing insight to new users.
There is no need for personal attacks;
Quote:

Personal attacks on others will not be tolerated.
druuna's post to a newbie was not in violation. Your responding post to a nearly two year old post is not warranted nor should you have posted. If you felt that a post warrants moderation then please report that post. Your tirade and personal attacks are something we will not tolerate. Please review the LQ Rules!

My suggestion to you is to tone it down;
Quote:

Challenge others' points of view and opinions, but do so respectfully and thoughtfully ... without insult and personal attack. Differing opinions is one of the things that make this site great.
As to your references to sitemap retention, I suggest that you open a new thread in LQ Suggestions & Feedback for topics that you think need improvement, attention or revision(s).

If you have any questions then feel free to contact me personally via PM/email.

To everyone: No further posting of non related topics in this thread!

Back on topic!

Blackbit 10-04-2012 08:26 AM

Reporting deletions
 
Quote:

Originally Posted by Dirty (Post 4323424)
Hello all. Great thread so far, really helpful. I do have a question though.

I want to take this script
Code:

#!/bin/bash

/usr/bin/find /insert/path/here -mtime +3 -exec rm {} \;

and add logging to it.

There is an easy way to do that. find allows multiple usage of exec like this:

Code:

find /home/backup/ -type f -exec echo Removing old backup: {} \; -exec rm {} \;
Another idea is to tell rm to report any deletions with -v:

Code:

find /home/backup/ -type f -exec rm -v {} \;
I prefer the first method, as the text is easier to change.

btw.: The parameter "-delete" is not available for all versions of find, but rm should be ;-)


All times are GMT -5. The time now is 01:10 AM.