https://linux.cn/article-8093-1.html

在本文中,我们将解释两个简单的命令行小技巧,它可以帮你只列出所有的今天的文件。

Linux 用户在命令行上遇到的常见问题之一是[定位具有特定名称的文件](https://linux.cn/article-5973-1.html),如果你知道确定的文件名则可能会容易得多。

不过,假设你忘记了白天早些时候创建的文件的名称(在你包含了数百个文件的 `home` 文件夹中),但现在你有急用。

下面用不同的方式只[列出所有你今天创建或修改的文件](https://linux.cn/article-7984-1.html)(直接或间接)。

###
1、 使用 ls 命令,只列出你的 home 文件夹中今天的文件。

  1. <span class="com" style="color: rgb(174, 174, 174); font-style: italic; -ms-word-wrap: break-word;">#</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word;">ls</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">-</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">al </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">--</span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word;">time</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">-</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">style</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">=+%</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">D </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">|</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word;">grep</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="str" style="color: rgb(101, 176, 66); -ms-word-wrap: break-word;">&#39;date +%D&#39;</span>

    其中:

  • -a - 列出所有文件,包括隐藏文件
  • -l - 启用长列表格式
  • --time-style=FORMAT - 显示指定 FORMAT 的时间
  • +%D - 以 %m/%d/%y (月/日/年)格式显示或使用日期

    Find Recent Files in Linux

    在Linux中找出最近的文件

    此外,你使用可以 -X 标志来按字母顺序对结果排序

  1. <span class="com" style="color: rgb(174, 174, 174); font-style: italic; -ms-word-wrap: break-word;">#</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word;">ls</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">-</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">alX </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">--</span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word;">time</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">-</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">style</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">=+%</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">D </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">|</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word;">grep</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="str" style="color: rgb(101, 176, 66); -ms-word-wrap: break-word;">&#39;date +%D&#39;</span>

    你也可以使用 -S 标志来基于大小(由大到小)来排序:

  2. <span class="com" style="color: rgb(174, 174, 174); font-style: italic; -ms-word-wrap: break-word;">#</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word;">ls</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">-</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">alS </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">--</span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word;">time</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">-</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">style</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">=+%</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">D </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">|</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word;">grep</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="str" style="color: rgb(101, 176, 66); -ms-word-wrap: break-word;">&#39;date +%D&#39;</span>

###
2、 另外,使用 find 命令会更灵活,并且提供比 ls 更多的选项,可以实现相同的目的。

  •  -maxdepth 级别用于指定在搜索操作的起点下(在这个情况下为当前目录)的搜索层级(子目录层级数)。
  •  -newerXY,用于所寻找的文件的时间戳 X 比参照文件的时间戳 Y 更新一些的文件。 XY表示以下任何字母:      - a - 参照文件的访问时间      - B - 参照文件的创建时间      - c - 参照文件的 inode 状态改变时间      - m - 参照文件的修改时间      - t - 直接指定一个绝对时间

    下面的命令意思是只找出 2016-12-06 这一天修改的文件:

  1. <span class="com" style="color: rgb(174, 174, 174); font-style: italic; -ms-word-wrap: break-word;">#</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word;">find</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">.</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">-</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">maxdepth </span><span class="lit" style="color: rgb(51, 135, 204); -ms-word-wrap: break-word;">1</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">-</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">newermt </span><span class="str" style="color: rgb(101, 176, 66); -ms-word-wrap: break-word;">&quot;2016-12-06&quot;</span>

    Find Today

    在 Linux 中找出今天的文件

    重要:在上面的 find 命令中使用正确的日期格式作为参照时间,一旦你使用了错误的格式,你会得到如下错误:

  2. <span class="com" style="color: rgb(174, 174, 174); font-style: italic; -ms-word-wrap: break-word;">#</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word;">find</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">.</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">-</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">maxdepth </span><span class="lit" style="color: rgb(51, 135, 204); -ms-word-wrap: break-word;">1</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">-</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">newermt </span><span class="str" style="color: rgb(101, 176, 66); -ms-word-wrap: break-word;">&quot;12-06-2016&quot;</span>

  3. <span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word;">find</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">:</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> I cannot figure out how to interpret </span><span class="str" style="color: rgb(101, 176, 66); -ms-word-wrap: break-word;">&#39;12-06-2016&#39;</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word;">as</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> a </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word;">date</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word;">or</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word;">time</span>

    或者,使用下面的正确格式:

  4. <span class="com" style="color: rgb(174, 174, 174); font-style: italic; -ms-word-wrap: break-word;">#</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word;">find</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">.</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">-</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">maxdepth </span><span class="lit" style="color: rgb(51, 135, 204); -ms-word-wrap: break-word;">1</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">-</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">newermt </span><span class="str" style="color: rgb(101, 176, 66); -ms-word-wrap: break-word;">&quot;12/06/2016&quot;</span>

  5. <span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">或者</span>
  6. <span class="com" style="color: rgb(174, 174, 174); font-style: italic; -ms-word-wrap: break-word;">#</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word;">find</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">.</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">-</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">maxdepth </span><span class="lit" style="color: rgb(51, 135, 204); -ms-word-wrap: break-word;">1</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">-</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word;">newermt </span><span class="str" style="color: rgb(101, 176, 66); -ms-word-wrap: break-word;">&quot;12/06/16&quot;</span>

    Find Todays Modified Files in Linux

    在 Linux 中找出今天修改的文件

    你可以在我们的下面一系列文章中获得 lsfind 命令的更多使用信息。