How to Use the rename Command on Linux

Share
  • June 10, 2019

Fatmawati Achmad Zaenuri/Shutterstock.com

Get to grips with the file renaming powerhouse of the Linux world and give mv—and yourself—a rest. Rename is flexible, fast, and sometimes even easier.  Here’s a tutorial to this powerhouse of a command.

What’s Wrong With mv?

There’s nothing wrong with mv . The command does a fine a job, and it is found on all Linux distributions, in macOS, and in other Unix-like operating systems. So it’s always available. But sometimes you just need a bulldozer, not a shovel.

The mv command has a purpose in life, and that is to move files. It is a happy side effect that it can be used to move an existing file into a new file, with a new name. The net effect is to rename the file, so we get what we want. But mv is not a dedicated file renaming tool.

Renaming a Single File With mv

To use mv to rename a file type mv, a space, the name of the file, a space, and the new name you wish the file to have. Then press Enter.

You can use ls to check the file has been renamed.

mv oldfile.txt newfile.txt
ls *.txt

mv oldfile.txt newfile.txt in a terminal window

Renaming Multiple Files with mv

Things get trickier when you want to rename multiple files. mv has no capability to deal with renaming multiple files. You must resort to using some nifty Bash tricks. That’s fine if you know some medium-grade command-line fu, but the complexity of renaming multiple files with mv stands in stark contrast to the ease of using mv to rename a single file.

Things escalate quickly.

Let’s say we’ve got a directory with a variety of files in it, of differing types. Some of these files have a “.prog” extension. We want to rename them at the command line so that they have a “.prg” extension.

Read the remaining 73 paragraphs

Source : How to Use the rename Command on Linux