Search code examples
unixls

LS - sort by date, display only file name


I'm trying to list some files, but I only want the file names, in order of file date. I've tried a few commands but they don't see to work.

I know that using this code I can list only the file names:

ls -f *

And I know that using this command I can list the files sorted by date:

ls -ltr *

So I have tried using this command to list the file names only, sorted by file date, but it doesn't sort by date:

ls -ltr -f *

That last command simply lists the file names, but sorted by file name, not date.

Any ideas how I can do this with a simple ls command?

FYI, once I get this working my ultimate goal is to only list the most recently created 10 file names, using something like this:

ls -ltr -f * | tail -10

Solution

  • You could try the following command:

    ls -ltr | awk '{ print $9 }' | tail -n +2
    

    It extracts the file names from the ls -ltr command.