I have a series of files with names of the form "foo.date.bar" where "date" is a six digit field such as 310715.
So for example
foo.310715.bar
foo.260815.bar
foo.110815.bar
foo.040815.bar
I would like to sort these into date order, based on the date in the filename not any file metadata, so that my script can delete some of them. Normally I would do this in Python or PHP where it would be easy, but I'm trying to learn how to do things in Bash. I made a first attempt with the command
for f in $( find $dir -type f | sort -r -t. -k 2 ); do
echo $f
done
but then I realised sorting the second column either numerically or alphabetically is no use, I have to sort it as a date. There doesn't seem any way to tell sort
how to treat the six-digit field as a date, or how to treat it as three 2-digit columns. I wondered if the next step would be to use sed
or tr
or suchlike to turn the six-digit field into something sort
can parse?
Thanks in advance for any assistance,
MB
Thanks to everyone for your excellent answers, I've learned a lot from reading them.