【问题标题】:How to find the latest executable file in a directory如何在目录中找到最新的可执行文件
【发布时间】:2014-10-28 02:20:58
【问题描述】:

我在linux上,我想知道如何在目录中找到最新的可执行文件? 我已经知道如何找到最新的:

ls -rt1 | tail -1

但是如何过滤掉可执行文件呢?


编辑: 我找到了解决方案:

find path/to/dir/myfile* -perm /u=x,g=x,o=x -mtime 0 | tail -1

这是存档吗?还是有更好的解决方案??

【问题讨论】:

    标签: linux bash find


    【解决方案1】:

    给定基本的find 命令来查找从当前目录开始的文件:

    find . -type f
    

    让我们添加功能:

    • 要查找可执行文件,您可以使用-executable 选项:

      find . -type f -executable
      
    • 要仅在一个深度级别上查找,即不在子目录中,请使用 -maxdepth 1 选项:

      find . -maxdepth 1 -type f
      
    • 要在目录中查找最后修改的文件,可以使用How to recursively find the latest modified file in a directory?

      find . -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" "
      

    总而言之,这会在一级深度中查找最后修改的可执行文件:

    find . -maxdepth 1 -type f -executable -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" "
    

    【讨论】:

    • 好的,但我想获取特定目录的 latest 可执行文件,而不是所有可执行文件。我找到了添加到帖子中的解决方案,请查看编辑说明。我想问一下这是保存还是有更好的解决方案
    • 您阅读完整答案了吗?最后一个命令返回一个文件,您只需将find . 替换为find /your/dir
    猜你喜欢
    • 2012-03-11
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    • 2021-01-12
    • 2012-09-13
    • 1970-01-01
    相关资源
    最近更新 更多