Common Linux Commands (2)

11. vim
vim is the built-in text editor of the Linux system, which can be understood as the Word software in the Windows system.
:w filename saves the document with the specified filename.
:wq saves and exits.
:q! exits without saving and forces exit from command line mode.

Function keys:
1) Insert mode: Press "i" to switch to insert mode. After entering insert mode with "i", you start typing from the current cursor position. Press "a" to enter insert mode from the next position of the current cursor. Press "o" to enter insert mode and start typing from a new line at the beginning.

2) Switch from insert mode to command line mode by pressing the "ESC" key.

3) Move the cursor: vi can directly use the arrow keys on the keyboard to move up, down, left, and right, but the standard vi uses lowercase letters "h", "j", "k", "l" to control the cursor movement left, down, up, and right respectively. 
Press "ctrl" + "b": move the screen "backward" one page.
Press "ctrl" + "f": move the screen "forward" one page.
Press "ctrl" + "u": move the screen "backward" half a page.
Press "ctrl" + "d": move the screen "forward" half a page.
Press number "0": move to the beginning of the document.
Press "G": move to the end of the document.
Press "$": move to the "end of the line" where the cursor is.
Press "^": move to the "beginning of the line" where the cursor is.
Press "w": the cursor jumps to the beginning of the next word.
Press "e": the cursor jumps to the end of the next word.
Press "b": the cursor goes back to the beginning of the previous word.
Press "#l": the cursor moves to the #th position in the line, e.g., 5l, 46l.

4) Delete text:
"x": each press deletes the "next" character at the cursor position.
"#x": for example, "6x" means delete the "next" 6 characters at the cursor position.
"X": uppercase X, each press deletes the "previous" character at the cursor position.
"#X": for example, "20X" means delete the "previous" 20 characters at the cursor position.
"dd": delete the line where the cursor is.
"#dd": delete # lines starting from the line where the cursor is.

5) Copy:
"yw": copies characters from the cursor position to the end of the word into the buffer.
"#yw": copies # characters into the buffer.
"yy": copies the line where the cursor is into the buffer.
"#yy": for example, "6yy" means copy the text from the line where the cursor is "downward" for 6 lines.
"p": pastes the characters in the buffer at the cursor position. Note: All copy commands related to "y" must be paired with "p" to complete the copy and paste function.

6) Replace:
"r": replaces the character at the cursor position.
"R": replaces the character at the cursor position until the "ESC" key is pressed.

7) Undo the last operation:
"u": if you mistakenly execute a command, you can immediately press "u" to return to the previous operation. Pressing "u" multiple times can execute multiple undos.

8) Change:
"cw": changes the word at the cursor position to the end of the word.
"c#w": for example, "c4w" means change 4 words.

9) Jump to a specified line:
"ctrl" + "g" lists the line number where the cursor is.
"#G": for example, "16G" means move the cursor to the beginning of line 16 in the document.

12. less / more
less and more are file viewing tools, but less has more features. It is quite difficult to open a 10G file in Windows, but in Linux, it is very convenient. less can open very large files, and compressed formats can also be opened directly.
-m shows a percentage similar to the more command.
-N shows line numbers.
-S formats the display.
$ less -S ab.tar.gz

13. head / tail
These two commands are quite simple; they just take a specified number of lines from the head and tail of a file, defaulting to 10 lines, and can be set with -n. Using pipes, you can take lines from the middle of a file.
# Get lines 21 to 40 from the file
$ head -40 a.txt | tail -n 20

14. g(un)zip / b(un)zip2
gzip and bzip2 are file compression tools that directly process the source files by default, with a compression ratio of about 2/3, and both can be set. Adding "un" means unpack, indicating decompression.
$ gzip a.txt
$ gunzip a.txt.gz

15. tar
Tape archive tar is a more complex command mainly used for packaging. Since tar can call gzip or bzip2 for compression, packaging and compression are often combined into one process, similar to Windows systems, which beginners often confuse. 
-c creates a package archive, and can be paired with -v to see the names of the files being packaged during the process.
-t checks the contents of the package archive to see which file names are included; the focus is on checking the "file names".
-x unpacks or decompresses, and can be paired with -C (uppercase) to unpack in a specific directory.
Optional parameters:
-j compress/decompress with bzip2 support; the file name is best as *.tar.bz2.
-z compress/decompress with gzip support; the file name is best as *.tar.gz.
-v displays the names of the files being processed during compression/decompression!
-f filename -f must be immediately followed by the name of the file to be processed! For beginners, remember that c is for create, x is for unpack, z corresponds to gzip, and j corresponds to bzip2, so the commonly used commands are as follows:
$ tar -jcvf filename.tar.bz2 A B C # Package and compress to a bz2 ending file
$ tar -jxvf filename.tar.bz2 # Decompress the .tar.bz2 ending file
$ tar -zcvf filename.tar.gz A B C # Package and compress to a gz ending file
$ tar -zxvf filename.tar.gz # Decompress the .tar.gz ending file

16. wc
wc = Word Count counts the number of lines, words, and characters in a file.
-l filename reports the number of lines.
-c filename reports the number of bytes.
-m filename reports the number of characters.
-w filename reports the number of words.

17. sort
Sorts by default according to the first column, which can be set with -k; the default sorting rule is ASCII code sorting, which can be modified with -n; -r sorts in the opposite direction; -n sorts according to numerical size; -o saves the sorted result to a specified file; -r sorts in reverse order; -t specifies the column separator character used for sorting; -k selects which range to sort.

Leave a Comment