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

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

问题描述

  • 操作系统:Linux

  • Operating system: Linux

文件系统类型:ext3

首选解决方案:Bash(脚本/单行)、Ruby 或 Python

Preferred solution: Bash (script/one-liner), Ruby, or 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

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

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 like, 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.

推荐答案

试试这个:

#!/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).

如果有很多文件,它可能需要一段时间才能返回任何内容.如果我们改用 xargs 可以提高性能:

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

这有点快.

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

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