Introduction to viM’s main modes

Vim Command mode Q: Are vi and vim installed from the same software package? A: NO, vim is an incremental version of vi. The most obvious difference is that Vim has syntax highlighting and is fully compatible with VI

To view a command, which package is installed:

[root@xuegod63 ~]# rpm -qf /usr/bin/vim
[root@xuegod63 ~]# which vim
[root@xuegod63 ~]# RPM -qf 'which vim' # 'backquote, the key under the ESC key, the executable in the backquoteThe [root @ xuegod63 ~]# RPM -qf $(which vi) #$Both $() and ' 'can be used for command substitution, which, much like variable substitution, is used to reassemble a command line by completing the quoted command line, replacing the result, and reassembling the new command lineCopy the code

The Vim editor operates in four modes

1.Vim uses four modes. Normal mode, Command line mode, Insert mode, Visual mode

root@xuegod63 ~]# cp /etc/passwd a.txt
[root@xuegod63 ~]# vim a.txtEnter the file ---- for the first time, press I, and "Insert" appears. Insert mode press Esc, and then enter the colon: ---- Command-line modeCopy the code

Example 1 How to switch from edit mode to command line mode?

Edit mode -> ESC -> Command mode ->: -> COMMAND line Mode Note If the command input is invalid in command mode, check whether the input method is Chinese and switch to English

Example 2 character operation (how to enter edit mode?)

Enter edit mode a I o a I o

I Insert before the current character (before the cursor) I Insert at the beginning of the line (at the beginning of the line) a Insert after the current character (after the cursor) a Insert at the end of the line (at the end of the line) O Insert on the next line (after the cursor) o Insert on the previous line (after the previous line) x Delete a character backwards x Delete a character forwards is equivalent todeleteU Undo step every press to undo a CTRL + R restore, every press to restore an R replacementCopy the code

Operations performed in normal mode

1. Cursor positioning

HJKL lower left, upper right0The $and end keys switch to the end of the line. Gg quickly moves to the first line of the document. G moves to the end of the line3Gg or3G quickly locate to the3Line /string(string) ----- find or locate the word or content you are looking for and then hit Enter. If there are many matching content, we can use N, N to search up and down, and vim will highlight the content found, and cancel highlighting :noh /^d ---- Angle bracket ^ meaning to indicate what to start with, Find contents beginning with the letter d -----$means what ends, find contents ending with the letter bash /bash$vim + /etc/passwd When the file is opened, the cursor is automatically placed on the last line of the file. Learn about this technique. vim +23/etc/passwd When the file is opened, the cursor is automatically placed at the end of the file23Ok, convenient for late troubleshooting. For example: server startup error, the23There are grammatical errors. Using vim +23The /etc/passwd command can be used to quickly locate23Line.Copy the code

I prefer vim to open the file, then press G and skip to the end. Because of this, vim + A.txt will forget it for a while. There are so many tricks in Linux that you should remember the usual ones.

2. Edit the text in normal mode

Delete, copy, paste, undo YY copy the whole line copy N line: Nyy, for example:2Yy, for copy2Line DD (delete, in the unit of behavior, delete the current cursor line) Delete N line: Ndd, for example:2Dd: delete2Line p: p paste cut: Dd x delete the character where the cursor is D delete the character from the cursor to the end of the line u Undo CTRL +r Restore the undo action, restore the undo action that was done, that is, restore it back to what it was before undo r replace, or to modify a character summary: Vim How to enter other modes a a O O I I all can be inserted, edit mode: Enter command line mode CTRL + V enter visual block mode R erase, rewrite, enter replacement mode When you enter the above mode, want to exit, press EscCopy the code

Visual Mode Visual block mode

When programming or modifying server configuration files that require multiple lines of comments, use Visual mode. 1, enter the Visual mode of batch deletion, the method is as follows: Delete: then press CTRL + V to enter the Visual block mode; Move the cursor down or up; Select a section and press D to remove the annotated symbol.

For example, delete the # from line 17 to line 20 in the sshd_config file

[root@xuegod63 ~]# vim /etc/ssh/sshd_config
Copy the code

Change:To:2. Enter Visual mode for batch modification. The method is as follows:

1), CTRL + V to enter column edit mode 2), move the cursor down or up to select the beginning of the line you want to comment or edit 4), then press capital I 5), insert a comment or symbol you want to insert, such as “#” 6), then press Esc to comment or add an example: Put a # before lines 17 through 20 in the sshd_config file

