The find
command combined with -exec
or xargs
can make this really easy.
If you want to execute psql
once per file, you can use the exec
command like this
find . -iname "*.sql" -exec psql -U username -d databasename -q -f {} \;
-exec
will execute the command once per result.
The psql
command allows you to specify multiple files by calling each file with a new -f
argument. e.g. you could build a command such as
psql -U username -d databasename -q -f file1 -f file2
This can be accomplished by piping the result of the find
to an xargs
command once to format the files with the -f argument and then again to execute the command itself.
find . -iname "*.sql" | xargs printf -- ' -f %s' | xargs -t psql -U username -d databasename -q