Searching for files / Listing files

Diablo

New Member
Joined
Oct 18, 2022
Messages
2
Reaction score
0
Credits
20
Hello,

How can I list all files back to back and by date without the folders?
Everything I've tried list the folders, then I need to open the folders to see the files. Now, is there a way to list files directly?
filea
fileb
filec
filed
filee
etc..

And maybe like this
filea ... folder name
fileb ... folder name
filec ... folder name
filed ... folder name
filee ... folder name
 


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!
 
Homework?

I'll bite.
This is a two command solution which works from the current directory:
Code:
find . -type f  -printf "%p %TY%Tm%Td\n" |for i in $(sed 's/\ /-/'); do basename $i; done > /tmp/fileOfFilenames

cat /tmp/fileOfFilenames |  sed 's/-/\ /' | awk '{print $2, $1}' | sort  -n > /tmp/fileOfFilenames

It's output goes to a file: fileOfFilenames in /tmp, and the contents looks like so:
Code:
....
20220914 file1
20220915 file2
20220916 file3
....
The user can add a sed/awk command to delete the date from the output in the fileOfFilenames, but I include it to show the sort by date from the earliest. The space in the first sed command was changed to - so that basename could delete full pathnames. Then sed is used to reverse the - to a space to separate the date from the file name. Then date and filename are reversed on the line to invoke sort on the numbers. I have assumed that the filenames have no spaces.
 
Last edited:
Moving this to Command Line

Wiz
 
Wow! you guys are just fantastic. However, it seems very complicated for a very simple thing. I'll give it a try tomorrow because I'm curious to see it working. It will also help when I save a file and forgot in which directory I used to save it!

Thank you very much
 
As I understood it, the original post asked for a list "by date without the folders", which is what post #3 achieves. If you want folders (directories), then JasKinasis' post #2 will get you that.

It seems that this post I made has been truncated in the outage which happened. In brief, to repeat the essence of it, the OP can try these ls commands to see if they get a desirable result:
Code:
ls -Ralt
will get a listing of all files and directories under the directory from which the command is run, and the files will have the dates of the files running from newest first in each directory.

Code:
ls -R1t
will also show a listing of the files and directories under the directory from which it is run but will show just the files and directory names with the files arranged from newest first in each directory first, one file per line. Note the number 1.
 
Last edited:

Staff online

Members online


Top