0

I'm googling for this, but the terms are so generic I'm getting way too many false positives, so what the hey, I'll ask here. I'm sure there's a simple answer but I don't generally script in *nix.

I'm trying to write a database refresh script to our test environment. The backups are mounted from an smbfs share, something like /data/backups/prod, but the subfolders get a little tricky. It looks like myproddbserver/yyyymmdd, with a bunch of files underneath.

I want to execute a command restore that will collect the files with known names from the most recent yyyymmdd directory. For example, if I had:

myproddbserver/20130630/foo.bak myproddbserver/20130630/bar.bak myproddbserver/20130731/foo.bak myproddbserver/20130731/bar.bak 

I would want to restore myproddbserver/20130731/foo.bak and myproddbserver/20130731/bar.bak.

How would I reliably get those paths?

0

1 Answer 1

1

Since you are using a yyyymmdd format, a numerical sort will work fine. The fastest way will be using the sort command:

newest_dir=$(printf '%s\n' myproddbserver/* | sort -rn | head -n1) 

You can also do this in pure bash:

newest=0 for d in myproddbserver/*; do (( d > newest )) && newest=$d done newest_files=("myproddbserver/$newest/"*) printf '%s\n' "${newest_files[@]}" 

The bash version is likely slower, but will handle all possible filenames. The sort method will fail if any filenames contain a newline character.

1
  • Works like a champ; exactly what I needed. Thanks. Commented Aug 13, 2013 at 17:56

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.