Sunday, February 1, 2015

Remove file(s) in directory recursively older than certain number of days using find and xargs

find <dir_path> -type f -mtime +<days> | xargs rm

for example to remove files older than 3 days:

find /data/log -type f -mtime +3 | xargs rm

for older than 5 minutes:
find /data/log -type f -mmin +5 | xargs rm

find files changed within last # days:
find /dir -type f -mtime -{num_day} | xargs rm


Even though all the examples above uses 'rm' to remove files, you can substitute 'rm' with other commands such as 'ls' to list all files recursively.

No comments:

Post a Comment