How to Rename Files in Linux Using Command Line

0
660
How to Rename Files in Linux Using Command Line

Renaming files is not a tough task. It doesn’t need some special skills or advanced operation tools. It is usually effortless unless done in a small file. But, when you have to rename a file containing pictures from last year’s Christmas reunion, you need some time-saving tricks and tips. Therefore, in today’s article, we will learn how to rename file Linux successfully. We have listed various methods for renaming files below. So, let’s start. 

Generally, there are two methods to rename batch files: one is the command-line interface, and the other one is by using a standalone application. However, for Linux, the command-line interface has undoubtedly stood as a winner. Hence, below we have talked about several ways to rename files through the command line. 

But, first, let’s know what a command line is. 

What is a command-line?

A command line is a user interface. Typing commands navigate it at prompts without using a mouse. Hence, when you use a command line to get a job done, your mouse is simply out of the question. A command-line only makes use of a keyboard to navigate instead of a mouse. For example, say the “Windows” folder in the Windows command line is “C:\Windows>.” But, in Linux, it will be simple, depending on the shell, “>” or “%.” 

Therefore, to get a fine renamed or moved or copy and paste, you have to use the command-line interface instead of the GUI or Graphical user interface. 

For renaming files in Linux, the mv command works the best. However, other methods like rename command, mvv command, and renameutils also work great. 

How to rename a file in Linux with the mv command?

The mv command is the easiest way to rename file Linux. “mv” is the short form of move. It does two essential but basic tasks while handling your files on Linux. Moving the files from one location to the other is renaming one or multiple files, using the terminal. 

Let’s see how renaming files on Linux works with the mv command.

Firstly, we are accessing the server using SSH through the command-line. 

So, to access the server, type the following in the terminal:

ssh [email protected]

If you are using a local PC, you have to open the terminal from the main menu itself instead of the server. Next, you need to know how the mv command works. So, to do this, you have to type and run the following.

mv –help

Now, the basic use of this command is as follows:

mv [option] [SOURCE]…[DIRECTORY]

The popular mv options include:

  • -f: this shows no text or message before you overwrite a file
  • -u: you can only move a file either when it is new, or it does not exist in the particular destination
  • -i: this option displays a warning message before you overwrite a file
  • -v: this shows what the command does. 

Now, the parameters are as follows:

[SOURCE]: this is the source destination of the file.

[DESTINATION]: this is the destination directory. 

Rename a file on Linux using the mv command

If you want to rename a file, then you can type the following command:

mv oldnamefile1 newnamefile1

Now, let’s assume that we have a file, file1.txt, in the directory. And, we want to rename and change it to file2.txt. To do this, you need to type the following:

mv file1.txt file2.txt

If you are not in the directory, you have to type a little more command, and then your file will be renamed.

cd /home/user/docs/files

mv file1.txt file2.txt

Hence, by using this method, you can easily Linux rename files. You can even rename multiple files using the mv command. 

Rename multiple files using the mv command

You can only rename one file using the mv command. But, you can use the mv command with other commands to rename multiple files. 

For renaming multiple files, let’s use while, for, and find loops. 

So, when you try to change every file in your current directory from .txt extension to .pdf extension, you can use the following command:

for f in *txt; do

   mv — “$f” “${f%.txt}.pdf”

done

This will generate a loop looking for all the .txt extension files and then change them from .txt extension to .pdf. After that, it will end the loop. 

If you are looking for more advanced and helpful features, then you can use the rename command. 

Rename a file on Linux using the rename command

You can rename your files using the rename command as well. This is not the common Linux command. The rename command is written in Perl instead of C., So; you can install it to rename a file in Linux. 

# sudo apt install rename

Now, the command’s syntax is:

# rename ‘s/old-name/new-name/’ files

As you see in the above highlighted text, a Perl expression is required and enclosed in ‘s/old-name/new-name/.’ Moreover, this also comes with the following optional argument. 

  • -v: Verbose; this means print file names are successfully renamed.
  • -f: Overwrite; this allows the existing files to overwrite.
  • -m: Manual; this means print manual page.
  • -n: No action; print the files to be renamed but do not rename.
  • -h: Help; print OPTIONS and SYNOPSIS
  • -V: Version; show the version number
  • -E-statement: this is a code to take action on files name, as -e, terminated by ‘;’
  • -e: Expression; to act on files name

# rename [-v] [-n] [-f] perlexpr [files]

Now, let’s show you how to use the basic syntax to rename all the files present in your personal directory. We will rename .txt files to .doc files. 

# rename ‘s/\.txt$/.doc/’ *

You can see that the above example was successful. Now, we will remain all the files again to .txt. To do this, we will use -v or Verbose. 

# rename -v ‘s/\.doc$/.txt/’ *

As you can see, when we used the Verbose option, we not only could see all the files renamed but also know the exact changes made. 

Now, showing you another example. This time, we will capitalize on all the first alphabet of the filenames. We are going to use -v again to see what changes will be made. 

# rename -v ‘s/./\U$&/’ *

Now, mastering the Perl technique will require some time and learning. However, you can always use the classic “mv” command to rename all your files. 

