To rename files matching a pattern on Linux, use a shell loop with a filename glob and mv, or use a rename utility when you need regular-expression substitutions. For example, for f in *.jpeg; do mv -- "$f" "${f%.jpeg}.jpg"; done changes the extension of matching files while preserving their names.
What “pattern” means on Linux
A pattern can mean different things, and that distinction determines the right command. A shell glob such as *.log selects names ending in .log; photo_??.jpg selects names with exactly two characters in that position. The shell expands a glob into a list of matching filenames before running a command. It is a selector, not a rule for editing each filename.
A regular expression, by contrast, describes text to match and can specify what part of a name to replace. For example, a Perl-style expression can match a leading draft_ and change it to final_. The word rename does not identify one universal Linux program: different distributions may provide different implementations with different syntax. For a simple batch change, shell globbing plus mv is widely available and makes the selection and transformation easy to inspect.
Rename extensions with a shell loop
Bash parameter expansion can remove or change a suffix without calling a separate text-processing tool. To change every matching .jpeg extension to .jpg in the current directory, use:
for f in *.jpeg; do
mv -- "$f" "${f%.jpeg}.jpg"
done
The expression ${f%.jpeg} removes the shortest matching .jpeg suffix from the value of f. Appending .jpg constructs the destination name. The quotes keep spaces, tabs, and wildcard characters inside a filename from being treated as separate shell syntax. The -- marks the end of command options, so a filename beginning with a hyphen is not mistaken for an option by GNU mv.
In Bash, a glob with no matches is normally left unchanged. Without a check, the loop may try to rename a literal file named *.jpeg. Turn on nullglob so an unmatched pattern expands to no filenames:
shopt -s nullglob
for f in *.jpeg; do
mv -- "$f" "${f%.jpeg}.jpg"
done
This setting affects later globs in the current shell. You can also test that a candidate is a regular file with [[ -f $f ]] if the selection needs that restriction.
Change prefixes, suffixes, or parts of names
Shell parameter expansion is useful for predictable edits. To remove a prefix from files selected by that prefix, use:
for f in report_*.csv; do
mv -- "$f" "${f#report_}"
done
${f#report_} removes the shortest matching pattern from the start of the name. The related form ${f%_old} removes a matching pattern at the end. For example, to change a trailing _old to _archived while keeping the rest of each filename, construct the destination as ${f%_old}_archived, selecting only names that end in _old.
To replace a string in the middle of a name, Bash can use its substitution form. For example, ${f//draft/final} replaces every occurrence of draft in the variable. Use it inside a loop over a carefully chosen glob:
for f in *draft*.txt; do
mv -- "$f" "${f//draft/final}"
done
That expression replaces all occurrences, not just the first. If the old text might appear more than once and only one occurrence should change, Bash also supports a single-replacement form, ${f/draft/final}. Check the proposed names before running either form: a broad substitution may change more of a filename than intended.
Use a rename utility for regular expressions
A rename utility can make complex substitutions concise, but first identify which implementation is installed. On many Debian- and Ubuntu-based systems, rename is the Perl-based File::Rename tool. A typical command to change a leading prefix is:
rename -n 's/^img_/photo_/' img_*.jpg
In this Perl expression, s/old/new/ describes a substitution and ^ anchors the match at the start of each name. The -n option commonly means “do nothing; show what would be renamed” for this implementation. After reviewing the proposed changes, remove -n to perform them. Confirm the option in the installed program’s manual before relying on it.
Other systems provide a simpler utility, often called rename.ul or described in the util-linux manual, that uses a different argument form. A common form is rename old new files...; it is not interchangeable with a Perl expression. Use man rename, man rename.ul, or your package manager’s documentation to check the local syntax and options. If the command is missing, search your distribution’s package repository for the Perl-based rename tool or util-linux version that fits the task.
Rename files in subdirectories
A glob such as *.txt normally selects names in the current directory, not files at every depth below it. For recursive work, find can select files and pass their names safely to a loop. In Bash, a NUL-delimited pipeline avoids breaking filenames that contain spaces or newlines:
find . -type f -name '*.jpeg' -print0 |
while IFS= read -r -d '' f; do
new=${f%.jpeg}.jpg
mv -- "$f" "$new"
done
The quotes around '*.jpeg' are important: they prevent the shell from expanding the pattern before find receives it. -type f limits selection to regular files, while -print0 and Bash’s read -d '' use a NUL separator. Filenames cannot contain NUL bytes, so this is safe for unusual names too.
There is a practical Bash detail: commands on the right side of a pipeline usually run in a subshell. That does not matter for the example above, but it can matter if the loop updates a counter or other shell variable that you expect to use afterward. If you need that value in the current shell, use process substitution with Bash or write the loop in another structure. Test recursive commands on a small directory first, since a selection mistake can affect files throughout a tree.
Preview changes and avoid overwriting files
Renaming is a filesystem operation, so treat a batch command as a small migration. Before running it, check the selected files and the calculated destinations. A simple preview can print both names instead of moving anything:
for f in *.jpeg; do
printf '%s -> %s\n' "$f" "${f%.jpeg}.jpg"
done
For a Perl-based rename, use its documented dry-run option if available. A preview helps catch unexpected matches, but it does not guarantee that every move will succeed: another process could create a destination between the preview and the actual command.
Also look for collisions. If both scan.jpeg and scan.jpg exist, changing the first file’s extension targets a name that is already present. By default, GNU mv can replace an existing regular file. Options such as -i prompt before overwriting; GNU mv also offers -n to avoid overwriting, but its exact behavior and availability can vary. Neither option resolves a naming conflict automatically. Decide how to handle duplicates before running the batch, and keep a backup when the files are important.
Common mistakes and limitations
- Unquoted variables: Writing
mv $f $newcan split names at spaces and expand wildcard characters. Quote both variables. - Unmatched globs: Bash may pass an unmatched pattern through literally. Use
nullglobor another explicit no-match check. - Case sensitivity: On typical Linux filesystems,
.JPGand.jpgare different names. A pattern for one case does not necessarily select the other. - Directory selection: A broad glob can include directories as well as files. Use a suitable pattern or test each candidate if only regular files should change.
- Renaming order: A two-step scheme can fail if one destination is still another file’s source name. Plan for cycles or use temporary names when swapping names.
Linux filesystems generally allow many punctuation characters in filenames, including spaces and newlines, but not the slash character, which separates path components, or the NUL byte. Robust quoting and NUL-delimited traversal matter because filenames are not necessarily simple words. Avoid parsing ordinary ls output in scripts; its display is intended for people and can be ambiguous for unusual names.
Choosing the right method
Use a shell loop with mv when you need a small, readable change such as changing an extension, removing a known prefix, or substituting text. It relies on standard shell features and makes the transformation visible. Use a Perl-based rename tool when many files need more sophisticated regular-expression rules, such as matching a date format or changing only a substring at a particular position.
For a one-off operation with complicated conditions, a short Python script can also be appropriate: it can use regular expressions, inspect destination paths, and report conflicts before moving anything. That adds a dependency on Python and requires careful path handling, so it is not automatically simpler than a shell loop. Whichever method you choose, separate selection from transformation, preview the result, and check how collisions will be handled. Those steps matter more than choosing the shortest command.
Frequently Asked Questions
How do I rename all files with a certain extension in Linux?
In Bash, use a glob in a loop and build each destination with parameter expansion, such as for f in *.txt; do mv -- "$f" "${f%.txt}.md"; done. Consider enabling nullglob and previewing the output first.
Does Linux have one standard rename command?
No. Different distributions may install a Perl-based utility or a simpler implementation with different syntax. Read the local manual page and try its documented preview option rather than assuming examples for another system will work.
How can I rename files recursively?
Use find to select files below a directory, then process its NUL-delimited output. For example, combine find . -type f -name '*.txt' -print0 with a Bash loop using read -d ''. Quote the pattern so the shell does not expand it early.
Will a rename command overwrite an existing file?
It depends on the program and its options. GNU mv normally replaces an existing destination file unless an option or alias changes that behavior. Preview destinations, check for collisions, and consult the relevant manual before a large batch.
Can I use wildcards to rename part of a filename?
Wildcards such as * select filenames; they do not by themselves specify how to rewrite each name. Use shell parameter expansion in a loop or a rename utility’s substitution syntax to construct the new name.