Skip to content

ls, lsl, lsd, lsf, lsjson: add --output-file flag - fixes #8358 - #10279

Open
hsdfat wants to merge 1 commit into
rclone:masterfrom
hsdfat:feat/ls-output-file-8358
Open

hsdfat wants to merge 1 commit into
rclone:masterfrom
hsdfat:feat/ls-output-file-8358

Conversation

@hsdfat

@hsdfat hsdfat commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

What does this change do?

Adds --output-file to ls, lsl, lsd, lsf and lsjson, so the listing can go to a file while --progress and the log keep the terminal. This is option 1 from the issue, which @ncw preferred over sending progress to stderr.

Today, redirecting stdout with -P captures the progress block as well, because the ls commands write through os.Stdout, which --progress intercepts (operations.SyncFprintf special-cases w == os.Stdout). A file passed to the same functions is written directly, so nothing new is needed in fs/operations.

  • cmd/ls/lshelp gets the flag and an Output() helper that returns os.Stdout or the created file, modelled on hashsum --output-file. It lives in lshelp because all five commands already import it for the shared help, which also gains a sentence about the flag. The name follows hashsum (and the issue) rather than tree --output.
  • ls, lsl, lsd and lsf pass the writer to the operations.List*/Lsf functions they already call with os.Stdout.
  • lsjson was the only one writing os.Stdout directly, with fmt.Print* for the brackets and separators. Its body moves into lsJSON(ctx, fsrc, remote, w io.Writer) and every write goes to w with the error checked. Output is unchanged: I compared lsjson -R and lsjson --stat from this branch and from master byte for byte on cmd/lsf/testfiles.

Without the flag all five commands still pass the real os.Stdout, so --progress interception behaves as before.

Testing

  • cmd/ls/lshelp/lshelp_test.go: Output() returns os.Stdout when the flag is empty, writes to and closes the file when set, and returns an error for a path that cannot be created.
  • cmd/lsjson/lsjson_test.go: lsJSON writes a parseable listing, and a --stat item, to the given writer, using cmd/lsf/testfiles; an empty directory gives exactly [\n]\n.
  • With the built binary on cmd/lsf/testfiles: ls, lsl, lsd, lsf, lsjson -R and lsjson --stat with --output-file leave stdout empty and write the listing to the file; lsjson -R --output-file x.json -P shows only the progress block on the terminal and x.json parses; a path in a missing directory fails with failed to create output file ....
  • go build ./..., go test -race on the five packages, and golangci-lint run (v2.15.0) on them all pass.

No change to docs/content/commands, which is generated at release time; the flag shows up in --help and the sentence in the shared help covers the usage.

Linked issue

Fixes #8358

Checklist

  • This change is trivial OR it has been discussed and agreed in the linked issue.
  • I have read the contribution guidelines.
  • (If I used AI tools to help write this code) I have read and understood the AI-assisted contributions guidance, and I have tested and take ownership of this change myself.
  • I have added tests for all changes in this PR if appropriate.
  • I have added documentation for the changes if appropriate.
  • All commit messages are in house style.
  • (Backend changes only) test_all passes for this backend and if submitting a new backend can provide a test account for the integration tester - see CONTRIBUTING.md.
  • This Pull Request is ready for review.

Write the listing to a file instead of standard output. Progress and
log output keep going to the terminal, so a listing can be captured
while watching --progress.

lsjson wrote to os.Stdout directly; it now writes to the chosen writer.
@hsdfat
hsdfat requested a review from ncw as a code owner November 7, 2026 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ability to see progress when redirecting STDOUT

1 participant