SQLite | Commands | Writing SELECT Results to a File with .output

The SQLite command-line tool normally displays SQL and command results on screen. The .output command redirects them to a file.

Redirect output to a file

.output ?FILENAME?

Specify an absolute path or a path relative to the directory in which SQLite was started. Use stdout to restore screen output.

.output /Users/devkuma/test/sqlite/logdata.txt
.output ../../test/sqlite/logdata.txt

After .output is set, all text that would have appeared on screen—including SELECT results and SQLite command output—is written to the file. SQLite creates a missing file and appends to an existing one.

Use .show to inspect the current destination:

sqlite> .show
        echo: off
         eqp: off
     explain: auto
     headers: off
        mode: list
   nullvalue: ""
      output: stdout
colseparator: "|"
rowseparator: "\n"
       stats: off
       width:
    filename: newdb.sqlite3

output: stdout means results currently appear on screen. For example:

select * from user;
devkuma|28|Seoul
kimkc|22|Busan
araikuma|32|Seoul
happykuma|23|Seoul
mykuma|23|Daejeon

Redirect output to logdata.txt in the current directory:

.output ./logdata.txt

Running the same SELECT now prints nothing to the terminal; its rows are stored in logdata.txt. Running .show also appends its output to that file, where the output entry shows ./logdata.txt.

Restore screen output

The setting lasts only for the current database connection. To restore terminal output during the session, run:

.output stdout

Subsequent SELECT statements and commands are displayed on screen again.