title: rledger split description: Split a ledger file into one file per year or month
rledger split
Split a beancount file into one file per year or month, plus a root file of include lines. The ledger is loaded before and after, and nothing is written unless the two load identically.
Usage
rledger split --by <year|month> [OPTIONS] [FILE]Arguments
| Argument | Description |
|---|---|
FILE | The file to split (default: the config's default file). It becomes the root. |
Options
| Option | Description |
|---|---|
--by <year|month> | Period each new file covers (required) |
--dir <DIR> | Directory for the period files (default: a directory named after FILE, next to it) |
--ledger <ROOT> | The ledger root FILE belongs to, when FILE is itself included by another file. The equivalence check loads this root. |
-n, --dry-run | Show the plan and run the equivalence check, but write nothing |
-P, --profile <PROFILE> | Use a profile from config |
Example
rledger split --by year ledger.beancountPlan: split ledger.beancount by year
create ledger/2023.beancount (412 dated directives)
create ledger/2024.beancount (388 dated directives)
rewrite ledger.beancount (keeps 4 option/plugin/include lines; adds 2 includes)
Equivalence check passed: 812 directives, 0 errors and warnings, options and plugins unchanged.
Backup of the original files: .rledger-backup/20241011T120000Z-split
Done; the ledger was reloaded from disk and matches.ledger.beancount now holds its options, plugins and existing includes, and:
include "ledger/2023.beancount"
include "ledger/2024.beancount"What goes where
- Dated directives (transactions,
open,balance,price, ...) go to the file for their year or month, in their original order. - Comments move with the directive they precede, copied verbatim. Nothing is reformatted; the only lines
splitwrites itself are the newincludelines and thepushtag/poptaglines described below, and it drops blank lines at the very top of each new file. The file's header (the comments above the first directive, up to the last blank line) and anything after the last directive stay in the root. option,pluginandincludestay in the root. The newincludelines go in front of anyincludethe file already had.pushtag/pushmetaapply per file, so a span that crosses a period boundary is closed at the end of one file and re-opened at the top of the next. Every directive keeps exactly the tags and metadata it had.
The equivalence check
Before writing, split loads the ledger as it is and as it would be, with plugins, booking and validation, and compares:
- the processed directive stream, in booking order;
- options and plugins;
- every error and warning, and the directive it is attached to;
- where each
documentpath resolves to; - realized capital gains and failing balance assertions.
If anything differs, split prints the first difference and exits with status 1 without writing anything. The current ledger is loaded twice, and if the two loads differ (a plugin that emits entries in a varying order, for example) the command refuses too, since no comparison could verify such a ledger. Such a ledger can still agree with itself by chance; the check after writing is the backstop. After writing, it loads the ledger from disk once more and restores the backup if that load disagrees.
A common refusal is a relative document path: it resolves against the directory of the file that contains it, so moving it into ledger/ would point it elsewhere. Use --dir . to keep the period files beside the root.
Why only by date
Same-date directives book in the order their files are loaded. Splitting by date keeps every date in one file, in its original order, so which lot a sale matches cannot change. Splitting by account would send same-date transactions to different files and could change it, so it is not offered.
Writing and backups
The original is first copied to .rledger-backup/<timestamp>-split/, keeping its group and permissions, with a manifest.txt listing every file saved and created. The new files are then written next to their targets under temporary names and moved into place, the root last. Until the root is replaced the old ledger is intact, and once it is the new one is complete. If anything fails before that point, nothing but the backup is left on disk. split refuses to overwrite a period file that already exists (a dangling symlink counts).
The ledger is loaded once more as soon as the backup exists. The backup sits in FILE's directory, so an include pattern that matches it (include "years/*" when FILE is in years/) would read it as part of the ledger; that is refused and the backup removed, before anything else is touched. Right before the first file is moved into place, every original is compared with what was checked, so an edit made in the meantime is refused rather than overwritten. A run that is killed partway leaves the originals and the complete backup; it can also leave temporaries named .<name>.rledger-tmp-... and new period files, which the manifest lists as created and which are safe to delete.
Files that are encrypted, not valid UTF-8, or have parse errors are refused. So is a FILE that is a symlink to a file in another directory: rledger, beancount and fava resolve its includes next to the link, but split works on the file the link points to and would write the period files next to that file, where the ledger opened through the link does not look. Split the file it points to if nothing opens the ledger through the link.
From an agent
ag-rledger split takes the same flags and needs --dry-run or --yes: without either it refuses with CONFIRMATION_REQUIRED, since it rewrites ledger files. Its JSON envelope carries the plan, the equivalence check's verdict, the written paths and the backup directory in result, and each refusal as a coded error (SYMLINK_ELSEWHERE, TARGET_EXISTS, EQUIVALENCE_FAILED, ...) with what the run got to in data.
ag-rledger split ledger.beancount --by year --dry-runExit Codes
| Code | Meaning |
|---|---|
| 0 | Split written (or, with --dry-run, would be) |
| 1 | The equivalence check refused; nothing was written |
| 2 | Error (file not found, parse errors, a target already exists, ...) |