How to recursively find and list the latest modifi

2019-01-03 19:00发布

  • 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.

16条回答
Explosion°爆炸
2楼-- · 2019-01-03 19:35

GNU Find (see man find) has a -printf parameter for displying the files EPOC mtime and relative path name.

redhat> find . -type f -printf '%T@ %P\n' | sort -n | awk '{print $2}'
查看更多
forever°为你锁心
3楼-- · 2019-01-03 19:35

Here is one version that works with filenames that may contain spaces, newlines, glob characters as well:

find . -type f -printf "%T@ %p\0" | sort -zk1nr
  • find ... -printf prints file modification (EPOCH value) followed by a space and \0 terminated filenames.
  • sort -zk1nr reads NUL terminated data and sorts it reverse numerically

As question is tagged with Linux so I am assuming gnu utils are available.

You can pipe above with:

xargs -0 printf "%s\n"

to print modification time and filenames sorted by modification time (most recent first) terminated by newlines.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-03 19:35

You may give the printf command of find a try

%Ak File's last access time in the format specified by k, which is either @' or a directive for the C strftime' function. The possible values for k are listed below; some of them might not be available on all systems, due to differences in `strftime' between systems.

查看更多
贪生不怕死
5楼-- · 2019-01-03 19:37

Try this

#!/bin/bash
stat --format %y $(ls -t $(find alfa/ -type f) | head -n 1)

It uses find to gather all files from the directory, ls to list them sorted by modification date, head for selecting the 1st file and finally stat to show the time in a nice format.

At this time it is not safe for files with whitespace or other special chars in their names. Write a commend if it doesn't meet your needs yet.

查看更多
\"骚年 ilove
6楼-- · 2019-01-03 19:38

This could be done with a reccursive function in bash too

Let F a function that displays the time of file which must be lexicographically sortable yyyy-mm-dd etc., (os-dependent?)

F(){ stat --format %y "$1";}                # Linux
F(){ ls -E "$1"|awk '{print$6" "$7}';}      # SunOS: maybe this could be done easier

R the recursive function that run through directories

R(){ local f;for f in "$1"/*;do [ -d "$f" ]&&R $f||F "$f";done;}

And finally

for f in *;do [ -d "$f" ]&&echo `R "$f"|sort|tail -1`" $f";done
查看更多
ら.Afraid
7楼-- · 2019-01-03 19:39

Ignoring hidden files — with nice & fast time stamp

Handles spaces in filenames well — not that you should use those!

$ find . -type f -not -path '*/\.*' -printf '%TY.%Tm.%Td %THh%TM %Ta %p\n' |sort -nr |head -n 10

2017.01.28 07h00 Sat ./recent
2017.01.21 10h49 Sat ./hgb
2017.01.16 07h44 Mon ./swx
2017.01.10 18h24 Tue ./update-stations
2017.01.09 10h38 Mon ./stations.json

More find galore can be found by following the link.

查看更多
登录 后发表回答