0

I check if there's some files of a certain kind inside a folder to lowercase the extension and then extract the content this way:

existDoc=""$(ls | grep .DOC | wc -l)

if [ $existDoc -gt 0 ]; then
    for file in *.DOC
    do
        mv $file $(basename "$file" .DOC)".doc"
    done
fi

and then conversion

for word in *.doc
    do
        text_doc=""$(basename "$word" .doc)
        sudo catdoc $word > $text_doc".txt"
    done

The issue is that a new empty file is created named "*.doc.txt" with no apparent reason.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
jomaweb
  • 531
  • you used $text_doc and $textdoc in your question. is this a typo? – michas Jul 29 '16 at 15:01
  • Use set -x to see what is going on. – michas Jul 29 '16 at 15:07
  • You can rename the files with rename "s/.DOC$/.doc/" *. Use rename -n "s/.DOC$/.doc/" * to check the result before. – Marco Jul 29 '16 at 16:01
  • you could simplify your "for file in .DOC" segment by just removing the "if" from the outside -- if there are no .DOC files, the loop won't execute anything. Just like your second segment does with *.doc files. – Jeff Schaller Jul 29 '16 at 16:42
  • since you've tagged bash, see also: mv "$file" "${file,,}" – Jeff Schaller Jul 29 '16 at 16:44
  • ikkachu , sometimes ".doc" sometimes, ".doc.txt" , so both appeared depending on the original files – jomaweb Jul 31 '16 at 11:47
  • Can't use "rename" Marco because when I tried it the docX files were renamed to doc and catdoc crashed with docX files . Maybe my fault. Will try with your code snippet – jomaweb Jul 31 '16 at 11:49

1 Answers1

5

A couple things:

If I understand you correctly, you would like to lowercase the extension of all *.DOC filenames and use catdoc to create text files of them.

shopt -s nullglob
for doc in ./*.DOC; do
    new_doc="${doc%.DOC}.doc"
    txt_doc="${doc%.DOC}.txt"

    catdoc "$doc" >"$txt_doc"
    mv "$doc" "$new_doc"
done

Or even shorter:

shopt -s nullglob
for doc in ./*.DOC; do
    catdoc "$doc" >"${doc%.DOC}.txt"
    mv "$doc" "${doc%.DOC}.doc"
done
  • The ${doc%.DOC} is using the ${parameter%word} parameter expansion of bash (or any POSIX shell) to remove the .DOC suffix from the filename in $doc.
  • Setting the nullglob shell option will ensure that nothing is matched by *.DOC if there are no files with the .DOC suffix. If not set, I would get the string *.DOC in $doc if there were no .DOC files.
  • Use a ./ prefix in ./*.DOC to avoid problems with filenames starting with -.
Kusalananda
  • 333,661
  • 1
    sudo is used because the destination folder is owned by root. Otherwise the files would not be written Kusalananda – jomaweb Jul 31 '16 at 11:56
  • @jomaweb In that case you either need sudo for the mv as well, or to run the whole thing with sudo in a script. I would personally do all the processing first, and then move the files in place with sudo mv. – Kusalananda Jul 31 '16 at 11:59
  • using for doc in *.DOC gives me an error when no DOC file is present – jomaweb Jul 31 '16 at 12:03
  • @jomaweb This is probably because you did not set shopt -s nullglob? – Kusalananda Jul 31 '16 at 12:36