如何根据年份移动文件

Omi*_*uin 18 find recursive files

我需要根据一年移动文件。我使用了find命令

find /media/WD/backup/osool/olddata/ -mtime +470 -exec ls -lrth {} \;|sort -k6
Run Code Online (Sandbox Code Playgroud)

但是要成功执行此命令,我需要知道mtime现在的确切值470 只是一个猜测。意味着如果我可以给出 2012 年,它会给我只与 2012 相关的文件。

所以我需要关于如何的建议

根据年份(例如 2012)查找文件并将它们移动到其他目录。

OS release 5.2

FIND version
GNU find version 4.2.27
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION SELINUX 
Run Code Online (Sandbox Code Playgroud)

Ant*_*hon 45

您想将该-newermt选项用于find

find /media/WD/backup/osool/olddata/ -newermt 20120101 -not -newermt 20130101
Run Code Online (Sandbox Code Playgroud)

获取2012年修改时间的所有文件。

如果您的发现不支持,-newermt您还可以执行以下操作以防止使用偏移计算:

touch -d 20120101 /var/tmp/2012.ref
touch -d 20130101 /var/tmp/2013.ref
find /media/WD/backup/osool/olddata/ -newer /var/tmp/2012.ref -not -newer /var/tmp/2013.ref
Run Code Online (Sandbox Code Playgroud)

联机帮助页

-newerXY reference
          Compares the timestamp of the current file with reference.   The
          reference  argument  is  normally the name of a file (and one of
          its timestamps is used for the comparison) but it may also be  a
          string  describing  an  absolute time.  X and Y are placeholders
          for other letters, and these letters select which time belonging
          to how reference is used for the comparison.

          ...

          m   The modification time of the file reference
          t   reference is interpreted directly as a time
Run Code Online (Sandbox Code Playgroud)

  • newermt 开关直到 v4.3.3 之后的版本才出现在 find 中,http://www.gnu.org/software/findutils/manual/html_mono/find.html。 (3认同)

Hau*_*ing 6

touch --date=2011-12-31T23:59:59 start
touch --date=2012-12-31T23:59:59 stop
find / -newer start \! -newer stop -printf %Tx" "%p\\n
Run Code Online (Sandbox Code Playgroud)

-exec ls 没有任何意义。


Bra*_*ley 5

根据手册页, -mtime 的参数是您要查找的天数。您可以用来date +%j查找自今年 1 月 1 日以来的天数。

  • 好方法。非常“挑剔”: -mtime -1 :是过去 24 小时内的任何文件。不仅仅是昨天(或今天)的文件。因此,还会有一点“重叠”:如果您确定自 2012 年以来已经过去了 120 天,而今天启动脚本时是 15:45,您最终将选择截至 12 月 31 日 15:45 的文件(而不是当天的最后一个)...您可能可以使用:nb_day - 1,然后“淘汰”2013 年以来的少数几个(这些可以通过 ls -l | grep -v 找到,但是如果它们仅存在 1 天,则输出会有所不同......有很多陷阱......) (2认同)