Dexter Naru
Dexter Naru

Reputation: 233

How to change filetype of all files within a directory in bash

I have a directory called letters an a bash script at the same level. What I want to do is change all docx files to txt. As a first step I tried to use the find command to list every docx file. My script is:

#!/bin/bash

letters_dir="$(cd "$(dirname "./letters")"; pwd)/$(basename "./letters")"

$files_docx=$(find $letters_dir -type f -iname "*.docx")

for $file in $files_docx; do
    echo $file
done

The error I'm getting is

./05.sh: line 5: =/media/gdisk/Documents: The file or directory does not exist ./05.sh: line 9: `$file': is not a valid identifier

I also tried passing the relative directory to the find command. Like this:

$files_docx=$(find letters -type f -iname "*.docx")

But I get the same result

Upvotes: 1

Views: 76

Answers (1)

ufopilot
ufopilot

Reputation: 3975

Using command rename on ubuntu

$ sudo apt install rename

rename 's/\.docx$/\.txt/' *.docx

Upvotes: 1

Related Questions