Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Know that renaming files like this is a divine level!

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

In Linux, renaming a file, we usually use the mv command, usually like this?

Under Linux, to rename a file, we usually use the mv command, which usually works like this:

$mv file1.txt file2.txt

This way of repeating the command is fine, of course, but the drawback is that you need to type the file name twice. It is good to have a short file name, but once it is longer, it will be very frustrating to type it twice.

This article introduces several more efficient methods of file re-command, which most people don't know, but all they know is the great god level.

An efficient method for renaming files

First, we need to edit the ~ / .bashrc file using vim.

$vim ~ / .bashrc

Add the following code to the end of the ~ / .bashrc file

# 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 to save and exit. Next, use the source command to make this change take effect.

$source ~ / .bashrc

Next, it's time to witness miracles!

Now that we want to rename the file1.txt file, we just need to do this:

$mv file1.txt

As can be seen from the dynamic picture above, the original file name will be displayed in the terminal, and then you can change the name directly without having to type the original name again.

After renaming is successful, the terminal will also prompt like this:

'file1.txt'->' file2.txt'

Other efficient renaming methods

Of course, in addition to modifying the .bashrc file, there are many efficient renaming methods, and here are two more.

(1) method 1: use mv command

Back to the mv command? Hit you in the face?

But goose, the command is mv, but the usage is different!

In many cases, we just change a part of the file name, for example, by changing file1.txt to file2.txt, that is, changing 1 to 2. In this case, there is actually a more efficient change:

# mv file {1,2} .txt

The result of the operation is as follows:

If we want to keep the original file and make a copy, just replace the mv command with the cp command.

# cp file {1,2} .txt

The result should be obvious, so there is no screenshot.

(2) method 2: use shortcut keys (ctrl+w,ctrl+y)

Since most people know that renaming a file requires entering the file name twice, then I will satisfy you and still enter the file name twice. However, a more efficient method of repeatedly entering file names is introduced here.

In Shell, we can use ctrl+w to cut the last word and ctrl+y to paste the cut content. So with this shortcut we can re-enter the file name more efficiently.

Move up the image directly:

Original address: https://www.linuxprobe.com/sigei-dashen.html

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report