Operations 7 min read

Master Bulk File Renaming in Linux with the Powerful rename Command

This guide explains how to use the Linux rename command for batch renaming files, covering basic syntax, Perl‑style regular expressions, preview options like -v and -n, case conversion, and common pitfalls, enabling efficient and safe mass file renaming.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Bulk File Renaming in Linux with the Powerful rename Command

In Linux, the mv command works well for renaming a single file, but it becomes cumbersome when you need to rename many files at once. The rename command solves this problem by allowing batch renaming using Perl‑style regular expressions.

Basic Syntax

The general form is: $ rename 's/old/new/' target_files Here s tells rename to substitute the first string with the second one. For example, the following command changes this.old to this.new:

$ rename 's/old/new/' this.old
$ ls this*
this.new

Renaming Multiple Files

To rename all files ending with .old to .new in the current directory:

$ ls *.old
report.old  schedule.old  stats.old  this.old
$ rename 's/old/new/' *.old
$ ls *new
report.new  schedule.new  stats.new  this.new

The same approach works for changing any part of a filename, such as turning report.* into review.*:

$ rename 's/report/review/' *

Previewing Changes

Use the -v option to see each rename operation as it happens:

$ rename -v 's/123/124/' *
status.123 renamed as status.124
report123.txt renamed as report124.txt

For a dry‑run that only shows what would happen without actually renaming, add -n (or --nono):

$ rename -n 's/old/save/' *
rename(logger.man-old, logger.man.save)
rename(lyrics.txt-old, lyrics.txt.save)
...

After confirming the output, run the command again without -n to apply the changes.

Regular‑Expression Details

In the pattern, a dot . matches any character. To match a literal dot, escape it with a backslash ( \.). For example:

$ rename -n 's/\.old/\.save/' *

Case Conversion

Rename can also change case. The following command converts all filenames starting with an uppercase W to lowercase:

$ rename -n 'y/A-Z/a-z/' W*
rename(WARNING_SIGN.pdf, warning_sign.pdf)
rename(Will_Gardner_buttons.pdf, will_gardner_buttons.pdf)
...

Here -n previews the changes, and y performs the case translation.

Summary

For single‑file renaming, mv is sufficient. For batch operations, rename is more convenient. Always use the -n (dry‑run) or -v (verbose) options first to preview changes and avoid accidental renames.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

command lineregexrenamebatch renaming
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.