Change All File Extensions Command Line
In this article
Yes, you can change all file extensions in a folder from the command line using a single command. On Windows, use ren *.txt *.md in Command Prompt; on macOS and Linux, use for f in *.txt; do mv "$f" "${f%.txt}.md"; done in Terminal. These commands rename every file with the specified old extension to the new one, though the exact syntax and safety considerations differ between systems.
Why Change File Extensions from the Command Line?
Graphical file managers let you rename files one at a time, but that becomes tedious when you have dozens or hundreds of files. The command line excels at batch operations because a single line can apply a consistent change to every matching file. You might need to change extensions when converting file formats (e.g., .jpeg to .jpg), fixing a typo in a batch of files, or preparing files for a script that expects a specific extension. Command-line renaming is also scriptable, meaning you can repeat it exactly on different folders or integrate it into larger automation workflows.
How the Commands Work
Both Windows and Unix-like systems use wildcards to match groups of files. The asterisk (*) matches any sequence of characters, so *.txt matches every file ending in .txt. The rename command then applies the new name pattern to each match.
- Windows (cmd):
ren *.txt *.md— therencommand takes two arguments: the pattern to match and the new name pattern. The*in the second argument carries over the original filename (without the old extension) and appends the new extension. - macOS/Linux (bash):
for f in *.txt; do mv "$f" "${f%.txt}.md"; done— this is a loop that iterates over each matching file, stores the name in the variablef, and renames it usingmv. The${f%.txt}syntax removes the.txtsuffix from the variable, then you add the new extension.
On Windows PowerShell, you can use Get-ChildItem *.txt | Rename-Item -NewName { $_.Name -replace '.txt$', '.md' } for more complex operations.
Choosing the Right Command for Your System
Your operating system determines which command is available and how it behaves. Windows Command Prompt uses ren (or rename), which is simple but has limitations—it cannot rename files across directories, and it does not support regular expressions. PowerShell is more powerful and available on all modern Windows systems. macOS and Linux use the Unix mv command, which is more flexible but requires a loop for batch operations. If you are on Windows but prefer Unix-style commands, you can install Windows Subsystem for Linux (WSL) or use Git Bash, which provides a bash environment.
Common Problems and How to Avoid Them
The most frequent issue is that renaming an extension does not change the file's actual format. A file named data.txt renamed to data.md is still plain text; the extension only tells the operating system which program to open it with. If you rename a .jpg to .png, it will not become a PNG image—it will likely fail to open properly. Another common problem is that the command may match unintended files if you use a broad wildcard. For example, ren *.* *.bak renames every file with a dot in its name, including report.final.docx to report.final.bak, which is probably not what you want. Always test with a small subset first, or use echo to preview the changes (on Unix, for f in *.txt; do echo mv "$f" "${f%.txt}.md"; done shows what would happen without actually renaming).
Comparing Windows, macOS, and Linux Approaches
Windows Command Prompt is the most straightforward for simple extension changes, but it lacks the ability to handle multiple extensions or complex patterns. PowerShell offers more control, such as filtering by size or date, and supports regular expressions. macOS and Linux bash loops are extremely flexible—you can add conditions, process files recursively, and combine with other commands like find. For example, to rename all .txt files in subdirectories, you would use find . -name '*.txt' -exec bash -c 'mv "$0" "${0%.txt}.md"' {} \;. The choice depends on your comfort level and the complexity of the task. If you are on Windows, PowerShell is recommended for anything beyond the simplest rename.
Safety and Backup Considerations
Renaming files is a destructive operation—there is no undo in the command line. If you make a mistake, you could lose track of your files or overwrite existing ones. For example, if you rename *.txt to .md and there are already files with .md extensions, the command may overwrite them (on Windows, ren will fail if the target exists; on Unix, mv will overwrite without warning). To stay safe, always back up your files before running a batch rename, or at least test on a copy. You can also use a version control system like Git to track changes. Another safety measure is to use a dry-run: on Unix, prefix the command with echo to see what it would do; on Windows PowerShell, use the -WhatIf parameter with Rename-Item.
Alternatives to the Command Line
If you are not comfortable with the command line, there are graphical tools that can batch rename files. On Windows, you can select multiple files in File Explorer, right-click, and choose "Rename"—Windows will rename them with a number suffix (e.g., file (1).txt), but this does not change extensions. For changing extensions, you can use a dedicated bulk rename utility like Advanced Renamer (free) or Ant Renamer. On macOS, you can use the built-in Finder: select files, right-click, and choose "Rename X Items," then select "Replace Text" or "Add Text" to change the extension. On Linux, tools like rename (Perl-based) or GPRename offer a graphical interface. These tools are often safer because they show a preview before applying changes.
Frequently Asked Questions
Will changing the extension corrupt my file?
No, changing the extension does not modify the file's content. It only changes the metadata that tells your operating system which program to use to open the file. If the new extension does not match the actual file format, the file may not open correctly in the associated program, but the data itself is untouched.
How do I change extensions for all files in subfolders?
On Windows Command Prompt, ren does not work recursively. Use PowerShell: Get-ChildItem -Recurse -Filter *.txt | Rename-Item -NewName { $_.Name -replace '.txt$', '.md' }. On macOS/Linux, use find . -name '*.txt' -exec bash -c 'mv "$0" "${0%.txt}.md"' {} \;.
Can I undo a batch rename?
There is no built-in undo for command-line renames. If you have a list of the original names, you can reverse the process by running the command with the extensions swapped. Otherwise, you need to restore from a backup or use a file recovery tool.
What if I want to change only files with a specific name pattern?
You can use more specific wildcards. For example, on Windows, ren report*.txt report*.md renames only files starting with "report". On Unix, use for f in report*.txt; do .... For more complex patterns, use PowerShell's -Filter or regular expressions.
Is there a way to rename extensions without overwriting existing files?
On Windows, ren will not overwrite existing files—it will show an error. On Unix, mv will overwrite by default. To avoid overwriting, add the -n flag (no-clobber) to mv: mv -n "$f" "${f%.txt}.md". In PowerShell, use -Force to overwrite or test for existence first.