how to output file names surrounded with quotes in SINGLE line?

后端 未结 9 961
[愿得一人]
[愿得一人] 2021-02-03 19:47

I would like to output the list of items in a folder in the folowing way:

\"filename1\"  \"filename2\" \"file name with spaces\" \"foldername\" \"folder name wit         


        
9条回答
  •  失恋的感觉
    2021-02-03 20:38

    EDIT:
    The following answer generate a new-line separated LIST instead of a single line.

    1. I'm second guessing that the OP uses the result for invoking other command
    2. converting the output LIST to single line is easy (| tr '\n' ' ')

    A less mentioned method is to use -d (--delimiter) option of xargs:

    find . | xargs -I@ -d"\n" echo \"@\" 
    

    -I@ captures each find result as @ and then we echo-ed it with quotes

    With this you can invoke any commands just as you added quotes to the arguments.

    $ find . | xargs -d"\n" testcli.js
    [ "filename1",
      "filename2",
      "file name with spaces",
      "foldername",
      "folder name with spaces" ]
    

    See https://stackoverflow.com/a/33528111/665507

提交回复
热议问题