DebugPointer
Published on

Linux command to rename extensions of multiple files

Linux command to rename extensions of multiple files

To batch rename file extensions of multiple files in a folder, these set of commands or shell script can do the work.

Rename extensions of all .txt files to .md

# Rename all *.txt to *.md
for file in *.txt; do
    mv -- "$file" "${file%.txt}.md"
done

Here, .txt represents a file extension, it will be replaced with a new file extension .md.

*.txt is a globbing pattern, where we use a * as a wildcard to match any string, in this case, *.txt will match all file names that end with .txt extension.

-- avoids issues with filenames that start with hyphens. -- in linux is used to mark the end of options list.

${file%.txt} is a parameter expansion, where, the .txt extension is removed from the end, and the filename is retained in the variable.

A one-liner to the above command can be as simple as-

for f in *.txt; do mv -- "$f" "${f%.txt}.md"; done

Rename extensions of all .js files to .jsx

In case of renaming .js to .jsx files for your react.js project,

# Rename all *.js to *.jsx
for f in *.js; do
    mv -- "$f" "${f%.js}.jsx"
done

A one-liner to the above command can be as simple as-

for f in *.js; do mv -- "$f" "${f%.js}.jsx"; done

Rename extensions of all .doc files to .docx

Let's take an example of replacing .doc to .docx

# Rename all *.doc to *.docx
for f in *.doc; do
    mv -- "$f" "${f%.doc}.docx"
done

A one-liner to the above command can be as simple as-

for f in *.doc; do mv -- "$f" "${f%.doc}.docx"; done

It's as easy as this! Happy Coding!