Linux Bash script to remove files older than 3 days

来源:互联网 发布:mastercam线割编程 编辑:程序博客网 时间:2024/05/16 15:39

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 /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.

0 0
原创粉丝点击