Mr Goobri
Mr Goobri

Reputation: 1469

Run latest mysqldump from bash script

I have a load of mysqldumps like this:

dump.data.YYYYMMDD.sql

Any ideas how I can run the latest one from bash?

Thanks in advance.

Upvotes: 0

Views: 137

Answers (2)

konsolebox
konsolebox

Reputation: 75478

You can do this:

#!/bin/bash
shopt -s nullglob  ## Make no expansion if no file is found from pattern.
if read -r LATEST < <(printf "%s\n" dump.data.*.sql | sort -rn); then
    echo "Processing $LATEST."
    (do something with $LATEST)
fi

Upvotes: 1

Michael - sqlbot
Michael - sqlbot

Reputation: 179004

Try this:

#!/bin/bash
LATEST=$(ls -1t dump.data.*.sql | head -n 1)
echo $LATEST

Note that character in ls -1t before the 't' is the digit '1' not the letter 'l'.,

Upvotes: 2

Related Questions