Linux
linux
February 19, 20222 min read

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.

Batch 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

Batch 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

Batch renaming file extension is as easy as this! Happy Coding!

Share this blog
Tagged in :
linux
bash
Like what you read?
Subscribe to our Newsletter
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
About the Author
Satvik
Satvik
Entrepreneur
Satvik is a passionate developer turned Entrepreneur. He is fascinated by JavaScript, Operating System, Deep Learning, AR/VR. He has published several research papers and applied for patents in the field as well. Satvik is a speaker in conferences, meetups talking about Artificial Intelligence, JavaScript and related subjects. His goal is to solve complex problems that people face with automation. Related projects can be seen at - [Projects](/projects)
View all articles
Previous Article
Next Article
April 23, 20206 min read