1

I have over a thousand files in a directory which I want to convert to text files. I use a code like the one below to first take out the spaces in the file names and then convert the files to text:

!/bin/bash

   find . -name '*.pdf' | while read file;
   do
    target=`echo "$file" | sed 's/ /_/g'`;
    echo "Renaming '$file' to '$target'";
    mv "$file" "$target";
    chmod 777 *.pdf;
    pdftotext -layout  "$target"  "$target.txt";
   done;

This code however converts a file like I love you.pdf to I_love_you.pdf.txt. I want to remove the .pdf part of the final file extension.

4

4 回答 4

1

我首选的方法是使用替换来修改扩展:

pdftotext -layout "$target" "${target/%.pdf/.txt}"

那里的%意思是只在字符串的末尾匹配。

于 2012-08-24T07:09:06.887 回答
0

使用' basename'例如。

basename "i_love_you.pdf" ".pdf" returns "i_love_you"

请参阅如何从 Bash 中的路径字符串中删除文件后缀和路径部分?

于 2012-08-24T07:16:53.240 回答
0

你的问题是这样的:

$target = "i_love_you.pdf"

所以

$target.txt = "i_love_you.pdf.txt"

请注意,如果您不向 提供第二个参数pdftotext,则默认情况下它将 file.pdf转换为file.txt,这似乎非常适合您的要求。

于 2012-08-24T07:11:47.973 回答
0

另一种选择可能是:

查找 ./ -name "*.pdf" -exec pdftotext {} \;

于 2016-09-07T13:43:28.913 回答