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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a directory with daily backups of my entire MySQL database.
I want to import the most recent backup into the database.

I know to import a backup I need to use mysql -u root -ppasswordhere < backup.sql

I've managed to get the most recent file with ls -Art | grep '.sql' | tail -n 1, but I don't really know how to pipe the output to the command mysql -u root -ppasswordhere

I am sure the answer is really obvious, I'm just confused.

share|improve this question
up vote 3 down vote accepted

Have you tried this?

mysql -u root -ppasswordhere < $(ls -Art | grep '.sql' | tail -n 1)
share|improve this answer
Works just great. Thanks! – Ivan Dokov Dec 31 '13 at 13:54
you are welcome. if you want to accept the answer then please click on the according tick button. like shown here: i.stack.imgur.com/uqJeW.png – mauro.stettler Dec 31 '13 at 13:56
I know, I know. I was waiting for the time limit. I cannot accept the answer in the first... (I guess) 15 minutes. – Ivan Dokov Dec 31 '13 at 15:16

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.