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.
GNU Find (see
man find
) has a-printf
parameter for displying the files EPOC mtime and relative path name.Here is one version that works with filenames that may contain spaces, newlines, glob characters as well:
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 numericallyAs question is tagged with Linux so I am assuming
gnu
utils are available.You can pipe above with:
to print modification time and filenames sorted by modification time (most recent first) terminated by newlines.
You may give the printf command of find a try
Try this
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 finallystat
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.
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?)
R the recursive function that run through directories
And finally
Ignoring hidden files — with nice & fast time stamp
Handles spaces in filenames well — not that you should use those!
More
find
galore can be found by following the link.