Rename a file in Linux with simple command line options
Linux is a very popular operating system and is widely used today on servers and also on standalone systems. It is based on UNIX and is a free OS. There are many people who love to work on it and consider it the most reliable and robust of all the operating systems available today. Working in Linux requires some prior knowledge as the commands have to be given through command line text. In this tutorial, we’ll show you how to rename files in Linux, with different commands. Linux has several ways of renaming files and directories in Linux like cp (copy), rm (remove), mv (move or rename),etc. We’ll walk you through them shortly.
Renaming a file
In order to rename a file in Linux you can use either of two approaches
1. Create a copy of the existing file with the new desired name and then delete the old file.
2. Rename the file by moving it with the mv command.
Lets take up some examples one by one:
Rename with copy and delete
$ cp oldfile newfile
This creates a copy of the same file with a new name in the same location.
$ rm oldfile
This will delete the old file keeping the newfile intact.
Rename by moving
$mv old-file-name new-file-name
This just moves the old file, to a new name.
Renaming multiple files
This is extremely easy case when we are dealing with one file but the complexity increases when we are dealing with multiple files, and this is something with which we encounter very often in real life.
Lets take an example where we want to rename multiple files.
In order to rename multiple files at once we can make use of wildcard characters (for ex: ‘*’). In the below example we will rename all the files with .txt to .dat.
We can achieve this by simply typing the following command:
$ mv *.txt *.dat
Now, let us assume you have a file whose name is misspelled or if there is an alphabet missing from the name of the file then in order to fix this we can leverage the mv command with other wildcard characters.
For example: we have a file with a name RR#.txt, whereas the filename should contain only alphanumeric values instead of any special characters. This can be fixed with the following command:
$ mv RR?.txt RR1.txt
Where ? is the wildcard character used in place of the unknown character.
Many times during your day to day work you may make a mistake you cannot undo. For example, moving or renaming a file accidentally. To avoid this problem Linux provides an interactive renaming option wherein the user will be prompted for confirmation before overwriting the file name. This empowers the end user to choose an option “Y” or “N” from the keyboard in order to finalize the action. In order to leverage this capability one must use -i option with mv command in the following manner.
$ mv -i RR1.txt ST1.txt
Once we run this command the Linux will prompt the end with following line:
mv: overwrite ‘RR1.txt’ ?
User can choose “Y” to continue or “N” to abort this action.
Renaming a directory
Renaming a directory in Linux and Unix is similar to renaming a file. All we need to do is replace the file name with the directory name that is to be renamed.
For example, if we wanted to rename the directory “RR1” to “ST1”, then we can use the following command.
$ mv RR1 ST1
Rename file to different location
If you want to change not only the name of the file, but also it’s location, use the following command:
$ mv RR2 newdir/.
This command moves the file RR2 from its current location and places it under the directory newdir/.
Track what’s happening with Verbose option
If you’re renaming a number of files, or doing other mass operations, you may want to track what is happening. Linux has an easy way of doing that with the -v or -verbose option. Here is the syntax
$ mv -v source.txt new_source.txt
Mass move and rename
Linux has another command, mmv, which stands for mass, move and rename. This is extremely helpful for renaming multiple files at a go. Its utility is not just limited to renaming of files. It can be used for moving, linking and appending multiple files as well. The reason why I like it most because it is the safest way to do these tasks. mmv does it all without any sudden destruction of files due to collisions of target names with existing file names. Moreover, before doing anything, mmv tries to identify any errors that would result from the entire set of actions specified and equips the user with the option of either terminating before beginning, or proceeding by avoiding the offending parts.The syntax for this command is:
$ mmv [options]
This should give you a good insight into how to rename files in Linux. If you want to try out more options, just use the ‘man’ command, and Linux will list out all the options, along with how to use them.
$ man move
Rename is definitely an important part of the Linux toolkit. Since there are so many ways to do it, it will take you a while to master it over the command line. Practise always make you better, so make sure you try it out yourself. If you ever need a recap, feel free to try out this Linux crash course.
Top courses in Linux
Linux students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.