Generally, you need to enter both file names for renaming. If the file name is too long, it will be troublesome. Is there a solution?
Method 1: General rename
$ mv file1.txt file2.txt
Method 2: Advanced 【mv】
$ mv file{
1,2}.txt
# 如果要保留源文件,就选择cp命令即可
$ cp file{
1,2}txt
Method 3: Use the shortcut key [ctl+w] to cut and [ctl+y] to paste.
Method 4: Rename efficiently.
First, we need to use vim to edit the ~/.bashrc
file.
$ vim ~/.bashrc
Add the following code to 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"
}
Then, press wq
save and exit. Then again, use the source
command to make this change take effect.
$ source ~/.bashrc
When you use the mv file1.txt
terminal again , the original file name will be displayed, and then you can directly change the name without having to enter the original name again.