As Vivian suggested, the -t
option of ls
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 say
ls -t */file.php
it will list
domain3/file.php domain4/file.php domain2/file.php domain1/file.php
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 is
ls -t */file.php | sed 's|/file.php||'
This is an ordinary
s/old_string/replacement_string/
substitution in sed
,
but using |
as the delimiter, because the old_string
contains a /
,
and with an empty replacement_string
.
(I.e., it deletes the filename and the /
before it — /file.php
—
from the ls
output.)
Of course, if you want the trailing /
on the directory names,
just do sed 's|file.php||'
or sed 's/file.php//'
.
If you want, add the -l
(lower-case L) option to ls
to get the long listing, including modification date/time.
And then you may want to enhance the sed
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:
ls -t /path/to/tld/*/file.php | sed 's|/file.php||'
To (recursively) search the entire directory tree under your current directory
(or some other top-level directory) is a little trickier.
Type the command
shopt -s globstar
and then replace the asterisk (*
) in one of the above commands with two asterisks (**
), e.g.,
ls -t **/file.php | sed 's|/file.php||'
With the zsh
shell:
tail -n 5 ./*.aff(D.om[1])
With other shells, it's quite difficult to come up with something reliable if you don't want to make assumptions on what file names may contain.
For instance, the bash
equivalent, if you're on a recent GNU system would be:
find . -maxdepth 1 -name '*.aff' -type f -printf '%T@:%p\0' |
sort -rzn |
sed -zn 's/[^:]*://p;q' |
xargs -r0 tail -n 5
Or:
find . -maxdepth 1 -name '*.aff' -type f -printf '%T@/%p\0' |
sort -rzn | (IFS=/ read -rd '' mtime file && tail -n 5 "$file")
Best Answer
The last modification time of a directory (think like phone directory, not folder) is the time it was last modified, like when an entry was removed, added or edited in that directory.
To find out the newest regular file recursively in it, you would need to read the contents of that directory and every directory within and for each file, check the file's modification time. That's a costly thing to do, I wouldn't expect any file manager application to do it.
You could however script it.
With the GNU implementations of
find
andsort
(and any Bourne-like shell), you could do:Which would give something like:
That is, giving the newest regular file in each directory with its timestamp. Directories without regular files in them are not shown.
To only see the list of directories, insert a
cut -zd/ -f2 |
before thetr
command.For a prettier output like in the zsh approach, you could replace the
tr
command with:While we're at using
gawk
, we could also tellfind
to print the timestamp as a fractional Unix epoch time andgawk
reformat it in local time:Which would give an output like: