You could combine ls and awk
Bash:
ls -lt /path/to/search/* | awk '{if ( NF > 1){ gsub("/path/to/search/",""); print $9"\t\t"$6" "$7" "$8} else { print $0 }}'
Where
/path/to/search
is the path to search.
NOTES:
- Leave the
/*
at the end of your search path for
ls
.
- Replace
/path/to/search/
in the
awk
command too. But don't use any shell shortcuts like
~
in there. Use the absolute path to the search directory.
It doesn't quite output things exactly how you specified, but it's pretty close and does sort the files by date AND displays the files on a single line.
The output should be something like this:
Code:
{filename} {date}
{filename} {date}
{filename} {date}
/path/to/search/sub-dir1:
{filename} {date}
{filename} {date}
{filename} {date}
/path/to/search/sub-dir2:
{filename} {date}
{filename} {date}
{filename} {date}
.....
Where
{filename}
and
{date}
are placeholders for the filename and date-stamp of the files, respectively.
The initial list of files at the start will be the files directly in the search directory.
Then for any sub-directories, you'll have the sub-directory's full path/name and then a list of the filenames and dates of any files.
I've used
ls -lt /path/to/search/*
to provide us with a recursive list of files in the given directory and any sub-directories.
The
-t
option tells
ls
to sort the files by date (Most recent first).
The
-l
option tells
ls
to put each result on a separate line.
But this also includes a lot of other information, like the permissions, ownership, date-stamp and the file-name.
So in order to get the output in the format you requested, I'm piping the results to the
awk
command, which then checks the number of fields in each line of output from
ls
and then filters the output.
Looking at the output from the
ls
command I used, (
ls -lt /path/to/search/*
) we can see the following patterns:
If we only have one field/column in the output - the output is a path to a sub-directory.
If we have more than one field - it's output from a file (with all of the permissions/group/date information).
So in
awk
,
if we have more than one field/column, we know we're dealing with a file-listing.
So we need to filter the output a little.
We first use
awk
's
gsub
function, to remove the search path from the listing.
The reason for this is because for files in the main search directory,
ls
will always print the full path/filename.
Then we print the 9th field ($9, the filename) and then two tab characters, followed by the 6th, 7th and 8th fields (date-stamp), separated by spaces.
Otherwise, we must have zero or one fields in the output (meaning either a blank line, or path to a sub-directory), so we just print the entire line ($0).
BTW - I haven't tested any of this code - I'm on my phone ATM - working purely from my admittedly patchy memory of what the
ls
commands output looks like.
But I'm pretty certain with the
-lt
options, the fields look something like this for files:
Code:
drwxrwxrwx+ 1 user group file-size month day yearOrTime filename
If the file is in the actual search directory, I'm pretty certain that the
-l
option outputs the full path/filename at the end.
Whereas if it's in a sub-directory, it only lists the filename, without the path.
Either way - to catch the case where we're listing files in the main search directory, awk will remove the search-path, so you only see the file-name.
Hopefully I've got this right and it's close to what you want.
If anything is wrong, it may just be the field numbers need tweaking. When I get home, I'll open this thread and will test out the code!