Use Linux command line like a hacker (reprint)

Source: Internet
Author: User

Read Catalogue

    • Objective
    • Using the Tmux MUX Console window
    • Move the cursor quickly in the command line
    • Quickly delete text from the command line
    • Quick View and search history commands
    • Quick Reference and Retouch history commands
    • Record screen and convert to animated GIF picture
    • Summarize
Back to the top of the preface

Before in the blog Park to see an article about the IntelliJ idea configuration, it is a GIF animated images to display, I feel very good. So in my post today and later, I will try to use GIF to show. GIF animation is very time-taking, in order to make my blog to create a boutique I also quite spell. The advantage of using dynamic diagram is that the demonstration effect is good, the disadvantage is that the motion diagram volume is too large, in order to reduce the picture volume, I can only reduce the resolution.
The topic of efficient use of the command line is a cliché on the web. And this article also draws on the "use of Linux command line like a hacker" recommended in the CSDN geek headline. But in this article, there are many of my own views and experience, such as I will mention some shortcut keys to memorize, some do not need to remember, after all, our memory is limited, I will also mention some mnemonic methods. Therefore, this article is definitely not scripted, deserves everyone to have, please remember to praise.

Back to top using the Tmux Multiplexing Console window

The first principle of efficient use of the command line is to try to avoid non-command-line interference, what does it mean? That is, one but open a console window, try not to switch on the desktop to switch, not a moment by other windows blocking the console, and then let other windows destroyed the background of the console, it is best to maximize or full screen console, preferably even the mouse do not use. Only in this way can the higher efficiency be achieved. But in practice, we often need to work in multiple consoles at the same time, such as commands to run the recording screen in one console, work in another console, or work in one console and read the document in another console. If you want to work in more than one console, and do not want a lot of window block to block, change to change, you can consider to try Tmux. Such as:

Tmux function A lot, what Session ah, Detach Ah, Atach ah what we do not need to care for the moment, just use its console window reuse function on the line. With the Concept of window and pane in Tmux, Tmux can create multiple windows that are not obscured by each other, display only one window at a time, and other windows are automatically hidden and can be toggled between windows using shortcut keys. At the same time, you can split a window into multiple pane, which are displayed simultaneously on the screen, and can be toggled between pane using shortcut keys.

Tmux shortcut keys are many, to want to fully understand tmux the best way of course is to read TMUX documents, use the command man tmux on it. But we just need to remember a few important shortcut keys on it, as in the following table:

shortcut Keys function
ctrl+b C Create a window
ctrl+b [N][p]
ctrl+b & close current Window
ctrl+b "
ctrl+b% Divide the current window or pane into two pane, arrange around
ctrl+b x close current pane
ctrl +b [↑][↓][←][→]
ctrl+[↑][↓][←][→] Adjust the current pane size, one grid at a time
alt+[↑][↓][←][→]

Tmux shortcut keys are special, in addition to adjusting the pane size of the shortcut keys, the other is to first press ctrl+b, and then press a character. Press Ctrl+b and then C to create a window, where C is the Create window. You can switch between windows by pressing Ctrl+b, then n or P, which is the meaning of next window and previous window. Close the window is the first press ctrl+b, then press &, this can only rote. Press CTRL+B, and then press "to indicate the split window up and down, you can imagine that single and double quotation marks on the keyboard are on the top and bottom of the relationship." First press ctrl+b, and then% to split the window, presumably because the percentages are left and right to write it. As for moving between pane and adjusting the pane size of the arrow keys, you don't have to say it more.

Go back to the top quickly move the cursor in the command line

When you enter a command at the command line, you often move the cursor around the command line. This is very simple, use the left and RIGHT arrow keys can be, but sometimes we enter a long string of commands, but suddenly to modify the command at the beginning of the content, if you use the left ARROW key one character to move the cursor to the beginning of the command, is it too slow? Sometimes we need to switch directly between the beginning and the end of the command, and sometimes we need to be able to move the cursor one word at a time, in the command line, which is not really a thing. Such as:

These kinds of mobile methods are shortcut keys. In fact, a character to move a character cursor also has shortcut keys Ctrl+b and ctrl+f, but these two shortcut keys we do not need to remember, what can be more convenient than the left and right arrow keys? What we really want to remember is these few:

shortcut Keys function
Ctrl + A Move the cursor to the beginning of the command line
Ctrl + E Move the cursor to the end of the command line
Alt + B Move the cursor one word to the left
Alt + F Move the cursor one word to the right

