I have a folder with o lot of sql scripts. I want to run all of them without specifying names of them. Just specify a folder name. Is it possible?
2 Answers
You can not do that natively, but here's simple bash command:
for sql_file in `ls -d /path/to/directory/*`; do mysql -uUSER -pPASSWORD DATABASE < $sql_file ; done
here USER, PASSWORD and DATABASE are the corresponding credentials and /path/to/directory is full path to folder that contains your files.
If you want to filter, for example, only sql files, then:
for sql_file in `ls /path/to/directory/*.sql`; do mysql -uUSER -pPASSWORD DATABASE < $sql_file ; done
2 Comments
JayRizzo
Here is why
ls might be bad as listed here stackoverflow.com/a/2152795/1896134 Please look at momer's comment. mywiki.wooledge.org/ParsingLsNobleUplift
The
ls command as it was previously written wasn't functional. It would list off the files in the directory but without the /path/to/ prefix, thus it wouldn't run anything.