如何递归查找和列出目录中的最新修改的文​​件与子目录和时间? [英] How to recursively find and list the latest modified files in a directory with subdirectories and times?

查看:121
本文介绍了如何递归查找和列出目录中的最新修改的文​​件与子目录和时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 操作系统:Linux


  • 文件系统类型:ext3

  • b
  • 首选解决方案:bash(script / oneliner),ruby,python



  • 目录中有几个子目录和文件。我需要列出所有以这样一种方式构建的目录:每个第一级目录列在最近创建/修改的文件的日期和时间旁边。



    为了说明,如果我触摸一个文件或将其内容修改为几个子目录级别,则该时间戳应显示在第一级目录名称旁边。假设我有一个像这样的目录:

    $ p $ ./ alfa / beta / gamma / example.txt

    code>

    我修改了文件 example.txt 的内容,我需要那个时间以人类可读的形式显示在第一级目录 alfa 旁边,而不是时代。我已经尝试了一些使用find, xargs sort 之类的东西,但我无法解决问题在创建/修改文件时,文件系统时间戳alfa不会改变。

    解决方案

    试试这个一:

    $ p $ #!/ bin / bash
    find $ 1 -type f -exec stat --format'%Y :%y%n'{}\; | sort -nr |切-d:-f2- |头部

    执行它,以递归方式开始扫描的目录的路径(它支持带空格的文件名)。

    如果有很多文件,它可能需要一段时间才会返回任何内容。如果我们使用 xargs 代替性能可以得到改善:

     #!/ bin / bash 
    查找$ 1 -type f -print0 | xargs -0 stat --format'%Y:%y%n'| sort -nr |切-d:-f2- |头

    这有点快。

    • Operating system: Linux

    • Filesystem type: ext3

    • Preferred solution: bash (script/oneliner), ruby, python

    I have several directories with several subdirectories and files in them. I need to make a list of all these directories that is constructed in a way such that every first-level directory is listed next to the date and time of the latest created/modified file within it.

    To clarify, if I touch a file or modify its contents a few subdirectory levels down, that timestamp should be displayed next to the first-level directory name. Say I have a directory structured like this:

    ./alfa/beta/gamma/example.txt
    

    and I modify the contents of the file example.txt, I need that time displayed next to the first-level directory alfa in human readable form, not epoch. I've tried some things using find, xargs, sort and the likes but I can't get around the problem that the filesystem timestamp of 'alfa' doesn't change when I create/modify files a few levels down.

    解决方案

    Try this one:

    #!/bin/bash
    find $1 -type f -exec stat --format '%Y :%y %n' "{}" \; | sort -nr | cut -d: -f2- | head
    

    Execute it with the path to the directory where it should start scanning recursively (it supports filenames with spaces).

    If there are lots of files it may take a while before it returns anything. Performance can be improved if we use xargs instead:

    #!/bin/bash
    find $1 -type f -print0 | xargs -0 stat --format '%Y :%y %n' | sort -nr | cut -d: -f2- | head
    

    which is a bit faster.

    这篇关于如何递归查找和列出目录中的最新修改的文​​件与子目录和时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