These shortcuts are too good to remember, a for ahead,e on behalf of END,B representative Back,f representative forward. Why does the shortcut for moving the cursor by word start with the ALT key? That's because the shortcut key that moves the cursor by character takes Ctrl. But the shortcut keys for moving the cursor by character we don't use it, because we have the right and left arrow keys.

Go back to the top quickly delete text on the command line

Modifying the input content is also something we often do, and modifying the command line involves deleting a portion of the content and then entering new content. We encounter the situation is sometimes only need to modify the individual characters, sometimes need to modify individual words, and sometimes, the input of a long period of a command, we say not all, the whole row deleted. The usual delete keys are BackSpace and delete, but is it too slow to delete one character at a time? So, please memorize the following shortcut keys:

shortcut Keys function
Ctrl + U Delete everything from the cursor to the beginning of the line, and if the cursor is at the end, the entire row is deleted.
Ctrl + K Delete everything from the cursor to the end of the line, and if the cursor is at the beginning, the whole line is deleted.
Ctrl + W Delete a word before the cursor
Alt + D Delete a word after the cursor
Ctrl + Y Paste the content you just deleted to the cursor, sometimes delete the wrong can use this shortcut key to recover deleted content

Results See:

These shortcuts are also good to remember, you represent undo,k on behalf of KILL,W representative of Word,d Delete, Y represents Yank. One of the more strange is that alt+d is also the beginning of Alt, that is because Ctrl+d is occupied. Ctrl+d has a few meanings, when editing the command line it represents the deletion of a character, of course, this shortcut is actually not used, because BackSpace and delete more convenient. When some programs read data from stdin, Ctrl+d represents EOF, which we occasionally use.

Back to top Quick View and search history commands

For commands that have been run, unless it is particularly short, we generally do not repeat the input, and it is much faster to find out from the history of nature. The most we've used is ↑ and ↓, especially the command that was just entered recently, using ↑ to turn up a few lines to find, click Enter to execute, how comfortable. But sometimes, obviously remember is not a long-ago command, but turned up half a day did not find, how to do? Then you history have to use the command to view all the history records. What if the history is very long? can be used history | less and history | grep ‘...‘ . But there is the ultimate kill trick, which is to press Ctrl+r to search through the history. After pressing Ctrl+r, each character entered will be incrementally matched to the history, and the more input, the more accurate the match. Of course, sometimes there are many commands that contain the same search string. By continuing to press CTRL+R, you will continue to search for the next matching history. Such as:

Here, you need to remember the commands and shortcut keys in the following table:

command or shortcut key
history View history
history | Less
history | grep ' ... '
ctrl + R reverse Search history, incremental match of characters entered
esc stop search history and place the results of the current match into the The previously entered command line
enter
ctrl + G stop search history and discard the results of the current match
alt +,

It is important to note that when we search in the history, there is a location tag, Ctrl+r is to start from the current position, reverse search, R for the reverse, each search for a record, the location tag will move to the head of history, the next search from here to continue to search the head. So, we must remember the shortcut key alt+> it can restore the location marker of the history record. It is also important to note that there are three shortcut keys to stop the search history, and if you press ENTER, the matching command executes immediately, and if you still want to have the opportunity to modify this command, do not press Enter and press ESC. If you don't want anything, just press ctrl+g, and it will pay you a blank command line.

Back to top quick reference and Retouch history commands

In addition to viewing and searching history, we can refer to the commands in the history in a more flexible way. A common simple example is a !! reference to the previous command, which !$ refers to the last parameter referencing the previous command, which ^oldstring^newstring^ represents replacing the oldstring in the previous command with NewString. These operations are some of the common techniques that we normally use when using the command line, in fact, their essence is the history expansion function provided by the history Library. Bash uses the history library, so you can also use these features. Its complete documentation can be viewed in the Man man history page. Knowing the theory of history expansion, we can also do some more complicated operations, such as:

The full format of the reference and adornment history commands is this:

![!|[?]string|[-]number]:[n|x-y|^|$|*|n*|%]:[h|t|r|e|p|s|g]

As you can see, a reference to the historical command is : divided into three parts, the first part determines which historical command to refer to, and the second part determines the first word in the history command, the word is numbered from 0, that is, the No. 0 Word represents the command itself, The 1th to the last word represents the parameters of the command, and the third part determines how the selected words are decorated. I'll list the complete table below:

