41

我想以下列方式输出文件夹中的项目列表:

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

换句话说,项目名称必须在单行中,用引号(单引号或双引号)括起来并用空格分隔。

我知道

find . | xargs echo

在一行中打印输出,但我不知道如何在每个项目名称周围添加引号。

此代码是 bsh 脚本的一部分。因此,解决方案可以是一组命令并使用临时文件来存储中间输出。

非常感谢您的任何建议。

干杯,安娜

4

10 回答 10

53

您也可以简单地使用 find "-printf",如下所示:

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

在哪里:

%p = file-path

这将用引号包围每个找到的文件路径,并用空格分隔每个项目。这避免了使用多个命令。

于 2013-02-28T14:15:26.907 回答
31

这应该工作

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

编辑:

这应该比上一个更有效率。

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

@Timofey 的解决方案最终将与 tr 一起使用,并且应该是最有效的。

find $PWD -exec echo -n '"{}" ' \; | tr '\n' ' '
于 2011-05-18T08:14:44.597 回答
20

您可以使用 GNUls选项--quoting-style轻松获得所需的内容。从手册页:

--quoting-style=WORD

WORD 对条目名称使用引用样式 : literal, locale, shell, shell-always, shell-escape, shell-escape-always, c,escape

例如,使用 command ls --quoting-style=shell-escape-always,您的输出变为:

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

使用--quoting-style=c,您可以准确地重现您想要的示例。但是,如果输出将由 shell 脚本使用,您应该使用正确转义特殊字符的形式之一,例如shell-escape-always.

于 2016-02-17T08:47:13.400 回答
17

试试这个。

find . -exec echo -n '"{}" ' \;
于 2011-05-18T08:14:06.837 回答
4
for f in *; do printf "'%s' " "$f"; done; echo

或者,感谢戈登戴维森

printf "'%s' " *; echo

尾随echo只是在输出中添加换行符。

于 2011-05-18T10:40:01.893 回答
3

10 年后,没有人建议 Bash “|while read” 方法?

find * -type d -depth 0|while read f; do echo \"$f\"; done

这是一个简单的 Bash shell 管道,而不是启动另一个程序,如 sed 或 xarg。如果你真的想对每个文件/文件夹做一些事情:

find * -type d -depth 0|while read f; do du -sh "$f"; done

顺便说一句,find *使用另一个排除 .xyz 文件/文件夹并且不会输出./前缀的 Bash 功能find .

于 2021-05-01T18:18:43.350 回答
1

编辑:
以下答案生成一个换行符分隔的 LIST而不是单行。

  1. 我第二次猜测 OP 使用结果来调用其他命令
  2. 将输出 LIST 转换为单行很容易 ( | tr '\n' ' ')

一个较少提及的方法是使用-d( --delimiter) 选项xargs

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

-I@捕获每个find结果@,然后我们用引号回显它

有了这个,您可以调用任何命令,就像您在参数中添加引号一样。

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

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

于 2016-01-04T02:28:14.180 回答
1

尝试

ls | sed -e 's/^/"/g' -e 's/$/"/g' | tr '\n' ' '
于 2018-11-29T20:23:30.667 回答
1
    find . | sed "s|.*|\"&\"|"

简要说明:
我们将 .* 模式的结果放入引号中。
好的来源是sed

详细说明:
图案:s/one/ONE/

  • 替代命令。
  • /分隔符。
    在我的表达中“|” 用于代替“/”来防止像模式 s/.*/\"&\"/ 中的“山”。
  • 一种正则表达式模式搜索模式。
  • 一个替换字符串。
  • .*表示任何重复无限次的符号。
  • \"表示"本身。
  • &对应于找到的模式的特殊字符。
于 2019-02-26T12:12:13.837 回答
-1

为了避免试图手动添加引号的黑客行为,您可以利用printfs%q格式化选项:

❯ ll .zshrc.d
total 112K
-rwxrwxrwx 1 root root  378 Jul  1 04:39  options.zsh*
-rwxrwxrwx 1 root root   57 Jul  1 04:39  history.zsh*
-rwxrwxrwx 1 root root  301 Jul  1 05:01  zinit.zsh*
-rwxrwxrwx 1 root root  79K Jul  1 05:19  p10k.zsh*
-rwxrwxrwx 1 root root  345 Jul  1 05:24  zplugins.zsh*
-rwxrwxrwx 1 root root  490 Jul  4 23:40  aliases.zsh*
-rw-r--r-- 1 root root 9.0K Jul 27 08:14  lscolors.zsh
-rw-r--r-- 1 root root    0 Aug 30 05:56 'foo bar'
-rw-r--r-- 1 root root    0 Aug 30 05:58 '"foo"'

❯ find .zshrc.d -exec printf '%q ' {} +
.zshrc.d .zshrc.d/history.zsh .zshrc.d/aliases.zsh .zshrc.d/zplugins.zsh .zshrc.d/p10k.zsh .zshrc.d/zinit.zsh .zshrc.d/options.zsh '.zshrc.d/"foo"' '.zshrc.d/foo bar' .zshrc.d/lscolors.zsh #

与:

find .zshrc.d -printf "\"%p\" "
".zshrc.d" ".zshrc.d/history.zsh" ".zshrc.d/aliases.zsh" ".zshrc.d/zplugins.zsh" ".zshrc.d/p10k.zsh" ".zshrc.d/zinit.zsh" ".zshrc.d/options.zsh" ".zshrc.d/"foo"" ".zshrc.d/foo bar" ".zshrc.d/lscolors.zsh" #

请注意文件.zshrc.d/"foo"被错误地转义。

❯ echo ".zshrc.d/"foo""
.zshrc.d/foo

❯ echo '.zshrc.d/"foo"'
.zshrc.d/"foo"
于 2020-08-30T06:01:01.097 回答