I usually include a date in the filename so that files and directories are listed in chronological order when I use the ls command, e.g. 2015-08-29_letter_to_santa.txt.

A drawback is that tab-completion is hampered. For the example above, if I want to open the letter to santa, I first need to get through the date before I can swiftly tab-complete the rest of the filename. This is annoying when many filenames start with a date.

I would like to be able to name the file something like letter_to_santa.2015-08-29.txt but still have ls output the files sorted by the date appearing in the filename. Any ideas of how to accomplish this, and at the same time preserving the colourful output that ls produces?

For the sake of being specific, let's assume that a filename is on the format basename.date.extension and . is used exactly twice in the filename.

  • 2
    Out of curiosity, doesn't ls -clt do what you want without embedding the timestamp in the filename? Sep 3, 2015 at 14:21
  • ...knowing that if you change ls's behavior for these filenames, it will also change them for files that don't have that filename format...
    – Jeff Schaller
    Sep 3, 2015 at 14:58
  • @Andy - not if one ever changes the inode (e.g. with chmod). Or perhaps the dates aren't creation dates, but the date of the subject matter? That's something I do, so I might today write an article about last year's summer holiday, and I'd use the date of the holiday to help me find it later. Sep 3, 2015 at 17:30
  • @Andy - The date in the filename is arbitrary and independent of ctime, mtime or atime, like Toby suggested.
    – DustByte
    Sep 3, 2015 at 19:58
  • 1
    If you are using bash you can type *santa and then ask for glob-complete-word with escape-g, so you dont need to type the start of the filename.
    – meuh
    Sep 4, 2015 at 13:56

1 Answer 1



 ls | sort -t. -k2 | while read f
    ls "$f"

I am assuming you have plain ordinary unix file without funny char in name.

  • In fact, doing just ls -lF | sort -t. -k2 almost does it (the way I want it). It even preserves the colours. The niggly bits are 1. directories are mixed with regular files (I would like all directories to be listed first, sorted according to "-t. -k2" as well), 2. the line "total x" shows, and 3. filenames without a . seem to appear in arbitrary order (would be nice if they came in alphabetical order) and filenames on the format basename.extension are sorted according to extension and not its full filename. I know, I didn't ask for any of this in the original question. :)
    – DustByte
    Sep 3, 2015 at 20:24
  • Extra -k will help, I ll see tomorrow.
    – Archemar
    Sep 3, 2015 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.