首页 > 解决方案 > 使用 Bash 在目录中查找最近的文件

问题描述

我想编写一个 Bash 脚本在文件夹中查找新文件(最近添加的文件)并将新文件的名称(包括文件类型,即 example.png)输出到变量。

我该怎么做?

标签: bashfile

解决方案


可能您想使用以下-t选项ls

ls -t

来自man ls

 --sort=WORD
        sort  by  WORD instead of name: none (-U), size (-S), time
        (-t), version (-v), extension (-X)

 --time=WORD
        change the default of  using  modification  times;  access
        time  (-u):  atime,  access, use; change time (-c): ctime,
        status; birth time: birth, creation;

        with  -l,  WORD  determines  which  time  to  show;   with
        --sort=time, sort by WORD (newest first)

 --time-style=TIME_STYLE
        time/date format with -l; see TIME_STYLE below

 -t     sort by time, newest first; see --time

推荐阅读