我使用find从目录及其子目录返回文件列表:
find $directory -type f -name "*.epub"然后,我想使用一个命令,该命令要求指定输入和输出文件名:
ebook-convert input.epub output.txt我希望将每个.epub转换为.txt,以便将输出存储在与输入相同的目录/子目录中。简单的管道不起作用。
find $directory -type f -name "*.epub" | ebook-convert我该怎么做?
发布于 2019-08-19 09:20:08
诀窍是不要创建一个文件列表,然后再迭代(参见为什么循环查找的输出错误的做法?)。
find "$directory" -type f -name '*.epub' -exec ebook-convert {} output.txt \;这将在*.epub目录中或在$directory目录下查找其名称与D3匹配的所有常规文件。对于每个参数,执行ebook-convert命令时都使用查找文件的路径名作为第一个参数,output.txt作为第二个参数。
这显然会覆盖每个找到的文件的output.txt,但下面的方法是创建一个与原始文件同名的文件,并将-converted.txt添加到名称的末尾(与原始文件位于同一个目录中):
find "$directory" -type f -name '*.epub' -exec ebook-convert {} {}-converted.txt \;这可能不适用于find的所有实现,因为它可能不会用找到的文件的路径名替换第二个{} (因为它与另一个字符串连接;但是,例如,GNU find处理它)。为了解决这个问题:
find "$directory" -type f -name '*.epub' -exec sh -c '
for pathname do
ebook-convert "$pathname" "$pathname"-converted.txt
done' sh {} +使用shell (如bash或zsh )支持**全局模式:
for pathname in "$directory"/**/*.epub; do
ebook-convert "$pathname" "$pathname"-converted.txt
done(这需要shopt -s globstar在bash中,并且将处理任何匹配的名称,而不仅仅是常规文件,除非在zsh中使用*.epub(.),或者在bash中使用显式-f测试)
https://unix.stackexchange.com/questions/536231
复制相似问题