Running a command against a selection of files

Lets say you want to pull out some data from a few SQL Lite databases. It would be painful to execute your SQL statement against each DB one at a time. Here are two ways to do that assuming you know that each DB is called “some.db”.

Using exec

find . -name "some.db" -exec sqlite3 {} "SELECT Some_Data from Some_Table" \;
  • The {} represents the placeholder that will be substituted with each path/filename that is found.
  • The \; implies that you want to run the command for each path/filename found.
  • Swapping out \; with +; signifies that you instead want to concatenate all path/filenames and then use that as the argument to the command.
  • You may also use a regular expression in the find command to control the granularity of matches

Using xargs

find . -name "some.db" | xargs -n1 -iDB sqlite3 DB "SELECT Some_Data from Some_Table"
Bash Shell tips