# Import & Export
VFS provides commands to bridge between the virtual filesystem and the real filesystem, plus the ability to run external commands on virtual files.
## Import Files
Import files from the real filesystem into a vault.
### Basic Import
```bash
avfs import <real_path> <vfs_path>
```
**Examples:**
```bash
# Import a single file
avfs import ~/documents/report.pdf /docs/report.pdf
# Import to current directory
avfs import ~/data.csv /data.csv
# Import with different name
avfs import ~/old-name.txt /new-name.txt
```
### Recursive Import
Import entire directories:
```bash
avfs import -r ~/project /project
```
**Options:**
| `-r, --recursive` | Import directories recursively |
| `--max-depth <N>` | Limit recursion depth |
**Examples:**
```bash
# Import directory tree
avfs import -r ~/src /src
# Limit depth
avfs import -r --max-depth 2 ~/project /project
```
## Export Files
Export files from a vault to the real filesystem.
### Basic Export
```bash
avfs export <vfs_path> <real_path>
```
**Examples:**
```bash
# Export a single file
avfs export /docs/report.pdf ~/downloads/report.pdf
# Export to current directory
avfs export /config.yaml ./config.yaml
```
### Recursive Export
Export entire directories:
```bash
avfs export -r /project ~/exported-project
```
**Options:**
| `-r, --recursive` | Export directories recursively |
| `--max-depth <N>` | Limit recursion depth |
| `--overwrite` | Overwrite existing files |
**Examples:**
```bash
# Export with overwrite
avfs export -r --overwrite /backup ~/backup
# Export specific version
avfs export --version 3 /docs/readme.txt ~/old-readme.txt
```
## External Commands (exec)
Run external bash commands on virtual files.
### How It Works
1. File is extracted to a secure temp directory
2. Command runs with the temp file
3. Modified file is re-imported (creating a new version)
4. Temp file is deleted
### Basic Usage
```bash
avfs exec '<COMMAND>' <PATH>
```
**Examples:**
```bash
# In-place text replacement with sed
avfs exec 'sed -i s/foo/bar/g' /docs/file.txt
# Format JSON with jq
avfs exec 'jq .' /config/settings.json
# Sort lines in a file
avfs exec 'sort -o {} {}' /data/names.txt
```
The `{}` placeholder is replaced with the temp file path.
### Options
| `--reimport` | Re-import the modified file (default) |
| `--stdin` | Pass file content via stdin |
### No Re-import
Run command without modifying the virtual file:
```bash
# Just view the output
avfs exec 'wc -l' /data/file.txt
```
## Pipe-Based Operations
For streaming data, use pipes with `cat` and `write`:
### Syntax
```bash
### Examples
```bash
# Sort and deduplicate
# Filter log lines
# Transform JSON
# Compress content
### Reading from External Sources
```bash
# Pipe from real filesystem
curl -s https://api.example.com/data | avfs write /api/response.json
# Pipe from any command
### Writing to External Destinations
```bash
# View in pager
# Send to clipboard (Linux)
# Convert with pandoc
## exec vs Pipe Comparison
| In-place editing | Yes | Yes |
| Multiple files | With globs | One at a time |
| Temp file created | Yes | No |
| Streaming support | No | Yes |
| Large files | Memory-limited | Stream-friendly |
### When to Use exec
- Commands that require file paths (not stdin/stdout)
- Commands that modify files in-place (like `sed -i`)
- When you need automatic versioning on modification
### When to Use Pipes
- Streaming large files
- Complex multi-stage pipelines
- Commands designed for stdin/stdout
## Common Workflows
### Backup to Real Filesystem
```bash
# Export entire vault
avfs export -r / ~/vault-backup-$(date +%Y%m%d)
```
### Import Project Files
```bash
# Import a project
avfs import -r ~/code/myproject /myproject
# Verify
avfs tree /myproject
```
### Process Files with External Tools
```bash
# Format all JSON files
for file in $(avfs find / -n "*.json"); do
avfs exec 'jq .' "$file"
done
# Or using cat/write for complex pipelines
sort -t',' -k2 | \
head -100 | \
avfs write /data-processed.csv
```