3

I have a number of sql files to import. For a single file I use the following command:

mysql -u root -p dbname < db.sql

Is it possible to use xargs to bulk import all files? something like: ls *.sql | xargs ....

I know that mysqlimport is the solution but I want to know if it's possible with xargs or not.

4

xargs is suitable to transform input into command line parameters. But as mysql not accepts SQL script file name parameter, xargs is not handy in this case.

This is a useful use of cat:

cat *.sql | mysql -u root -p dbname

Anyway, your attempt to use ls in that way leads to the famous Why you shouldn't parse the output of ls(1) article.

  • Con cat enation, who knew! – jordanm Oct 23 '12 at 21:19
2

If your sql files are independent of eachother it may be faster to import them in parallel:

parallel -j10 sql mysql://root:passwd@/dbname '<' {} ::: *.sql

Watch the intro video to GNU Parallel to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

0

This command will load all sql files in the current folder, doing 8 in parallel and remove each file if it loads successfully:

find . -type f -name "*.sql" -print0 | xargs -0 -n1 -P8 sh -c 'cat $0 | mysql -A -u<redacted> -p<redacted> -h<redacted> dbname && rm $0'

(I don't claim this is the best way to load sql files, but it does satisfy the question of using xargs to bulk import files)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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