To rename a group of files with a single command, use the rename command. It requires the use of regular expressions and can tell you what changes will be made before making them. Credit: Manchester City Library For decades, Linux users have been renaming files with the mv command. It’s easy, and the command does just what you expect. Yet sometimes you need to rename a large group of files. When that is the case, the rename command can make the task a lot easier. It just requires a little finesse with regular expressions. Unlike the mv command, rename isn’t going to allow you to simply specify the old and new names. Instead, it uses a regular expression like those you’d use with Perl. In the example below, the “s” specifies that we’re substituting the second string (old) for the first, thus changing this.new to this.old. $ rename 's/new/old/' this.new $ ls this* this.old A change as simple as that would be easier using mv this.new this.old, but change the literal string “this” to the wild card “*” and you would rename all of your *.new files to *.old files with a single command: $ ls *.new report.new schedule.new stats.new this.new $ rename 's/new/old/' *.new $ ls *.old report.old schedule.old stats.old this.old As you might expect, the rename command isn’t restricted to changing file extensions. If you needed to change files named “report.*” to “review.*”, you could manage that with a command like this: $ rename 's/report/review/' * The strings supplied in the regular expressions can make changes to any portion of a file name — whether file names or extensions. $ rename 's/123/124/' * $ ls *124* status.124 report124.txt If you add the -v option to a rename command, the command will provide some feedback so that you can see the changes you made, maybe including any you didn’t intend — making it easier to notice and revert changes as needed. $ rename -v 's/123/124/' * status.123 renamed as status.124 report123.txt renamed as report124.txt On the other hand, using the -n (or –nono) option makes the rename command tell you the changes that it would make without actually making them. This can save you from making changes you may not be intending to make and then having to revert those changes. $ rename -n 's/old/save/' * rename(logger.man-old, logger.man-save) rename(lyrics.txt-old, lyrics.txt-save) rename(olderfile-, saveerfile-) rename(oldfile, savefile) rename(review.old, review.save) rename(schedule.old, schedule.save) rename(stats.old, stats.save) rename(this.old, this.save) If you’re then happy with those changes, you can then run the command without the -n option to make the file name changes. Notice, however, that the “.” within the regular expressions will not be treated as a period, but as a wild card that will match any character. Some of the changes in the examples above and below are likely not what was intended by the person typing the command. $ rename -n 's/.old/.save/' * rename(logger.man-old, logger.man.save) rename(lyrics.txt-old, lyrics.txt.save) rename(review.old, review.save) rename(schedule.old, schedule.save) rename(stats.old, stats.save) rename(this.old, this.save) To ensure that a period is taken literally, put a backslash in front of it. This will keep it from being interpreted as a wild card and matching any character. Notice that only the “.old” files are selected when this change is made. $ rename -n 's/.old/.save/' * rename(review.old, review.save) rename(schedule.old, schedule.save) rename(stats.old, stats.save) rename(this.old, this.save) A command like the one below would change all uppercase letters in file names to lowercase except that the -n option is being used to make sure we review the changes that would be made before we run the command to make the changes. Notice the use of the “y” in the regular expression; it’s required for making the case changes. $ rename -n 'y/A-Z/a-z/' W* rename(WARNING_SIGN.pdf, warning_sign.pdf) rename(Will_Gardner_buttons.pdf, will_gardner_buttons.pdf) rename(Wingding_Invites.pdf, wingding_invites.pdf) rename(WOW-buttons.pdf, wow-buttons.pdf) In the example above, we’re changing all uppercase letters to lowercase, but only in file names that begin with an uppercase W. Wrap-up The rename command is very helpful when you need to rename a lot of files. Just be careful not to make more changes than you intended. Keep in mind that the -n (or spelled out as –nono) option can help you avoid time-consuming mistakes. Related content how-to How to find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number. By Sandra Henry Stocker Jun 24, 2024 8 mins Linux opinion Linux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability. By Sandra Henry Stocker Jun 17, 2024 5 mins Linux how-to How to print from the Linux command line: double-sided, landscape and more There's a lot more to printing from the Linux command line than the lp command. Check out some of the many available options. By Sandra Henry Stocker Jun 11, 2024 6 mins Linux how-to Converting between uppercase and lowercase on the Linux command line Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy. By Sandra Henry Stocker Jun 07, 2024 5 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe