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 |
All times are GMT -5. The time now is 11:29 PM. |