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

天大地大妈咪最大 提交于 2019-12-03 10:45:27

问题


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 with spaces"

In other words, item names must be in a single line, surrounded with quotes (single or double) and divided by spaces.

I know that

find . | xargs echo

prints output in a single line, but I do not know how to add quotes around each item name.

This code is part of a bsh script. The solution can therefore be a set of commands and use temporary files for storing intermediate output.

Thank you very much for any suggestion.

Cheers, Ana


回答1:


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' ' '



回答2:


You could also simply use find "-printf", as in :

find . -printf "\"%p\" " | xargs your_command

where:

%p = file-path

This will surround every found file-path with quotes and separate each item with a space. This avoids the use of multiple commands.




回答3:


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.




回答4:


Try this.

find . -exec echo -n '"{}" ' \;



回答5:


for f in *; do printf "'%s' " "$f"; done; echo

Or, thanks to Gordon Davisson:

printf "'%s' " *; echo

The trailing echo is simply to add a newline to the output.




回答6:


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




回答7:


    find . | sed "s|.*|\"&\"|"

Brief description:
We take result of .* pattern and put it into quotes.
Good source is sed.

Detail description:
Pattern: s/one/ONE/

  • s Substitute command.
  • / Delimiter.
    In my expression "|" is used instead of "/" to prevent "mountains" like in pattern s/.*/\"&\"/.
  • one Regular expression pattern search pattern.
  • ONE Replacement string.
  • .* Means any symbol that repeats unlimited number of times.
  • \" Means " itself.
  • & Special character that corresponds to the pattern found.



回答8:


try

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


来源:https://stackoverflow.com/questions/6041596/how-to-output-file-names-surrounded-with-quotes-in-single-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!