Hello there! If you’re like me, you probably find yourself working with numerous files of different types every day. Occasionally, we find ourselves needing to rename multiple files or even change their extensions. The task can be daunting, especially if done manually. But hey, that’s why we love Linux, right? It offers us the tools to make our work easier, and in this case, the rename
command comes to our rescue. Let’s delve into the details of how we can leverage this command to rename extensions of multiple files effortlessly.
To batch rename file extensions of multiple files in a folder, these set of commands or shell script can do the work.
Bulk 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
Bulk rename extensions of all .md files to .mdx
Let’s take an example of replacing .md
to .mdx
# Rename all *.doc to *.docx
for f in *.md; do
mv -- "$f" "${f%.doc}.mdx"
done
A one-liner to the above command can be as simple as-
for f in *.md; do mv -- "$f" "${f%.doc}.mdx"; done
Bulk 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
Bulk 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
So there you have it! Renaming multiple files extensions need not be a nightmare anymore. With the simple yet powerful rename
command, we’ve seen how Linux can simplify this process, turning a daunting task into a straightforward one. I hope this post has made your Linux journey a tad bit easier. As always, remember, the power of Linux is in its commands, and mastering them is the key to making the most of it. Keep exploring, keep learning, and till our next Linux adventure, happy coding!”