Home Bash Tips [Bash Tips] Rename Files Without Typing Full Name Twice In Linux

[Bash Tips] Rename Files Without Typing Full Name Twice In Linux

By sk
Published: Updated: 3.4K views 4 mins read

This brief guide explains how to rename files without typing full name twice in Linux operating systems.  As we all already know, we use mv command to rename files in Linux. To rename a file, the command would be:

$ mv <old_file_name> <new_file_name>

Let me show you an example.

$ touch ostechnix1.txt
$ mv ostechnix1.txt ostechnix2.txt
Rename files in Linux using mv command
Rename files in Linux using mv command

This is how we rename files using mv command in Linux. The above command will rename the file named "ostechnix1.txt" to "ostechnix2.txt". As you see, we type the file names two times in the above command. However, it is not necessary. We can easily rename files in Linux using mv command without having to type the file names two times, with the help of a simple BASH function.

Rename Files without Typing Full Name Twice in Linux

Edit your ~/.bashrc file with your favorite editor:

$ nano ~/.bashrc

Add the following lines at the end:

# Bash Function To Rename Files Without Typing Full Name Twice
function mv() {
  if [ "$#" -ne 1 ] || [ ! -e "$1" ]; then
    command mv "$@"
    return
  fi

  read -ei "$1" newfilename
  command mv -v -- "$1" "$newfilename"
}
A Simple Bash Function To Rename Files Without Typing Full Name Twice
A Simple Bash Function To Rename Files Without Typing Full Name Twice

Here, the bash function name is mv. You can choose any other different name of your liking. Save and close the file. And then run the following command to take effect the changes:

$ source ~/.bashrc

From now on, you can only specify the original file name and rename the file as shown below.

$ mv ostechnix1.txt

This will display the file name in the Terminal. Edit the file name and hit ENTER key to rename it. Have a look at the following visual demo.

Rename Files Without Typing Full Name Twice In Linux
Rename Files Without Typing Full Name Twice In Linux

As you see in the above output, I have renamed the file ostechnix1.txt to ostechnix2.doc. Quite handy, right?

Use to arrow keys to move thorough the letters or hit the backspace key to remove all letters and type a new name for the file. Not just file name, you can rename the file extension as well. This is literally like hitting the F2 key and rename the file in GUI.

Other ways to Rename Files without Typing Full Name Twice

There are also a few other ways to rename a file without having to type full name two times.

Method 1 - using mv command

Apart from the BASH function method, here is another wimple way rename files with mv command. We don't even need a BASH function. Use the following one-liner command to quickly rename the file:

$ mv ostechnix{1,2}.txt

The above command would copy the file named ostechnix1.txt to ostechnix2.txt.

Here is another example. The following command will rename "IMG_20140210_150415974.jpg to "IMG_20140210_ostechnix.jpg".

$ mv IMG_20140210_{150415974,ostechnix}.jpg

If you don't want to rename, but want to have two copies of the same file, use cp command like below:

$ cp ostechnix{1,2}.txt

The above command will copy the contents of ostechnix1.txt to ostechnix2.txt. You should have now two files with same contents.

Method 2 - Using readline keybindings (CTRL+w,  CTRL+y and CTRL+y)

We can use readline keybindings to quickly rename a file. These keybindings comes with mainstream shells by default. The CTRL+w command will cut the last word from a command and the CTRL+y key will paste that word in Terminal.

Rename Files Without Typing Full Name Twice With readline Keybindings
Rename Files Without Typing Full Name Twice With readline Keybindings

As you see in the above output,

  • First, I type "mv ostechnix1.txt".
  • Secondly, I press CTRL+w to cut the last word i.e. ostechnix1.txt.
  • Thirdly, I press CTRL+y to paste the last word.
  • Finally, I press CTRL+y once again to paste the same word and then rename it.

Method 3 - Using "imv" from renameutils

The renameutils is a set of programs that is designed to batch renaming files and directories faster and easier. Renameutils consists of an utility called "imv" among others. It allows you to interactively rename the file name.

To rename a file, do:

$ imv ostechnix1.txt

This will display the above filename in the Terminal. Edit the filename as you like and hit ENTER to rename it. To learn about other renaming methods, refer the following guide.

Hope this helps.

Resource:

You May Also Like

4 comments

Jalal April 22, 2020 - 10:29 am

Hi,
Thanks a lot
I use rename command for renaming

Reply
Khurram April 23, 2020 - 12:27 pm

Hi
Thanks … its a handy one!

Reply
Eric April 27, 2020 - 6:04 pm

Should say CTRL+w:
“Secondly, I press CTRL+y to cut the last word i.e. ostechnix1.txt.”

Reply
sk April 27, 2020 - 6:24 pm

Thanks for pointing it out. Fixed now.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

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

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More