Super User is a question and answer site for computer enthusiasts and power users. 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 .gz sql dump file (example: foo.sql.gz) that i want import in my database with the classic mysql command.

gunzip -c foo.sql.gz > foo.sql

mysql -uroot -ppassword foo < foo.sql

foo is the database.

How can i pipe these two commands in a single one?


gunzip -c foo.sql.gz | mysql -uroot -ppassword foo

but doesn't seem to work; i get gzip: stdout: Broken pipe

share|improve this question
FYI you really do not want to pass the mysql password on the command line. This leads to it being stored in ~/.bash_history for all to see. If you just put -p it will prompt you to enter your password which is much safer (and doesn't break the zcat pipe either) – thefreeman Jan 21 '13 at 23:36
You orignal command would work if gunzip < foo.sql.gz | mysql -uroot -ppassword foo – Yada Feb 1 '13 at 20:49
up vote 30 down vote accepted
zcat foo.sql.gz | mysql -uroot -ppassword foo

This will also leave foo.sql.gz as it is.

share|improve this answer
thanks, it works! – apelliciari Mar 19 '12 at 13:35
In terms of speed this works much more slower than gunzip && mysql -e "use unzipped.sql". – podarok Jan 21 at 10:08

For those on Max OSX there is a bug with zcat so you'll need to use gzcat instead.

gzcat foo.sql.gz | mysql -uroot -ppassword foo
share|improve this answer

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.