Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ls: Proposal to add -0 (zero) option for null terminated output #2836

Closed
kimono-koans opened this issue Dec 31, 2021 · 4 comments
Closed

ls: Proposal to add -0 (zero) option for null terminated output #2836

kimono-koans opened this issue Dec 31, 2021 · 4 comments
Labels

Comments

@kimono-koans
Copy link
Contributor

kimono-koans commented Dec 31, 2021

Unix allows newlines in filenames. A -0 (zero) option which would print the 'ls' results with NUL terminators instead of newlines

See: Why you shouldn't parse the output of ls @@https://archive.is/55aBh

And discussion: https://news.ycombinator.com/item?id=29747034

@tertsdiepraam
Copy link
Member

Interesting idea! My personal opinion is that we should follow GNU if they implement something like this, but not take the lead on it. I think that ls in general is just not made to be parsed and other tools are probably much better suited to being parsed. Nevertheless, something like this could indeed make parsing ls output at least viable. So either way is fine with me.

@rivy
Copy link
Member

rivy commented Jan 1, 2022

There is more discussion about this at Stack Overflow... Why not parse ls (and what to do instead)?.

Notably, that discussion includes an interesting way to use LS_COLORS to output arbitrary strings around filenames (including NULs), eg LS_COLORS='lc=\0:rc=:ec=\0\0:di=:ex=:fi=:' \ls -1 --color=always | cat -A which can be parsed with a regex like /\x0([^\x0])\x0\x0/gms or, alternatively LS_COLORS='lc=\0:rc=:ec=\0\0:di=:ex=:fi=:' \ls -1 --color=always | perl -0ne 'chop; print "`$_\n`" if /./'.

At the moment, it appears that the uutil ls doesn't interpret LS_COLORS as having internal character escape sequences. But, since the above LS_COLORS technique exists, I'd be more inclined to accept a fix for the LS_COLORS interpretation instead of a new, non-GNU option. On the other hand, lots of other utilities have added this to disambiguate filenames (find -print0, perl -0, sort -z, and xargs -0), and it would be a more direct solution to the filename disambiguation problem, so I wouldn't be against the option.

@Arcterus , @sylvestre , opinion?

@tertsdiepraam
Copy link
Member

tertsdiepraam commented Jan 27, 2022

Interesting development for this issue: GNU went ahead and implemented --zero for ls in coreutils 9, so we can implement this without having to worry about compatibility.

Here is the description from the docs:

‘--zero’
    Output a zero byte (ASCII NUL) at the end of each line, rather than a newline.
    This option enables other programs to parse the output even when that output
    would contain data with embedded newlines. This option is incompatible with
    the --dired (-D) option. This option also implies the options --show-control-chars,
    -1, --color=none, and --quoting-style=literal (-N).

Source: https://www.gnu.org/software/coreutils/manual/html_node/General-output-formatting.html

@tertsdiepraam
Copy link
Member

Closing in favour of #2929.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants