I'm writing a backup script of sorts. My backup server contains 3 directories (named by date in the MM-DD-YYYY format) For example:

  • 12-22-2015
  • 12-28-2015
  • 12-29-2015

Im writing my backup script now to find the oldest directory (12-22-2015) and will name it to todays date (in preparation for an rsync).

My question is, is it possible to find the oldest directory (can be done by either the oldest directory name which is MM-DD-YYYY -OR- by the unix timestamp for that directory -- would prefer unix timestamp as this should be more accurate?)

The catch is I need to store the directory name of the oldest directory as a variable in my local script

Im performing all calls to my remote server via ssh "command" type calls

Thank you!

  • Read the filename, convert it to YYYY-MM-DD, sort, convert back to MM-DD-YYYY?
    – muru
    Commented Jan 1, 2016 at 18:14

1 Answer 1


ls -t sorts files by mtime (youngest first), and ls -r reverses the sort order, so both var=$(ssh $remote "ls -t | tail -n 1") or var=$(ssh $remote "ls -tr | head -n 1") should work.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .