The script below will find all .docx
and .rtf
files in the current directory and rename them to first_few_words.rtf
or docx
. First you need to install catdoc
. On Debian and its derivatives you can do that with
sudo apt-get install catdoc
Oddly enough, catdoc
gives me a segfault when I try it on a docx
I have, so use docx2txt
for the doc files instead:
sudo apt-get install docx2txt
Once you have catdoc
and docx
installed, cd
to the directory that contains your files and run this:
for file in *rtf; do
name=$(catdoc "$file" | grep . | head -1 | sed 's/ /_/g') &&
mv "$file" "$name".rtf;
done
for file in *docx; do
name=$(docx2txt < "$file" | grep . | head -1 | sed 's/ /_/g') &&
mv "$file" "$name".docx;
done
CAUTION: This will delete the original files, make sure you make a back up first, just in case.
EXPLANATION: both utilities convert docx
or rtf
to text, I then select the first non empty line of that text, convert spaces to underscores (it will make your life easier in the long run) and rename the files accordingly.