Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am looking for a solution to move files that are year older from today. My log partition is getting full, but I can not remove them. They are needed for a long long time. Anyway one solution I came up with is:

find /sourcedirectory -mtime 365 -exec mv "{}" /destination/directory/ \;

Would this work? Asking because of the "-mtime 365" would this move the files that are year older from today to a new location?

Thank you!

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

You're almost right. '-mtime 365' will be all files that are exactly 365 days old. You want the ones that are 365 days old or more, which means adding a '+' before the number, so the line should be:

find /sourcedirectory -mtime +365 -exec mv "{}" /destination/directory/ \;

To be on the safe side, start by just doing a ls -l instead of mv - that way you can check in advance that you're getting exactly the files you want, before re-running it with mv.

If you want to be sure that you are only moving files, not directories, add -type f to the line:

find /sourcedirectory -mtime +365 -type f -exec mv "{}" /destination/directory/ \;
share|improve this answer
1  
And limit the search to files with -type f. –  SvW May 8 '13 at 10:11
    
Indeed. I'll add that to the answer. –  Jenny D May 8 '13 at 10:12
    
Thank you for the solution! :) I will add the -type f as well. –  mYzk May 8 '13 at 10:12
1  
Bear in mind that mtime is Modification time (which sounds like what you probably want -- most of the time it is). If these logs are read/referred to often you might want to use -atime (last access time). –  voretaq7 May 8 '13 at 19:00
add comment

You can use this command, and specify that you only find for files, not directory, and the file is older than one year

find /sourcedirectory -type f -mtime +365 -exec mv "{}" /destination/directory/ \;
share|improve this answer
    
Why should he use rm? Your command deletes both the log files and possibly the destination directory. –  SvW May 8 '13 at 10:08
    
Ops, my mistake, I think @mYzk want to remove this files. Edited –  Gnouc May 8 '13 at 10:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.