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?
|
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 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. |
Thank you for your help.
|
find /u1/database/prod/arch -type f -mtime +3 -exec rm {} \;
What does the ; at the end do? |
man is your friend. Try
Code:
man find 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. |
Wonderful
Thanks for your help!!!
|
Quote:
|
Quote:
|
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. |
Quote:
Can you explain what the {} \; characters are for. thanks |
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. |
Quote:
Code:
(\;) |
@trunikov
1. Unix does not a have file creation time Quote:
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 |
Quote:
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! |
hello!
Hello
im new.. regars to all |
Quote:
|
Hello all. Great thread so far, really helpful. I do have a question though.
I want to take this script Code:
#!/bin/bash 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! |
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. |
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.
|
find /path/dir -mtime +30 -delete
this is not working for me. |
Quote:
Code:
exec 3>&1 1>/path/to/logfile ; find . -print -delete ; exec 1>&3 Code:
( exec 1>/path/to/logfile ; find . -print -delete ) |
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. 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. |
Hi,
Quote:
Quote:
Quote:
Quote:
BTW: Reported. |
Quote:
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 |
Quote:
Judging on who is a "jerk" and who is not by the work they do is a rather simple-minded point of view. |
Moderator response
Hi,
Quote:
Quote:
There is no need for personal attacks; Quote:
My suggestion to you is to tone it down; Quote:
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! |
Reporting deletions
Quote:
Code:
find /home/backup/ -type f -exec echo Removing old backup: {} \; -exec rm {} \; Code:
find /home/backup/ -type f -exec rm -v {} \; 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. |