38

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!

cr0c
  • 1,116
  • 3
  • 15
  • 32

6 Answers6

54

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 like this -mtime +365.

You may also be interested in the -maxdepth 1 flag, which prevents you from moving items in sub directories.

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

At the end of the line we add \; so that find knows that's the end of the command we are executing.

So the line should be:

find /sourcedirectory -maxdepth 1 -mtime +365 -type f -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, like this:

find /sourcedirectory -maxdepth 1 -mtime +365 -type f -exec ls -l {} \;
gm3dmo
  • 9,632
  • 1
  • 40
  • 35
Jenny D
  • 27,358
  • 21
  • 74
  • 110
  • 3
    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 08 '13 at 19:00
  • 2
    What does the backslash at the end of this line do? – Ben Liyanage Mar 19 '16 at 20:07
  • 1
    @BenLiyanage The backslash is an escape character for the semicolon that follows. The semicolon means the end of the `exec` statement. See the man page for `find`. – Jenny D Mar 20 '16 at 11:27
5

Be careful when using the above solutions, I used them and ended up moving all files in all subfolders!!!!

This command moves all files in /source directory and all subfolders under source directory:

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

Instead, use option -maxdepth 1 for only files in /sourcedirectory

find /sourcedirectory -maxdepth 1 -mtime +365 -exec mv "{}" /destination/directory/ \;
harleygolfguy
  • 151
  • 1
  • 3
  • The highest rated answer contains this note, and suggests using `-type f`. Did you mean this instead to mean that the subfolders do get moved but the contents of them remain in the subfolders instead of getting moved out of them or...? – austinian Jul 15 '15 at 02:41
  • 1
    no, I mean that 'find' will find all files in /sourcedirectory and all subfolders within that /sourcedirectory, which I didn't think about at the time. The '-maxdepth 1' restricts the 'find' to only /sourcedirectory – harleygolfguy Jul 16 '15 at 15:36
  • BTW, I would have had this as just a comment to the highest rated answer, which was a good answer, but I don't have the reputation. :) – harleygolfguy Jul 16 '15 at 15:38
1

You can use the below command with atime if the files are accessed often

find /sourcedirectory -type f -atime +365 -exec mv -t /destinationdirectory {} +;
masegaloeh
  • 17,978
  • 9
  • 56
  • 104
0

You can also pipe to the while command like this:

find /sourcedirectory/ -mtime +365 -print | while read line ; do mv $line /destination/directory/ ; done
Mark
  • 101
  • 1
0

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/ \;
cuonglm
  • 2,346
  • 2
  • 15
  • 20
0
$ find /sourcedirectory/ -maxdepth 1 -mtime +365 -exec mv "{}" /destination/directory/ \;

find: missing argument to `-exec'

Correct would be remove ending forward slash from /sourcedirectory/

$ find /sourcedirectory -maxdepth 1 -mtime +365 -exec mv "{}" /destination/directory/ \;
guzzijason
  • 1,370
  • 7
  • 18
  • In my tests, I was not able to replicate the `find: missing argument` error, however I do agree that dropping the trailing slash from `/sourcedirectory` is correct. – guzzijason Sep 29 '18 at 22:47