0

I will use ls output for pipe input, so I need to escape the file name. when I use GNU ls, It works well. what's the equivalent in BSD ls? I hoping the output is like this.

$ gls --quoting-style escape t*1
text\ 1  text1
lutaoact
  • 4,149
  • 6
  • 29
  • 41

2 Answers2

1

Why are/were you trying to use ls in a pipeline? You should probably be using find (maybe with -print0 and xargs -0, or -exec).

I suppose you could use ls -1f and then run the output through vis (or some similar filter) with some appropriate options to add the necessary quoting or escaping of your choice, but without knowing what you are feeding filenames into, and what (if any) other options you would want to use with ls, it's impossible to give much better guidance.

Greg A. Woods
  • 2,663
  • 29
  • 26
0

From the freebsd man page on ls there is no such option, however, you can try -m which will give you a comma separated streamed output:

-m       Stream output format; list files across the page, separated by
         commas.

I tried it on osx and it gave me:

$ ls -m
Hello World, Hello World.txt, foo.txt

That is a lot easier to parse from a script.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • 1
    The manual lies by omission -- the output from `-m` is _also_ line separated and it is not so easy to parse, though I guess it depends on exactly what scripting language one uses. I think you will find the normal newline-separated stream of names from `find`, or `ls -1`, is far easier to parse with the likes of `sh` and `awk` and similar. Whenever you start having to parse multiple lines each with multiple items you end up with two separators that cause you problems. With just newlines as the one and only separator it is far less likely that you'll have to worry about quoting. – Greg A. Woods May 20 '15 at 17:06