格式化目录中的所有XML文件并将其保存在子目录中

Ben*_*Ben 2 bash find xargs xmllint

我正在尝试编写一个脚本,它将查看目录,查找所有XML文件,运行它们xmllint,并将格式化结果保存到名为的子目录中的同名文件中formatted.这是我到目前为止的脚本:

find . -maxdepth 1 -type f -iname "*.xml" | xargs -I '{}' xmllint --format '{}' > formatted/'{}'
Run Code Online (Sandbox Code Playgroud)

这在某种程度上有效.子目录最终得到一个名为的文件,"{}"它只是通过处理的最终文件的结果xmllint.如何让文件正确写入子目录?

Lev*_*sky 6

{}您看到的名称文件可能应该包含所有格式化的文件.这样做的原因是您正在使用的重定向实际上不是命令的一部分xargs.重定向由shell解释,因此它的作用就是运行

find . -maxdepth 1 -type f -iname "*.xml" | xargs -I '{}' xmllint --format '{}'
Run Code Online (Sandbox Code Playgroud)

并将输出保存到名为的文件formatted/{}.

尝试使用--output选项xmllint而不是重定向:

... | xargs -I '{}' xmllint --format '{}' --output formatted/'{}'
Run Code Online (Sandbox Code Playgroud)

您还可以xargs使用以下-exec选项来避免调用find:

find . -maxdepth 1 -type f -iname "*.xml" -exec xmllint --format '{}' --output formatted/'{}' \;
Run Code Online (Sandbox Code Playgroud)