Is it possible, using grep
, find
, etc, to sort a list of directories by the last modified date of the same-named file (e.g., file.php
) within? For example:
domain1/file.php (last modified 20-Jan-2014 00:00)
domain2/file.php (last modified 22-Jan-2014 00:00)
domain3/file.php (last modified 24-Jan-2014 00:00)
domain4/file.php (last modified 23-Jan-2014 00:00)
Each directory has the same file name (e.g., file.php
).
The result should be:
domain3/ (last modified 24-Jan-2014 00:00)
domain4/ (last modified 23-Jan-2014 00:00)
domain2/ (last modified 22-Jan-2014 00:00)
domain1/ (last modified 21-Jan-2014 00:00)
Best Answer
As Vivian suggested, the
-t
option ofls
tells it to sort files by modification time (most recent first, by default; reversed if you add-r
). This is most commonly used (at least in my experience) to sort the files in a directory, but it can also be applied to a list of files on the command line. And wildcards (“globs”) produce a list of files on the command line. So, if you sayit will list
But, it you add the
-1
(dash one) option, or pipe this into anything, it will list them one per line. So the command you want isThis is an ordinary
s/old_string/replacement_string/
substitution insed
, but using|
as the delimiter, because theold_string
contains a/
, and with an emptyreplacement_string
. (I.e., it deletes the filename and the/
before it —/file.php
— from thels
output.) Of course, if you want the trailing/
on the directory names, just dosed 's|file.php||'
orsed 's/file.php//'
.If you want, add the
-l
(lower-case L) option tols
to get the long listing, including modification date/time. And then you may want to enhance thesed
command to strip out irrelevant information (like the mode, owner, and size of the file) and, if you want, move the date/time after the directory name.This will look into the directories that are in the current directory, and only them. (This seems to be what the question is asking for.) Doing a one-level scan of some other directory is a trivial variation:
To (recursively) search the entire directory tree under your current directory (or some other top-level directory) is a little trickier. Type the command
and then replace the asterisk (
*
) in one of the above commands with two asterisks (**
), e.g.,