Editing a single file
Create a file
Create a new file with a given content; the program fails when the file exists.
write "/home/user/app/notes.txt" "first\n" mode: create
/home/user/app/notes.txt
Overwrite a file
Replace the whole content of an existing file.
write "/home/user/app/notes.txt" "second\n" mode: overwrite
{"format":"nurt-fs-snapshot/1","entries":[{"path":"/home/user/app/notes.txt","type":"file","content_utf8":"first\n"}]}
/home/user/app/notes.txt
Create a file with several lines
Create a multi-line file without escaping quotation marks.
write "/home/user/app/Dockerfile" r#"FROM alpine:3.20
RUN apk add --no-cache curl
CMD ["sh"]
"# mode: create
/home/user/app/Dockerfile
cat > /home/user/app/Dockerfile <<'EOF'
FROM alpine:3.20
RUN apk add --no-cache curl
CMD ["sh"]
EOF
Prepend a line to a file
Add a header line at the start of a file.
write "/home/user/app/notes.txt" "# Notes\n" mode: prepend
{"format":"nurt-fs-snapshot/1","entries":[{"path":"/home/user/app/notes.txt","type":"file","content_utf8":"a\n"}]}
/home/user/app/notes.txt
Insert a line at a line number
Insert a comment before line 3 of a file.
insert "// updated" in: "/home/user/app/main.go" line: 3 position: before
{"format":"nurt-fs-snapshot/1","entries":[{"path":"/home/user/app/main.go","type":"file","content_utf8":"a\nb\nc\n"}]}
/home/user/app/main.go
Apply several changes as one transaction
Apply three changes to one file; when one of them does not apply, none is applied.
edit "/home/user/app/main.go" changes: [
{replace: "oldName(", with: "newName(", occurrence: 1},
{insert: "// updated", line: 3, position: before},
{delete_lines: 10..12}
] expect: 3
{"format":"nurt-fs-snapshot/1","entries":[{"path":"/home/user/app/main.go","type":"file","content_utf8":"oldName(1)\n"}]}
/home/user/app/main.go
Set a key in a JSON file
Set one key of a JSON file and keep the layout of the rest.
set "/home/user/app/package.json" key: "scripts.test" value: "vitest"
{"format":"nurt-fs-snapshot/1","entries":[{"path":"/home/user/app/package.json","type":"file","content_utf8":"{\n \"scripts\": {\n \"test\": \"jest\"\n }\n}\n"}]}
/home/user/app/package.json
jq '.scripts.test = "vitest"' package.json > p.tmp && mv p.tmp package.json
Set a key in a TOML file
Change a version in a TOML manifest and keep its comments.
set "/home/user/app/Cargo.toml" key: "package.version" value: "0.4.0"
{"format":"nurt-fs-snapshot/1","entries":[{"path":"/home/user/app/Cargo.toml","type":"file","content_utf8":"[package]\nversion = \"0.3.9\" # bump on release\n"}]}
/home/user/app/Cargo.toml
Set several keys at once
Set several keys of a YAML file in one operation.
set "/home/user/app/config.yaml" values: {"server.port": 8080, "server.debug": false}
{"format":"nurt-fs-snapshot/1","entries":[{"path":"/home/user/app/config.yaml","type":"file","content_utf8":"server:\n port: 80\n"}]}
/home/user/app/config.yaml
Read a key of a structured document
Read one value from a JSON document.
read "/home/user/app/package.json" key: "scripts.test"
{"format":"nurt-fs-snapshot/1","entries":[{"path":"/home/user/app/package.json","type":"file","content_utf8":"{\"scripts\":{\"test\":\"jest\"}}"}]}
jest
Query a document
Select the names of all dependencies of a JSON manifest.
read "/home/user/app/package.json" |> query "dependencies"
{"format":"nurt-fs-snapshot/1","entries":[{"path":"/home/user/app/package.json","type":"file","content_utf8":"{\"dependencies\":{\"left-pad\":\"1.0.0\"}}"}]}
left-pad
Run a query on a SQLite database
Read the active users of a SQLite database.
sql "/home/user/app/app.db" "select id, name from users where active = ? limit 5" params: [1]
id
sqlite3 /home/user/app/app.db "select id, name from users where active = 1 limit 5"
Write a file only when it did not change meanwhile
Replace a text only when the file still has the content that was read earlier.
replace "debug" "info" in: ["/home/user/app/app.conf"] if_match: "1a2b3c4d"
{"format":"nurt-fs-snapshot/1","entries":[{"path":"/home/user/app/app.conf","type":"file","content_utf8":"level = debug\n"}]}
E_STALE
Write a Windows registry value
Write a DWORD value to the registry of the current user.
write "registry:/HKCU/Software/Example/Mode" "3" value_type: dword mode: overwrite
registry:/HKCU/Software/Example/Mode
Set-ItemProperty HKCU:\Software\Example -Name Mode -Value 3 -Type DWord
Append a line to a log file
Append a line to the end of a file and keep its content.
write "/home/user/app/notes.txt" "done\n" mode: append ensure_newline: true
{"format":"nurt-fs-snapshot/1","entries":[{"path":"/home/user/app/notes.txt","type":"file","content_utf8":"start"}]}
/home/user/app/notes.txt
printf '\ndone\n' >> /home/user/app/notes.txt
Create a file with Windows line endings
Create a batch file with CRLF line endings.
write "/home/user/app/run.cmd" "@echo off\necho run\n" mode: create line_ending: crlf
/home/user/app/run.cmd
Create a file in a missing directory
Create a file together with the directories of its path.
write "/home/user/app/out/logs/today.txt" "start\n" mode: create parents: true
/home/user/app/out/logs/today.txt
mkdir -p /home/user/app/out/logs && printf 'start\n' > /home/user/app/out/logs/today.txt
Write a JSON file
Write a JSON file; a document that is not valid JSON is rejected.
write "/home/user/app/settings.json" r#"{"debug": false}"# mode: create format: json
/home/user/app/settings.json
Replace a text in one file
Replace one text in one file.
replace "localhost" "db.internal" in: ["/home/user/app/app.conf"]
{"format":"nurt-fs-snapshot/1","entries":[{"path":"/home/user/app/app.conf","type":"file","content_utf8":"host = localhost\n"}]}
/home/user/app/app.conf
sed -i 's/localhost/db.internal/g' /home/user/app/app.conf
Insert a line after the last line
Insert a line at the end of a file.
insert "exit 0" in: "/home/user/app/run.sh" position: end
{"format":"nurt-fs-snapshot/1","entries":[{"path":"/home/user/app/run.sh","type":"file","content_utf8":"echo run\n"}]}
/home/user/app/run.sh
Delete a range of lines
Delete lines 10 to 12 of a file.
edit "/home/user/app/main.go" changes: [{delete_lines: 10..12}]
{"format":"nurt-fs-snapshot/1","entries":[{"path":"/home/user/app/main.go","type":"file","content_utf8":"a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\n"}]}
/home/user/app/main.go
sed -i '10,12d' /home/user/app/main.go
Edit a file only when the content matches
Apply a change only when the file still has the content that was read earlier.
edit "/home/user/app/main.go" changes: [{replace: "oldName(", with: "newName("}] if_match: "1a2b3c4d"
{"format":"nurt-fs-snapshot/1","entries":[{"path":"/home/user/app/main.go","type":"file","content_utf8":"oldName(1)\n"}]}
E_STALE
Remove a key of a configuration file
Remove one key of a JSON file.
set "/home/user/app/package.json" key: "scripts.lint" value: null
{"format":"nurt-fs-snapshot/1","entries":[{"path":"/home/user/app/package.json","type":"file","content_utf8":"{\"scripts\":{\"lint\":\"eslint .\",\"test\":\"jest\"}}"}]}
/home/user/app/package.json
Set a value in an INI file
Set a value in an INI file and keep the other lines.
set "/home/user/app/app.ini" key: "database.host" value: "db.internal" format: ini
{"format":"nurt-fs-snapshot/1","entries":[{"path":"/home/user/app/app.ini","type":"file","content_utf8":"[database]\nhost = localhost\n"}]}
/home/user/app/app.ini
Set a variable of an environment file
Change a variable of a dotenv file.
set "/home/user/app/.env" key: "LOG_LEVEL" value: "debug" format: env
{"format":"nurt-fs-snapshot/1","entries":[{"path":"/home/user/app/.env","type":"file","content_utf8":"LOG_LEVEL=info\n"}]}
/home/user/app/.env
Read a key of a YAML file
Read one nested value of a YAML file.
read "/home/user/app/config.yaml" key: "server.port"
{"format":"nurt-fs-snapshot/1","entries":[{"path":"/home/user/app/config.yaml","type":"file","content_utf8":"server:\n port: 8080\n"}]}
8080
Read lines of a file
Read lines 5 to 15 of a file with their numbers.
read "/home/user/app/main.go" lines: 5..15 line_numbers: true
main.go
sed -n '5,15p' /home/user/app/main.go | cat -n
Read the last lines of a file
Read the last twenty lines of a log.
read "/home/user/app/app.log" last: 20
app.log
tail -n 20 /home/user/app/app.log
Write to a SQLite database
Insert a row into a SQLite database.
sql "/home/user/app/app.db" "insert into users (name, active) values (?, ?)" params: ["ann", 1]
/home/user/app/app.db