[root@xuegod63 ~]# vim /etc/ssh/sshd_config
Copy the code

Change:To:

Command line Operation skills

1. Command line operation skills

:w Save :w! Force save :q has not changed anything, quit :q! Modified, do not save, forcibly exit :wq Save and exit :wq! Force save and exit :x Save and exit :e! Restore to the state when the file was opened and no changes were made. If you don't want to save it and want to restore it, press e! In normal mode, press capital ZZ to save and exitCopy the code

Example: wq! Forcibly save and exit

[root@xuegod63 ~]# ll /etc/shadow-- -- -- -- -- -- -- -- -- --.1 root root 1179 919 12:57 /etc/shadow
[root@xuegod63 ~]# vim /etc/shadow
Copy the code

Example 1: Invoking an external file or command

Syntax: In command line mode, type:! + Command example: In vim edit document write to write the MAC address. [root@xuegod63 ~]# cp /etc/passwd a.txt
[root@xuegod63 ~]# vim a.txt:! Ifconfig # Call the ifconfig command in vim to read other files. Append the contents of other files to the current document :r /etc/hostsCopy the code

2, text replacement format: range (where % all content) s separator old content separator new content (separator can be customized) default is the first word of each line that meets the requirements (/g all)

[root@xuegod63 ~]# vim a.txt
:1.3 s/bin/xuegod
:1.3 s/bin/xuegod/g
:3S /xue/aaaaa/g13The first bin in the line is replaced with xueGod #13Find all the bin in the line to replace with xueGod # only put the first3"Xue" = "aaaaa"doAnd the DO character :% s/do/xuegod/g
:% s/do/xuegod/gi # will text alldoReplace with xueGod # all of the textdoReplace with xuegod, and ignoredoCase of :% s@xuegod@do@g # replaces all the words xuegod in the text withdo, you can also use the @ delimiterCopy the code

Customize the Vim environment

1. Temporary Settings

[root@xuegod63 ~]# vim a.txt
:setNu sets the line number:setNonu Cancels line number setting :noh cancels highlightingCopy the code

2. Set the environment permanently

~/.vimrc # Create a.vimrc in the user's home directory. This only affects one user and does not create one yourselfCopy the code

Example example 1: Temporarily customizing VIM Enable the line number display function

[root@xuegod63 ~]# echo "set nu" >  /root/.vimrc
[root@xuegod63 ~]# vim /etc/passwd #
Copy the code

3. Vim opens multiple filesMethod 1: In this case, open two documents

[root@xuegod63 ~]# vim -o /etc/passwd /etc/hosts
Copy the code

Note: Enter: QA to exit all open files at once

Method 2: Open both documents left and right

[root@xuegod63 ~]# vim -O /etc/passwd /etc/hosts
Copy the code

Note: CTRL + Ww toggles the edit between two documents. The upper-case O split the left and right screen, and the lower-case O split the upper and lower screen

Compare the contents of two files

[root@xuegod63 ~]# cp /etc/passwd mima.txt
[root@xuegod63 ~]# echo aaa >> mima.txt
Copy the code

Method 1:

[root@xuegod63 ~]# diff /etc/passwd mima.txt
40a41 > aaa
Copy the code

Method 2:

[root@xuegod63 ~]# vimdiff /etc/passwd mima.txt
Copy the code

Other editors

Nano editor emacs editor GHOME editor Gedit example:

[root@xuegod63 ~]# gedit /etc/passwd
Copy the code

Actual: on Centos6/RHEL6 recover ext4 file system mistakenly deleted files

[root@xuegod63 ~]# rm -rf /Can this be executed successfully? If the command fails to be executed, rm: in"/"Rm: Use the --no-preserve-root option to skip safe mode [root@xuegod63 ~]# rm -rf /* # this can be executed successfully.Copy the code

If you delete a file from an ext4 file system, you can restore it: Extundelete, ext3 restore using: ext3grep Windows restore files deleted by mistake: Final Data V2.0 Chinese version and EasyRecovery XFS file system delete files, there is no good way to complete recovery, you need to find professional data recovery company extension:

The Linux file system consists of three parts: file name, inode, and block. Windows also consists of three parts. A.txt -->inode --> block Specifies the name of the file where metadata information is storedCopy the code

View file name:

[root@xuegod63 ~]# cp /etc/passwd a.txt
[root@xuegod63 ~]# ls a.txt  
a.txt
Copy the code

