One of the common operations that you will perform repeatedly is renaming files and folders. Most often you will rename files directly from the command line, but you might also want to do it from a shell script. Well, you could do it with a file manager as well, but what fun is that?
In Linux, all folders are files as well, or it can be considered as a file in almost all file operations. The commands that you use to rename files can also be used to rename folders.
The linux command to rename files is mv or the move command. The Move and Rename are the almost the same operation in Linux and can be used interchangeably. The general syntax of the mv command is
mv <options> <source file name> <destination>
Another command is rename which is much more flexible than the mv command. Most modern distros have the rename command installed, other wise you could install it. It is widely used to rename similarly named multiple files in one go. The general syntax of rename command is
rename <expression> <replacement> <filenames>
Rename Files in the Current Folder
Most times you will be rename the file within the same folder. In this case the mv command takes two arguments: the source file name and the destination name. Assuming that the file world.txt is in the current working directory, you can rename it to earth.txt.
$ mv world.txt earth.txt
The new file name is earth.txt and it will be in the same folder as where the file world.txt was and will have the same file contents and metadata such as permissions. Using the rename command here to just rename one file is probably an overkill, but here is how you do it.
$ rename world earth earth.txt
Rename Files to Another Folder
You can move the file to another folder as well as the change the name of the file using the same mv command. You just have to provide either the absolute or relative path of the destination.
$ mv world.txt /path/to/folder/earth.txt
If you provide a folder as destination, then the command will just move the file to the new folder without renaming it. The destination path just end with the path seperator which is forward slash (/).
$ mv world.txt /path/to/folder/
Files with Extensions
Even if you want to change just the file extension and not the entire file name, the process is the same. You specify the file name with the new extension as the extension is part of the file name.
$ mv image.jpeg image.jpg
The rename command is much more useful in this case. You can rename several different files with the same extension to another extension rather easily.
$ rename .jpeg .jpg image*.jpeg
The above command will rename the extension to jpg of all files that have file name that start with image and an extension jpeg.
File names with Spaces or Hypen (-)
If either the source or destination files have spaces in them, you will need to escape those names. You can do it in one of two ways: either by using the escape character which is the backward slash (\) or by using double quotes (“) around the file name.
$ mv "file name with space.txt" "new file space.txt"
The above example uses double quotes around the names. The double quotes will work even if there are no spaces in the file names. The example below shows how to use escape the names without the quotes.
$ mv file\ name\ with\ space.txt new\ file\ space.txt
Each back slash is followed by a space character which causes the command to match the space character literally rather than consider it as a argument separator. FWIW, You can use both the quotes as well as escape the name in the same command as shown below…
$ mv "file\ name\ with\ space.txt" "new\ file\ space.txt"
The same technique will work with file names that start with a hypen (-). The hypen is used by the command to specify command line options, so if the file name happen to start with a hypen then it will cause the command to consider it as an option.
$ mv -v "\-file\ name.txt" newfile.txt
Use Date as File Name
Sometimes you would want to use the output of a command as part of the file name. The most common use case is when you want to append the current date or time to the file name when backing up files. If you are using a shell script to backup files and you want to know when the backup was made, adding the current date to the file name will help.
$ mv logfile.txt "logfile.txt.$(date)"
You use the dollar sign ($) to specify that what follows is a command. The command itself is enclosed with in the simple brackets as (…). In the above example the date command is executed and the result is appended to the file name.
Renaming Multiple Files
The rename command is meant for this use case. It allows you to easily rename files (or folders) based on a regular expression. Using the mv command to do a rename of multiple command is much more involved. You can do this by using a shell loop such as for or while and using mv repeatedly for each file.
The rename command takes regular expressions to match existing file names and you can replace a part of the file name that are common across all matching file names.
$ rename world hello *world*
For example, lets’ change the word world to hello in all the files that contain the word world. You can also change the extension easily as mentioned in an example above. The rename command is much more powerful than the simple examples shown here. You can use regular expression substitution syntax for more powerful substitutions. Consider reading the manual or info page for rename.