大网数据广告活动
服务器运维问答

Move files to another directory which are older than a date

· 52CE 我爱测

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 /sourcedirec…

问题原文

原帖链接 · cr0c · CC BY-SA 3.0

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!

已采纳解答

原帖链接 · Jenny D · CC BY-SA 3.0

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 {} \;

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2013-05-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。