如何列出文件名中包含“2015”的一天以上的文件?

rɑː*_*dʒɑ 11 command-line find

我想列出所有不属于今天的日志文件,只2015列出名称中包含的文件。

这些是我尝试过的命令:

find . -name 2015 -mtime +1 -exec  ls -ltrh  {} \;
Run Code Online (Sandbox Code Playgroud)
find . -name *2015* -mtime +1 -exec  ls -ltrh  {} \;
Run Code Online (Sandbox Code Playgroud)

两者的输出都没有,但文件仍然存在。这些命令有问题,我没有得到什么。任何帮助,将不胜感激。

Eli*_*gan 20

该命令find . -name '*2015*' -mmin +1440 -ls可能会执行您想要的操作。详情请见下文。

你的第一个命令有-name 2015. 它没有工作,因为它发现只是有些文件的名称完全相同 2015,与他们没有其他字符。

您的第二个命令 ,find . -name *2015* -mtime +1 -exec ls -ltrh {} \;可能由于以下几个原因而失败:

1. 未加引号的*字符由 shell 展开,然后传递给find.

如果当前目录中直接包含任何文件(运行该find ...命令时所在的目录),其名称包含2015(并且不以 a 开头.),则shell扩展*2015*为这些文件名的列表,然后传递该列表作为find. 这不是您想要的——相反,您希望将*2015*字面上的参数作为参数传递给 find,以便find而不是 shell 可以找到与它匹配的文件。

要解决该问题,请引用*2015*. 有三种常见的方法可以做到:

  • '*2015*'(即,find . -name '*2015*' -mtime +1 -exec ls -ltrh {} \;
  • "*2015*"(即,find . -name "*2015*" -mtime +1 -exec ls -ltrh {} \;
  • \*2015\*(即,find . -name \*2015\* -mtime +1 -exec ls -ltrh {} \;

建议用单引号将其写为'*2015*',因为:

但在这种情况下,这并不重要。'并且"两者都对待*相同,并且表达式不够复杂,无法\引用以使其难以理解。

2.-mtime +1只选择两天或更多天前修改过的文件。

正如man find所说:

       Numeric arguments can be specified as

       +n     for greater than n,

       -n     for less than n,

       n      for exactly n.
Run Code Online (Sandbox Code Playgroud)
       -mtime n
              File's data was last modified n*24 hours ago.  See the  comments
              for -atime to understand how rounding affects the interpretation
              of file modification times.
Run Code Online (Sandbox Code Playgroud)
       -atime n
              File was last accessed n*24 hours ago.  When  find  figures  out
              how  many  24-hour  periods  ago the file was last accessed, any
              fractional part is ignored, so to match -atime +1, a file has to
              have been accessed at least two days ago.
Run Code Online (Sandbox Code Playgroud)

假设一个文件在 47 小时前被修改过。为了弄清楚有多少个24小时周期也就是find回合下来:它是一个24小时前。但-mtime +1只匹配修改时间严格超过24 小时前的文件。因此昨天的文件不匹配。

请参阅为什么 find -mtime +1 只返回早于 2 天的文件?有关更多信息,请参考 steeldriver 的建议

要查找的文件随时超过24小时前最后一次修改,我建议,而不是规定它作为1440分钟前-mmin +1440

       Numeric arguments can be specified as

       +n     for greater than n,

       -n     for less than n,

       n      for exactly n.
Run Code Online (Sandbox Code Playgroud)

一些读者可能想知道为什么我没有引用{}。有些人引用{}以提醒人类它不是括号扩展的表达式。伯恩式炮弹(如bash的要求{}与里面什么都没有被引用。也许一些非 Bourne 风格的 shell 会特别对待它;这可能就是一些用户引用它的原因。但也有一种误解,认为有时必须引用{}这样才能-exec正确处理带空格的文件名。That;s false: with {}or '{}',find得到相同的参数,因为shell在传递给之前删除了引号'{}'find. 为了应对这种误解,我不要引用{},但它是一个风格问题-如果你喜欢{}到文档如何壳不治疗{}特殊,这很好。

我建议您也更改您的ls命令,或者(如穆鲁所建议的)将其替换为find-ls操作。ls -ltrh可能没有按照您的意图执行,因为它为找到的每个文件单独运行,因此指定排序的-t-r标志无关紧要。

尽管输出的格式与 with 略有不同ls -l,但使用-ls更简单、更容易。

       -mtime n
              File's data was last modified n*24 hours ago.  See the  comments
              for -atime to understand how rounding affects the interpretation
              of file modification times.
Run Code Online (Sandbox Code Playgroud)

或者如果你决定你真的只需要列出文件的名称(包括它们的路径,相对于.),你可以简单地指定不动作,从而导致使用默认-print动作:

       -atime n
              File was last accessed n*24 hours ago.  When  find  figures  out
              how  many  24-hour  periods  ago the file was last accessed, any
              fractional part is ignored, so to match -atime +1, a file has to
              have been accessed at least two days ago.
Run Code Online (Sandbox Code Playgroud)