Thursday, July 5, 2012

Find command usage

How to clean up data when ever disk usage exceeds the threshold limit.
Among the many ways do it, here is the simple usage of Unix find command to delete files older than a year.

find . -type f -mtime +365 -delete
Where -mtime +365 suggests to list files older than 365 days.
or
 find . -type f -atime +365 -delete
Where -atime stands for access time which is when the file was last read.

Some more examples



find . -mtime 0             # find files modified between now and 1 day ago # (i.e., within the past 24 hours)
find . -mtime -1            # find files modified less than 1 day ago  # (i.e., within the past 24 hours, as before)
find . -mtime 1             # find files modified between 24 and 48 hours ago
find . -mtime +1           # find files modified more than 48 hours ago
find . -mmin +5 -mmin -10        # find files modified between # 6 and 9 minutes ago

find <path to dir> -mtime +365 -exec ls -la {} \;
To check that you're picking up what you want, then:

find <path to dir> -mtime +365 -exec rm {} \;
To delete them. If there are too many, try:

find <path to dir> -mtime +365 | xargs rm {}
You can also add a -name '*.txt', say, depending on your file extension, to pick up only those files you want.

No comments:

Post a Comment