我想对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
如何使用find
或xargs
对-exec
执行此操作?
这是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