Unix查找传递文件名到Exec并在输出重定向中

问题描述 投票:0回答:1

我想对base64编码的图像目录进行编码,以除去base64输出中的换行符。然后将其另存为<imageName>.base64.txt

我可以使用以下for循环执行此操作:

  for file in $(find path/to/images -name "*.png"); do
    base64 "$file" | tr -d "\n" > "$file".base64.txt
  done

如何使用findxargs-exec执行此操作?

unix find pipe io-redirection
1个回答
0
投票

这是How do I include a pipe | in my linux find -exec command?的非常近似,但不包括您要处理的情况。

要获取文件名,您可以运行|循环

-exec sh -c

find path/to/images -name "*.png" -exec sh -c ' for file; do base64 "$file" | tr -d "\n" > "${file}.base64.txt" done' _ {} + find -exec一起使用,会将所有从+的搜索结果一次性拍摄到find内部的小脚本中。 sh -c '..'表示调用一个显式外壳程序以运行_内部定义的脚本,并以文件名列表作为参数。

使用'..'的替代版本,与上面的循环同样昂贵,下面将这样做。但是此版本使用xargs字符NUL分隔文件名,并且-print0以相同字符(两个GNU特定标志)回读它。

xargs    
© www.soinside.com 2019 - 2024. All rights reserved.