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!
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 thatfind
knows that's the end of the command we are executing.So the line should be:
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:
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:
Instead, use option -maxdepth 1 for only files in /sourcedirectory
You can use the below command with atime if the files are accessed often
You can use this command, and specify that you only find for files, not directory, and the file is older than one year
Correct would be remove ending forward slash from
/sourcedirectory/