Table one, which historical command to quote:

operator function
! All references to historical commands are! Start with a quick replacement in addition to the ^oldstring^newstring^ form
!n Reference to Nth History command
!-n Reference to the penultimate nth history command
!! Refer to the previous command, equal to!-1
!string Reverse Search history, first command starting with string
!? String[?] Reverse Search history, first command with string
^oldstring^newstring^ Quick replacement of the previous command, replacing oldstring with newstring
!# Reference the currently entered command

Table two, choose which word:

operator
0 The No. 0 word, in the shell, is the command itself.
N Nth Word
^ 1th Word, you can omit the preceding colon when using ^
$ The last word, using $ yes, can omit the preceding colon
% and? string? Matches the word, you can omit the preceding colon
X-y From the first x Word to the first y word,-y for 0-y
* All words except the No. 0 Word are equal to 1-$
x* From the first x word to the last word, equal to x-$, you can omit the preceding colon
X- From the first X Word to the second word of the penultimate

Table three, what to do with the selected words to modify:

p
operator function
h Select path Start, do not file name
t Select Path end, as long as filename
r Choose file name, do not extend
e Choose Extension, do not file name
s/oldstring/newstring/
g global substitution, and s with

These commands are actually very good to remember, H for head, as long as the path starts not the file name, T stands for tail, as long as the path end of the file name, R for Realname, as long as the file name does not extend the name, E for extension, as long as the extension does not file name, S represents Substitute, perform the Replace function, G for global, global Replace, p for print, print not to execute. Sometimes the use of light :p is not enough, we can also put this reference modified command directly on the current command line and not immediately execute, its shortcut is:

operator function
Ctrl + Alt + E Expands the history command reference on the current command line, does not execute immediately after expansion, can be modified, and is not executed until you press Enter
Alt + ^ As with the above function

These two shortcut keys, remember one on the line. So, when we finish the reference to the history command, we can start with a look at it and then execute it correctly. Seeing as it is, I am relieved to have a look at it every time.

Go back to the top recording screen and convert to animated GIF picture

Finally, I will show you the process of making animated GIF pictures. The software I used has recordmydesktop , mplayer , and convert . You need a separate console to run the recording function when you use recordmydesktop , and you need to enter CTRL + C to stop the recording when the recording is complete. So I used the tmux. First, I start Tmux, and then run recordmydesktop--full-shots--fps 2--no-sound--no-frame--delay 5-o ~/Pictures/record_to_gif.ogv command to start recording. Since Recordmydesktop does not exit immediately after the recording starts, the window is occupied when the video is started, so I press ctrl+b C to have Tmux create another window, and then any action done in this window will be recorded System. When the recorded operation is complete, press ctrl+b N to switch to the window that the recordmydesktop command is running, and press CTRL + C to terminate the recording. Then, use mplayer-ao null Record_to_gif.ogv-vo jpeg:outdir=./record_to_gif to extract the recorded video as a picture. Of course, at this time more pictures, in order to reduce the size of the last GIF file, we can delete the insignificant frame, leaving only the keyframes. Finally, use the command convert-delay record_to_gif/* record_to_gif.gif to generate the GIF animation. The entire process, such as:

The resulting GIF images are generally larger, often more than 20M, if the time is a little longer, more than 60M is often a thing. Before making a GIF, each frame of the picture is about 200k. I think it might be because GIF doesn't have a good compression algorithm like JPEG or PNG. GIF to deal with the vector map effect is very good, against the photo and I like this, compression is a bit too much. Blog Park allows uploading of images per sheet can not exceed 10M, so, in order to reduce the volume of GIF files, I only use the convert -resize 1024x576 record_to_gif.gif record_to_gif_small.gif command to make the image smaller before uploading.

Back to top of the summary

There are a lot of tricks for using the Linux command line, and I can't talk about it all here. The best way to learn the Linux command line is to use a man bash document that looks at the Bash as well. But what I'm talking about here can significantly improve the efficiency of using the command line, at least for the two days I think I have a qualitative leap. In addition, the use of GIF animated images in the blog to do the example, I think it is my blog since a qualitative leap. I hope you enjoy it.

Thank the Yumbo Lord for sharing

(Jingshan Ranger in 2016-08-11 published in the blog Park, reproduced please indicate the source. )

Use Linux command line like a hacker (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.