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

后端 未结 9 960
[愿得一人]
[愿得一人] 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:34

    Try this.

    find . -exec echo -n '"{}" ' \;
    
    0 讨论(0)
  • 2021-02-03 20:35

    this should work

    find $PWD | sed 's/^/"/g' | sed 's/$/"/g' | tr '\n' ' '
    

    EDIT:

    This should be more efficient than the previous one.

    find $PWD | sed -e 's/^/"/g' -e 's/$/"/g' | tr '\n' ' '
    

    @Timofey's solution would work with a tr in the end, and should be the most efficient.

    find $PWD -exec echo -n '"{}" ' \; | tr '\n' ' '
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题