SQLite Forum

Extract files from sqlar with a directory prepended
Login
> What do you mean? I don't see how to print the first `($dir || name)`.

The use of `--verbose` is documented in the help text:

```
$ sqlite3 -A --help
.archive ...             Manage SQL archives
   Each command must have exactly one of the following options:
     -c, --create               Create a new archive
     -u, --update               Add or update files with changed mtime
     -i, --insert               Like -u but always add even if unchanged
     -t, --list                 List contents of archive
     -x, --extract              Extract files from archive
   Optional arguments:
     -v, --verbose              Print each filename as it is processed
     -f FILE, --file FILE       Use archive FILE (default is current db)
     -a FILE, --append FILE     Open FILE using the apndvfs VFS
     -C DIR, --directory DIR    Read/extract files from directory DIR
     -n, --dryrun               Show the SQL that would have occurred
   Examples:
     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar
     .ar -tf ARCHIVE          # List members of ARCHIVE
     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE
   See also:
      http://sqlite.org/cli.html#sqlar_archive_support
```

So:

```sh
# Extract with long options
$ sqlite3 test.sqlar -A --extract --directory ~/tmp /etc/logrotate.conf
# nothing printed

$ ls -l ~/tmp/etc/logrotate.conf 
-rw-r--r-- 1 aho aho 533 Jan 21  2019 /home/aho/tmp/etc/logrotate.conf

# Let's clean up and try again in verbose mode, this time with short options
$ rm -f ~/tmp/etc/logrotate.conf

$ sqlite3 test.sqlar -AxvC ~/tmp /etc/logrotate.conf
/home/aho/tmp//etc/lo$ ls -l ~/tmp/etc/logrotate.conf 

$ ls -l ~/tmp/etc/logrotate.conf 
-rw-r--r-- 1 aho aho 533 Jan 21  2019 /home/aho/tmp/etc/logrotate.conf
```

> How to run the SQL commands in an sqlite3 session without using -Ax. I tried the following. But no files are written.
>
> `SELECT  ($dir || name), writefile(($dir || name), sqlar_uncompress(data, sz), mode, mtime) FROM sqlar WHERE (1) AND (data IS NULL OR $dirOnly = 0);`

`$dir` and `$dirOnly` are substituted internally in the SQLite shell. I've already explained the semantics of both parameters [in an earlier reply](https://sqlite.org/forum/forumpost/d034f7b1f3d1ad9a?t=h).

Here's the actual query issued by the above extract, as revealed by the `arExtractCommand()` source pointed to in my earlier reply, and confirmed via a `--dryrun` run:

```
$ rm -f ~/tmp/etc/logrotate.conf

$ sqlite test.sqlar

sqlite> SELECT
   ...> ('/home/aho/tmp/' || name),
   ...> writefile(('/home/aho/tmp/' || name), sqlar_uncompress(data, sz), mode, mtime)
   ...> FROM sqlar WHERE (name = '/etc/logrotate.conf' OR substr(name,1,20) = '/etc/logrotate.conf/') AND (data IS NULL OR 0 = 0)
   ...> AND name NOT GLOB '*..[/\]*';
/home/aho/tmp//etc/logrotate.conf|533

sqlite> .quit

$ ls -l ~/tmp/etc/logrotate.conf 
-rw-r--r-- 1 aho aho 533 Jan 21  2019 /home/aho/tmp/etc/logrotate.conf

$ sqlite3 test.sqlar -AxnC ~/tmp /etc/logrotate.conf
SELECT  ($dir || name), writefile(($dir || name), sqlar_uncompress(data, sz), mode, mtime) FROM sqlar WHERE ( name = '/etc/logrotate.conf' OR substr(name,1,20) = '/etc/logrotate.conf/') AND (data IS NULL OR $dirOnly = 0) AND name NOT GLOB '*..[/\]*'
SELECT  ($dir || name), writefile(($dir || name), sqlar_uncompress(data, sz), mode, mtime) FROM sqlar WHERE ( name = '/etc/logrotate.conf' OR substr(name,1,20) = '/etc/logrotate.conf/') AND (data IS NULL OR $dirOnly = 0) AND name NOT GLOB '*..[/\]*'

```