Hello, I’m Liang Xu.

As a Linux user, you must have typed countless commands on Linux terminals. Some commands are very short, such as ls, CD, PWD, etc., so there is no pressure. However, some commands are longer, such as:

$ git push origin HEAD:refs/for/masterCopy the code

Would you be devastated if you missed even one letter?

In the past, if I typed a wrong command, I moved the cursor to the wrong place and corrected it. I believe that not only I, most of my friends are doing so.

If the command is short, it’s ok. If the command is long, is it frustrating?

Here are seven ways you can use Bash to improve your command input so that you can effectively correct it if you make a mistake.

1. Obtain the last parameter of the command

! $

In Bash,! The $represents the last parameter in the command. Let’s consider the following scenario to illustrate the use of this symbol. Suppose I want to move a file to another location, but the filename is wrong, for example:

$mv /path/to/ error file /some/other/place mv: cannot stat '/path/to/ error file ': No such file or directoryCopy the code

In this case, we move the cursor to the wrong file name, and then correct the file name. This method can certainly be used, but if the command is long, it will be less efficient.

In this case, we can do this:

$ mv /path/to/对的文件 !$
mv /path/to/对的文件 /some/other/placeCopy the code

This way we don’t have to enter the last parameter. If the parameter is long, the efficiency gain will be noticeable.

2. Obtain the NTH parameter of the command

! :n

We often misplace the order of commands’ arguments, for example:

$ tar -cvf afolder afolder.tar
tar: failed to openCopy the code

In this case, we usually delete the misplaced parameters and then enter the correct ones. At this point, if the parameters are very long, I expect you will have the urge to burst the screen.

Here’s what you can do:

$! : 0! : 1. : 3! :2 tar -cvf afolder.tar afolderCopy the code

Perfect solution! But what do those symbols mean?

If you look closely, you should be able to tell. Based on the mistyped command,! :0 is tar,! :1 is -cvf,! :2 is afold! :3 Do I need to tell you? In plain English, positional parameters.

So we just need to reorder these elements by positional parameters, and we’re OK. Easy, right?

PS: ! : represents the last positional element.

3. Obtain all parameters

! : - $1

Generally speaking, the first element of a command is the specific command, followed by parameters. If we want to get all the parameters, we can use it! : – $1.

For example, I wanted to use tar but wrote zip instead:

$ zip -cvf afolder.tar afolderCopy the code

At this point, you can do this:

$ tar ! : - $1Copy the code

Of course, you can use this if you don’t want to get all the parameters! :1-2, or! :3-9 to get a subset of the parameters, provided you have enough of them.

4. Obtain the parameters of the penultimate NTH command

! -n:$

Under Linux we type a lot of commands, generally speaking! Stands for the last command, which is the same as! Minus 1 is equivalent. If we want to get the argument of the penultimate NTH command, we can use! – n: $.

For example, when we move a file, if we are prompted that the file does not exist, we usually use ls to see what files are in the corresponding directory:

$ mv /path/to/wrongfile /some/other/place
mv: cannot stat '/path/to/wrongfile': No such file or directory
$ ls /path/to/
rightfileCopy the code

At this point, we can use it again! $will fetch /path/to/, which is the last element of the current last command, but we need the element of the penultimate command, what do we do?

We can use it! – 2: $:

$ mv /path/to/rightfile ! -2:$ mv /path/to/rightfile /some/other/placeCopy the code

So we can use this shortcut when we need the previous element again when we need the command to cut back and forth.

2020 selected Ali/Tencent and other front-line big factory interview, resume, advanced, e-book public number “Liang Xu Linux” background reply “information” free access

5. Obtain the directory corresponding to the file

! $:h

Suppose we want to package a file and say it doesn’t exist:

$ tar -cvf system.tar /etc/afile
 tar: /etc/afile: Cannot stat: No such file or directory
 tar: Error exit delayed from previous errors.Copy the code

The usual way to do this is to go to the /etc directory and see what files are in there. That’s fine, but if the pathname is long, you might want to prick the screen again.

We can use it! $:h to get the path of the last parameter. Now the last parameter is /etc/afile, which corresponds to /etc/afile. We can use it! $:h to get the path of the last argument:

$ cd ! $:h cd /etcCopy the code

6. Get the first element of the current row

! # : 1.

Normally, we rename a file like this:

$ mv /path/to/some/file /path/to/some/file.bakCopy the code

The two parameters are very similar, except that the.bak is added to the end of the parameter, and we need to write it twice. What a waste of time! We can use it! #:1 to get the first parameter of the current line.

$ cp /path/to/some/file ! #:1.bak cp /path/to/some/file /path/to/some/file.bakCopy the code

Does that save a lot of typing?

7. Replace the string in the command

!!!!! :gs

This operation is even more powerful, replacing some strings from the previous command directly. For example, we have a command like this:

$ echo my f key doef not work
my f key doef not workCopy the code

If we want to replace f with s, we can do this:

$!!!!! :gs/f /s / echo my s key does not work my s key does not workCopy the code

Similarly, if we wanted to change “does” to “did”, we could do this:

$!!!!! :gs/does/did/ echo my s key did not work my s key did not workCopy the code

In this way, you can change the keywords in the command to what you want.

summary

This article introduces 7 shortcuts that can be used to increase the efficiency of command typing. These methods are still piecemeal, but if you put them together, they can be even more powerful.

All you see is true love. Why don’t you like it before you leave? Your “sanlian” is the biggest motivation for liang Xu’s continuous creation!

  1. Pay attention to the original public number “good Xu Linux”, the first time to get the latest Linux dry goods!
  2. Public number background reply [information] [interview] [resume] to get selected first-line big factory interview, self-improvement, resume and other information.
  3. Follow my blog: lxLinux.net