Check the inode number:

Common sense: Each file has an inode number.

[root@xuegod63 ~]# ls -i a.txt
440266 a.txt
Copy the code

View file properties in the inode; Run the stat command to view the inode contents

[root@xuegod63 ~]# stat a.txt
[root@xuegod63 ~]# ls -l a.txt
-rw-r--r-- 1 root root 1720 Oct 25 10:21 a.txt
Copy the code

Logical deletion: Why is deletion faster than replication?After deleting a file by mistake, what should I do first?? You don’t want to delete the stored decades of large delete. Avoid overwriting files that are deleted by mistake. How to avoid it? Unmount the partition where you want to restore files or mount it read-only

Actual: Recover files that have been mistakenly deleted on the ext4 file system

downloadextundelete sourceforge.net/Prepare the test partition: The OPEN Source software distribution center first adds a hard disk

[root@xuegod63 /]# fdisk /dev/sdb # create sdb1 partition
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help)Disk /dev/sda: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units= cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b8b35
Device Boot  Start  End   Blocks  Id  System /dev/sda1
* 1   204800  83   Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2   /dev/sda3
26  1301   1428   10240000  1024000  83
Linux
1301  82
Linux swap / Solaris
Command (m forCreate a new partition e extended p primary partition (14 -P # Create a Selected partition4
First cylinder (1428- 2610..default 1428):
Using default value 1428
Last cylinder, +cylinders or +size{K,M,G} (1428- 2610..default 2610) : +1G # specifies partition size Command (m)forHelp): w # save The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@xuegod63 ~]#rebootOr [root @ xuegod63 ~]# partx -a /dev/sdb # obtain new partition table
Copy the code

Extension: What if you want to restore a file that was deleted under the root? Method 1: Power off immediately, and then mount the disk in read-only mode to another computer for recovery Method 2: Put ExtunDelete on the virtual machine (the virtual machine system should be the same as the server version), and copy it to the USB disk after the installation, insert the USB disk into the server, and save the restored files to the USB disk (do not let the restored data write to /, which will overwrite the previously deleted files).

Using the new partition table:

[root@xuegod63 /]# mkdir/TMP /sdb1
[root@xuegod63 ~]Mkfs. ext4 /dev/sdb1
[root@xuegod63 ~]# mount /dev/sdb1 / TMP /sdb1
Copy the code

Preparing the test environment

Copy some test files, then delete them again, and then demonstrate recovery:

[root@xuegod63 ~]# cp /etc/passwd /tmp/sdb1
[root@xuegod63 ~]# cp /etc/hosts /tmp/sdb1
[root@xuegod63 ~]# echo aaa > a.txt
[root@xuegod63 ~]# mkdir -p /tmp/sdb1/a/b/c
[root@xuegod63 ~]# cp a.txt /tmp/sdb1/a
[root@xuegod63 ~]# cp a.txt /tmp/sdb1/a/b
[root@xuegod63 ~]# touch /tmp/sdb1/a/b/kong.txtInstall the tree command: [root@xuegod63 ~]# RPM - the ivh/MNT/Packages/tree - 1.5.3-2. El6. X86_64. RPM
[root@xuegod63 ~]# tree /tmp/sdb1/ TMP/sdb1 / ├ ─ ─ a ├ ─ ─ a.t xt └ ─ ─ b ├ ─ ─ a.t xt ├ ─ ─ c # empty directory └ ─ ─ kong. TXT file # empty ├ ─ ─ hosts ├ ─ ─ lost + found └ ─ ─ passwdCopy the code

Lost + Found is a directory created using the standard ext3/ext4 file system format. It is intended to be a directory where lost fragments can be placed in case of an error in the file system.

You can delete rm -rf lost+found and create mklost+foundCopy the code

Delete file:

[root@xuegod63 ~]# cd /tmp/sdb1/
[root@xuegod63 sdb1]# ls
a  hosts   lost+found    passwd
[root@xuegod63 sdb1]# rm -rf a hosts passwd
Copy the code

After deleting a file by mistake, what should I do first?? How do I avoid overwriting files that are mistakenly deleted? Unmount the partition where you want to restore files: or mount it read-only

[root@localhost ~]#cd /root
[root@localhost ~]# umount /tmp/sdb1
Copy the code

Install extundelet

Upload extunDelete to Linux:

To upload extunDelete from Windows to Linux, install xManager V5 or CRT [root@xuegod63 ~]# RPM - the ivh/MNT/Packages/LRZSZ - 0.12.20-27.1 el6. X86_64. RPMAfter installation, you have the rz command and the sz command rz: upload files from Windows to Linux Sz file name: download, transfer files from Linux to WindowsCopy the code

Unzip and install ExTundelet

[root@centos63~]#mount /dev/sr0 /mnt
[root@centos63~]# vim /etc/yum.repos.d/Centos-6.repo
[CentOS6]
name=CentOS-server
baseurl=file:///mnt
enabled=1
gpgcheck=0
[root@xuegod63]# yum -y install e2fsprogs-devel gcc gcc-c++ gcc-g77
[root@xuegod63 extundelete0.24.]# tar xf extundelete - 0.2.4. Tar..bz2
[root@xuegod63 ~]# CD extundelete - 0.2.4
[root@xuegod63 extundelete0.24.]#./configure # check the system installation environment [root@xuegod63 extundelete0.24.]# make
-j 4Compile the source code into an executable binary file. -j4use4Processes compile at the same time to speed up compilation or use4The core CPU is compiled simultaneously. [root@xuegod63 extundelete0.24.]# make installWhat is the difference between install and CP? Install Specifies permission to copy. Cp No. Example: [root@xuegod63 ~]# install -m 777 /bin/find /opt/find
[root@xuegod63 ~]# ll /opt/
Copy the code

Restore data

Method 1: Restore by inode node Method 2: restore by file name Method 3: restore all files in a directory such as directory A. Method 4: restore all files

[root@xuegod63 ~]# umount /tmp/sdb1/
[root@xuegod63 ~]# mkdir test # create a directory for storing the restored data
[root@xuegod63 ~]# cd test/
Copy the code

Method 1: View the name of the deleted file through the inode node:

[root@xuegod63 test]# extundelete /dev/sdb1 --inode 2
lost+found  11 
passwd     12   Deleted
hosts   13     Deleted
a  7313   Deleted
Copy the code

Extension: The inode value of the ext4 file system’s partition root directory is 2, and the inode value of the XFS partition root directory is 64

[root@xuegod63 test]# ls-id / # XFS filesystem
64  /
[root@xuegod63 test]# mount /dev/sdb1 /tmp/sdb1/
[root@xuegod63 test]# ls -id /tmp/sdb1/
2  /tmp/sdb1/
[root@xuegod63 test]# umount /tmp/sdb1/
Copy the code

Method 1: restore through the inode node

root@xuegod63 test]# extundelete /dev/sdb1 --restore-inode 12
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 9 groups loaded.
Loading journal descriptors ... 63 descriptors loaded.
[root@xuegod63 test]# ls
RECOVERED_FILES
[root@xuegod63 test]# diff /etc/passwd RECOVERED_FILES/file.12# does not have any outputCopy the code

Method two: restore by file name

[root@xuegod63 test]# extundelete /dev/sdb1 --restore-file hosts
[root@xuegod63 test]# diff /etc/passwd RECOVERED_FILES/hosts# does not have any outputCopy the code

Method 3: Restore all files in a directory, such as directory A:

[root@xuegod63 test]# extundelete /dev/sdb1 --restore-directory a
[root@xuegod63 test]# tree RECOVERED_FILES/a/[root@xuegod63 ~]# tree /root/sdb1-back/a// root/sdb1 - back/a / ├ ─ ─ a.t xt └ ─ ─ b ├ ─ ─ a.t xt ├ ─ ─ c └ ─ ─ kong. TXTCopy the code

Method 4: Restore all files

[root@centos6 test]# rm -rf RECOVERED_FILES/
[root@xuegod63 test]# extundelete /dev/sdb1 --restore-all
[root@centos6 test]# tree RECOVERED_FILES/Data before and after deletion:Copy the code

Practice: Use remote connection tools such as XManager to manage Linux

1. Introduction to common remote connection tools in Linux 2. How to use xManager Open after installation1. How to use XshellExample 1: Connect to a new serverExample 2: Resize the Xshell fontExample 3: Adjust the default path for the rz and sz commandsExample 1: Upload a folder to the Linux serverExample 1: [root@xuegod63 ~]# gnome-terminal Example 2: [root@xuegod63 ~]# firefox & Method 2: Call the desktop with xStart

Note: use MK for xmangaer5 after installation, late run, prompt update to the new version, you do not update, update, possible

The serial number is no longer usable. www.netsarang.com/zh/free-for… You can also download the official free version