Rename a file on Linux using the mmv command

The mmv command is used to copy, move, rename, and append files in bulk. You can do this by using the standard wildcards. To install it on your Linux, you have to run the following command:

$ sudo apt-get install mmv

Now, let’s you have the below-mentioned files in your directory:

$ ls

v1.txt v2.txt v3.txt

Let’s say you want to rename all your files starting with “v” to “w.” So, you can easily do this manually. But, changing the names of bulk files is not easy. Hence, in such a situation, you can easily use the mmv command. 

So, to rename files from “v” to “w”, run:

$ mmv v\* w\#1

Now, let’s check if the files are successfully renamed or not

$ ls

w1.txt w2.txt w3.txt

As you can see, all the file starting with the name “v” has successfully changed to “w.” Therefore, you can get your answers on how to rename a file in Linux. 

Explanation

When you look at the above example, you can see the “v\*” is the “from” pattern and “w\*” is the “to” pattern. According to the above example, mmv command will look up all the files starting with “v.” It will then rename all the files as per the next parameter, i.e., the “to” pattern. We have used wildcards such as ‘*,’ ‘[], ‘and ‘?’. We use them to match the arbitrary characters. Moreover, keep in mind that you have to escape the wildcard characters. If you do not escape them, those will be expanded by the shell. Hence, mmv will be unable to understand it. 

As you can see, in the above example, we have used the asterisk wildcard. And, we have written it as #1. Remember to escape the hash sign. Also, you can close the patterns with quotes as well. 

Moreover, you can easily change .txt files to .doc files with mmv command. You have to run:

$ mmv \*.txt \#1.doc

So, in this simple way, you change one file extension to another file extension.

Now, let’s say you have the following files:

$ ls

efgd1.txt efgd2.txt efgd3.txt

Now, you want to change the “efg” to “yza” in all files. You can do this by typing and running the following command:

$ mmv ‘*efg*’ ‘#1yza#2’

Note: The patterns are enclosed in single quotes. 

So, let’s check if “efg” is replaced with “yza.” 

$ ls

yzad1.txt yzad2.txt yzad3.txt

Hence, you can see the files’ names are changed from efgd1, efgd2, efgd3 to yzad1, yzad2, and yzad3. 

Therefore, in this manner, you can easily rename files using mmv command. 

Also Read: 13 Best Business VoIP Providers: Ring Central, Nextiva, Ooma, and More

Rename a file on Linux using renameutils

Renameutils is another method to rename files in Linux using the command line. Renameutils are programs designed for batch renaming directories and files easier and faster. It consists of the five following programs:

  1. qmv (quick move)
  2. imv (interactive move)
  3. deurlname (delete URL)
  4. qcp (quick copy)
  5. icp (interactive copy)

In this article, we will show you how to rename files through qmv and qcp. 

Installing renameutils

You will find renameutils in the default repository of Linux. So, if you want to install it in an Arch-based system, run the following command:

$ sudo pacman -Syu renameutils

For installing it in a Debian-based system, run the following:

$ sudo apt install renameutils

  • qmv

When you run the qmv program, it opens your file names in the editor and allows you to edit them. 

Let’s say you have the following file names in a folder “space”

$ ls space/

abcd1.txt abcd2.txt abcd3.txt

Now, if you want the files in the “space” directory, run:

$ qmv space/

Change the file names as you wish. You will be able to see the preview as you change the file names. 

Moreover, you can also add cd and then run qmv. 

Once you open the files, you will see two columns. 

So, the left column displays the filenames source, and the right column displays the destination names. Now, you can rename the files on the right column as you wish. After you are done renaming the files, save and quit them.

You will be able to see the following output:

The plan is valid.

abcd1.txt -> xyzd1.txt

abcd2.txt -> xyzd2.txt

abcd3.txt -> xyzd3.txt

Regular rename

abcd1.txt -> xyzd1.txt

abcd2.txt -> xyzd2.txt

abcd3.txt -> xyzd3.txt

If you want to check if the changes are made, then check it using the ‘Is’ command:

$ ls space/

xyzd1.txt xyzd2.txt xyzd3.txt

So, as you can see, all the changes have been made. The renameutils has also renamed the directory name. 

If you do not want to use the dual-column format, you can use the following command to display just the destination file column. 

$ qmv -f do space/

Here “do” refers to a destination only, and “-f” refers to format. Now, you will be able to the destination column, and you can make changes to it. So, you are done now and save and quit the file. 

  • qcp

This program does not rename files but instead copies files. So, in this case, you will get two copies of the same file. So, you keep both the original and duplicate files. 

$ qcp space/

Now, rename the files on the right-hand side. Then, save and quit the file. If you want to verify the changes, then do this by running the following command.

$ ls space/

abcd1.txt abcd2.txt abcd3.txt xyzd1.txt xyzd2.txt xyzd3.txt

Hence, in the above ways, you can rename file Linux.

Takeaway!

I hope this article helped you to know how to rename file Linux. While the mv command is the most popular and used, you can also try the rename command and the mmv command for batch renaming files. 

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.