bash 如何在单行中输出用引号括起来的文件名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6041596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to output file names surrounded with quotes in SINGLE line?
提问by Ana
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.
此代码是 bsh 脚本的一部分。因此,解决方案可以是一组命令并使用临时文件来存储中间输出。
Thank you very much for any suggestion.
非常感谢您的任何建议。
Cheers, Ana
干杯,安娜
采纳答案by freethinker
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.
@Timofey 的解决方案最终将与 tr 一起使用,并且应该是最有效的。
find $PWD -exec echo -n '"{}" ' \; | tr '\n' ' '
回答by Benjamin A.
You could also simply use find "-printf", as in :
您也可以简单地使用 find "-printf",如:
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.
这将用引号包围每个找到的文件路径,并用空格分隔每个项目。这避免了使用多个命令。
回答by George Hilliard
You can use the GNU ls
option --quoting-style
to easily get what you are after. From the manual page:
您可以使用 GNUls
选项--quoting-style
轻松获得您想要的东西。从手册页:
--quoting-style=WORD
use quoting style
WORD
for entry names:literal
,locale
,shell
,shell-always
,shell-escape
,shell-escape-always
,c
,escape
--quoting-style=WORD
WORD
对条目名称使用引用样式 :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:
例如,使用命令ls --quoting-style=shell-escape-always
,您的输出变为:
'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
.
使用--quoting-style=c
,您可以准确地重现您想要的示例。但是,如果输出将被 shell 脚本使用,您应该使用正确转义特殊字符的形式之一,例如shell-escape-always
.
回答by Timofey Stolbov
Try this.
尝试这个。
find . -exec echo -n '"{}" ' \;
回答by glenn Hymanman
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.
尾随echo
只是在输出中添加换行符。
回答by leesei
EDIT:
The following answer generate a new-line separated LISTinstead of a single line.
编辑:
以下答案生成一个换行分隔的 LIST而不是单行。
- I'm second guessing that the OP uses the result for invoking other command
- converting the output LIST to single line is easy (
| tr '\n' ' '
)
- 我再次猜测 OP 使用结果来调用其他命令
- 将输出 LIST 转换为单行很容易 (
| tr '\n' ' '
)
A less mentioned method is to use -d
(--delimiter
) option of xargs
:
一个较少提及的方法是使用-d
( --delimiter
) 选项xargs
:
find . | xargs -I@ -d"\n" echo \"@\"
-I@
captures eachfind
result as@
and then we echo-ed it with quotes
-I@
捕获每个find
结果@
,然后我们用引号对其进行回显
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" ]
回答by Ferroao
try
尝试
ls | sed -e 's/^/"/g' -e 's/$/"/g' | tr '\n' ' '
回答by iosuser2332
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/
简要说明:
我们将 .* 模式的结果放入引号中。
好的来源是sed。
详细说明:
图案:s/one/ONE/
- sSubstitute command.
- /Delimiter.
In my expression "|"is used instead of "/"to prevent "mountains" like in pattern s/.*/\"&\"/. - oneRegular expression pattern search pattern.
- ONEReplacement string.
- .*Means any symbol that repeats unlimited number of times.
- \"Means "itself.
- &Special character that corresponds to the pattern found.
- ■替换命令。
- /分隔符。
在我的表达“|”中 用于代替“/”以防止“山”,就像模式 s/.*/\"&\"/ 中一样。 - 一种正则表达式模式搜索模式。
- ONE替换字符串。
- .*表示无限次重复的任何符号。
- \"意味着"本身。
- & 与找到的模式相对应的特殊字符。