Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Diary has entries with month and day, no years, in the format %D_%M.tex but index in wrong order because of wrong sorting and poor filename -format. How should I call my Diary -entries to get them sortable by sort -command (from old to new or the reverse)? The key is radix -sorting, notice this discussion here, but it is still open which one of them. By which Radix -sorting algorithms and when? What are the pros-and-cons?

STOP here if you know sorting -- the following part is for those wanting to reproduce the diary with small working example and intended output

Wrong Output

 $ ls Days/ |sort -M 

Intended Output


Small example (with wrong sorting -order n.b. the radix -bug)

$ ls Days/
12_4.tex  29_3.tex  3_4.tex
$ cat Makefile 
.PHONY: all
all:   days.tex  

days.tex: $(wildcard Days/*.tex)
  ls Days/*.tex | awk '{printf "\\input{%s}\n", $$1}' > days.tex
$ make; pdflatex diary.tex
$ cat diary.tex


  1. Radix sorts in Wikipedia here.
share|improve this question
When do you write entries. If you write them fresh on the day, the the solution could be as simple as ls -1t – emory Apr 22 '12 at 22:24
@emory: good observation but I fool in this diary because it is a course-must-do -diary for the course here. So I need to re-edit and improve it later but good start! Nice one-liner! – user2362 Apr 22 '12 at 22:28
Extremely good discussion here, simplicity rocks! %Y_%M_%D makes it like a radix number, easy to sort. I think this question although got excellent answers in chat belongs here because it is a practical example how to use sorting in practise, cool. – user2362 Apr 22 '12 at 22:54
Wikipedia: "Unlike a least significant digit (LSD) radix sort, a most significant digit radix sort does not necessarily preserve the original order of duplicate keys." -- and something about radix-sorting in tri-structure, the structure can change the result of the same algorithm. The LSD reserving the original order can become handy...investigating. – user2362 Apr 23 '12 at 2:43

1 Answer 1

up vote 0 down vote accepted

An alternative is to rename the files:

for FILE in Days/*
      DAY=`cut --delimiter '_' --fields 1 ${FILE}`
      MONTH=`cut --delimiter '_' --fields 2 ${FILE} | cut --delimiter '.' --fields 1
      cp ${FILE} ${NEWFILE}

or you could use the touch command so that ls -1t would work.

share|improve this answer
+1 for implicitly using the Radix -format, yes it is the key -- now the puzzle is when and which radix sorting to use with a diary. Pros and cons? – user2362 Apr 22 '12 at 23:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.