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

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

    You can use the GNU ls option --quoting-style to easily get what you are after. From the manual page:

    --quoting-style=WORD

    use quoting style WORD for entry names: literal, locale, shell, shell-always, shell-escape, shell-escape-always, c, escape

    For example, using the command ls --quoting-style=shell-escape-always, your output becomes:

    'filename1' 'filename2' 'file name with spaces' 'foldername' 'folder name with spaces'
    

    Using --quoting-style=c, you can reproduce your desired example exactly. However, if the output is going to be used by a shell script, you should use one of the forms that correctly escapes special characters, such as shell-escape-always.

提交回复
热议问题