Professional Documents
Culture Documents
In Linux
ostechnix.com/bash-tips-rename-files-without-typing-full-name-twice-in-linux
sk April 21,
2020
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>
$ touch ostechnix1.txt
$ mv ostechnix1.txt ostechnix2.txt
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.
1/5
Edit your ~/.bashrc file with your favorite editor:
$ nano ~/.bashrc
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.
2/5
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.
$ mv ostechnix{1,2}.txt
The above command would copy the file named ostechnix1.txt to ostechnix2.txt.
$ 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:
3/5
$ 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.
$ 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.
4/5
How To Rename Multiple Files At Once In Linux
5/5