【问题标题】:preserve whitespace from find -printf output保留 find -printf 输出中的空格
【发布时间】:2016-02-09 05:01:34
【问题描述】:

我正在阅读文件列表并使用 -printf 输出它,我基本上是在创建一个由换行符分隔的字符串。当我尝试遍历此内容时,其中带有空格字符的文件名将呈现为新条目。

如何在保留文件名中的空格的同时阅读此内容?

loop() {
    files=$(find "${base}" -name "*" -printf "%TD%TT|%p\n")

    for string in $files
    do 
        stamp=${string%|*}
        file=${string#*|}

        echo "$stamp - $file"
    done
}

【问题讨论】:

  • 为什么不将您的 find 直接输入到 while 循环中。在此处搜索[bash] find while printf0 xargs。祝你好运。

标签: bash find printf


【解决方案1】:

由于 shellter 的评论,While 循环允许我将行分隔为变量:

loop() {
    while read string
    do
        stamp=${string%|*}
        file=${string#*|}

        files_new[$file]=$stamp
    done < <(find "${base}" -name "*" -printf "%TD%TT|%p\n")
}

【讨论】:

  • 试试while IFS=\| read -r file stamp; do files_new[$file]=$stamp;done &lt; &lt;(find "$base" -name \* -printf "%TD%TT|%p\n")
  • 不介意我问,但这有什么好处?
  • 不多,只是将stamp=${string%|*}file=${string#*|} 保存在for 循环体的开头。它也更具可读性。
  • ...我无法理解 "*" 作为 -name 的参数的含义。似乎是多余的。 :-)
  • 好的,很酷。请务必查看find-type 选项及其参数。例如,您可以选择仅通过传递 d 参数输出目录,仅通过传递 f 输出文件等等。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-03
  • 2015-06-25
  • 1970-01-01
  • 2014-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多