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 examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe