Linux VIM editor and restoring files deleted by mistake under ext4 – xmanager tool

Time:2021-8-25

Introduction to main modes of vim

VIM command mode
Q: are VI and VIM installed in the same package?
A: No, VIM is an added version of VI. the most obvious difference is that VIM can be syntax highlighted. It is fully compatible with VI

Check a command, which software package is installed:

[[email protected] ~]# rpm -qf /usr/bin/vim
[[email protected] ~]# which vim
[ [email protected] ~]#RPM -qf ` which VIM ` # ` backquote, the key under the ESC key, and the command can be executed in the backquote
order
[[email protected] ~]# rpm -qf $(which vi)     #$(可执行命order)
$()  与`` 都可以进行命order替换,命order替换与变量替换差不多,都是用来重组命order行的,先完成引号里
的命order行,然后将其结果替换出来,再重组成新的命order行

Four operation modes of vim editor

1. VIM has four common modes
Normal mode (commonly known as command mode), command line mode
Insert mode (commonly known as edit mode), visual mode (commonly known as visual block mode)

[email protected] ~]# cp /etc/passwd a.txt
[[email protected] ~]# vim a.txt
Entering the file for the first time - normal mode (commonly known as command mode)
Press the I key to display "insert" --- insert mode (commonly known as edit mode)
Press ESC and enter the colon: --- command line mode

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

Edit mode – > ESC – > command mode – > command line mode
Note that in the command mode, when the input command is invalid, check whether the input method is Chinese input method and switch to English input method

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

Enter edit mode a i o a i o
explain:

I insert before current character (before cursor)
Insert at the beginning of line I (at the beginning of line)
A insert after current character (after cursor)
A end of line insert (end of line)
O insert next line (from another line)
O previous line insert (previous line insert)
X deletes a character backwards
X deletes a character forward
Equivalent to delete
U undo step
Undo every time you press
Press Ctrl + R to restore. Press Ctrl + R once to restore
R replacement

Operation in normal mode

1. Cursor positioning

hjkl
Lower left, upper right
The 0 and home keys indicate switching to the beginning of the line, and the $and end keys indicate switching to the end of the line
GG quickly navigate to the first line of the document, and G navigate to the non line
3GG or 3G fast positioning to line 3
/String (string) -- find or locate the word or content you are looking for, and then hit enter. If there are many matching contents, we can
To search up and down through N and N, and VIM will highlight the found content. To cancel the highlighting, use: Noh
/^d
----Angle brackets ^ mean what to start with. Find what starts with the letter D
-----$means what to end with. Find what ends with the letter bash
/bash$
VIM + / etc / passwd after opening the file, the cursor will automatically be on the last line of the file. Learn about this technique.
VIM + 23 / etc / passwd after opening the file, the cursor will automatically be located on line 23 of the file to facilitate later troubleshooting. E.g. service
The device starts with an error. On the 23rd, there is a syntax error. Use VIM + 23 / etc / passwd to quickly navigate to line 23.

Share my experience: I prefer VIM to open the file, then press g to jump to the end. Because of this VIM + a.txt
I’m sure I’ll forget it for a while. There are too many tips in Linux. You should remember those commonly used.

2. Edit text in normal mode

Delete, copy, paste, undo
YY copy entire line
Copy line n: NYY, for example: 2yy, which means copy 2 lines
DD (delete, in behavioral units, delete the line where the current cursor is located)
Delete n rows: NDD, such as 2dd, means to delete 2 rows
p: P paste
Cutting: DD
X deletes the character at the cursor position
D delete from cursor to end of line
u
Undo operation
ctrl+r
还原撤销过的操作,将做过的Undo operation再还原回去,也就是说撤销前是什What kind,再还原成什
What kind
R replacement, or used to modify a character
Summary: how VIM enters other modes
A, O, I, I can be inserted and edited
: enter command line mode
CTRL + V to enter visual block mode
R erase, rewrite and enter the replacement mode
After you enter the above mode and want to exit, press ESC

Visual mode visual block mode

When programming or modifying the server configuration file, multi line comments are required, and visual mode will be used.
1. Enter the batch deletion in visual mode as follows:
Delete: press Ctrl + V again to enter the visual block mode; Move the cursor down or up; Select part of the content and press D to delete the note
Interpretive symbols.

Example: set sshd_ Delete the # number before lines 17 to 20 in the config file

[[email protected] ~]# vim /etc/ssh/sshd_config

Change:
Linux VIM editor and restoring files deleted by mistake under ext4 - xmanager tool
Is:
Linux VIM editor and restoring files deleted by mistake under ext4 - xmanager tool
2. Enter the batch modification in visual mode as follows:

1) . Ctrl + V enter column editing mode
2) Move the cursor down or up to select the beginning of the line to be commented or edited
4) , then press capital I
5) , and then insert the comment or the symbol you need to insert, such as “#”
6) Press ESC again, and all comments or additions will be made
Example: in sshd_ Add a # sign before lines 17 to 20 in the config file

[[email protected] ~]# vim /etc/ssh/sshd_config

Change:
Linux VIM editor and restoring files deleted by mistake under ext4 - xmanager tool
Is:
Linux VIM editor and restoring files deleted by mistake under ext4 - xmanager tool

Command line operation skills in command line mode

1. Command line operation skills in command line mode

: W save
:w!   Force save
: Q quit without any modification
:q!   Modified, do not save, forced exit
: WQ save and exit
:wq!   Force save and exit
: X save exit
:e!   Restore: restore to the state when the file is opened without modification. A lot of changes, do not want to save, want to restore, press: e!
In the normal mode, press the uppercase ZZ to save and exit

Example: WQ! Force save and exit

[[email protected] ~]# ll /etc/shadow
----------1. Root 1179 September 19 12:57 / etc / shadow
[[email protected] ~]# vim /etc/shadow

Example 1: calling an external file or command

Syntax: in command line mode, enter:+ command
Example: write the MAC address to be written in the VIM edit document.
[[email protected] ~]# cp /etc/passwd a.txt
[[email protected] ~]# vim a.txt
:!ifconfig
The ifconfig command is invoked in vim.
Read other files( Append the contents of other files to the current document)
:r /etc/hosts

2. Text replacement
Format: range (where% all content) s separator old content separator new content (separator can be customized)
The default is the first qualified word in each line (/ g all)

[[email protected] ~]# vim a.txt
:1,3 s/bin/xuegod
:1,3 s/bin/xuegod/g
:3 s/xue/aaaaa/g
#Replace the first bin in lines 1 to 3 with xuegod
#Replace all the bin found in lines 1 to 3 with xuegod
#Just replace all Xue in line 3 with AAA
To modify a.txt, insert a few do and do characters into the file at will
:% s/do/xuegod/g
:% s/do/xuegod/gi
#Replace all do in the text with xuegod
#Replace all do in the text with xuegod, and ignore the case of do
:%  [email protected] @ [email protected] #Replace all xuegod in the text with do. When replacing, you can also use @ as the separator

Customize VIM usage environment

1. Temporary settings

[[email protected] ~]# vim a.txt
:set nu
set nu 
:set nonu  取消set nu 
:noh
set nohlsearch 

2. Permanent setting environment

VIM / etc / vimrc # setting will affect all users of the system
~/. vimrc # in the user's home directory, create a. Vimrc. This only affects a certain user and does not build one yourself

Example 1: temporarily customize VIM to enable the function of displaying line number

[[email protected] ~]# echo "set nu" >  /root/.vimrc
[ [email protected] ~]#VIM / etc / passwd # found that the default line number already exists

Linux VIM editor and restoring files deleted by mistake under ext4 - xmanager tool
3. VIM opens multiple files
Method 1: open two documents under the situation above and below

[[email protected] ~]# vim -o /etc/passwd /etc/hosts

Linux VIM editor and restoring files deleted by mistake under ext4 - xmanager tool

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

Method 2: open two documents left and right

[[email protected] ~]# vim -O /etc/passwd /etc/hosts

Note: Ctrl + WW switches between two documents for editing. The upper case o is divided into left and right screens, and the lower case o is divided into upper and lower screens

Compare the contents of two files

[[email protected] ~]# cp /etc/passwd mima.txt
[[email protected] ~]# echo aaa >> mima.txt

Method 1:

[[email protected] ~]# diff /etc/passwd mima.txt
40a41 > aaa

Method 2:

[[email protected] ~]# vimdiff /etc/passwd mima.txt

Other editors

Nano editor
Emacs editor
Ghome editor GEDIT
Example:

[[email protected] ~]# gedit /etc/passwd

Actual combat: restore files deleted by mistake under ext4 file system on centos6 / rhel6

[[email protected] ~]# rm -rf /
#Can this be executed successfully?
If the execution is unsuccessful,
RM: recursion at '/' is dangerous
RM: use the -- no preserve root option to skip safe mode
[ [email protected] ~]#RM - RF / * # this can be executed successfully.

ext4Delete files on the file system to recover:extundeleteext3Resume use:ext3grep
windowsRecover files deleted by mistake:final data v2.0Chinese version andeasyrecovery
xfsWhen files are deleted from the file system, there is no good way to restore them completely. You need to find a professional data recovery company
Extension:

Linux file system consists of three parts: file name, inode and block
Windows also consists of these three parts.
a.txt     -->inode    --> block
The file name stores the file metadata information and really stores the data

View file name:

[[email protected] ~]# cp /etc/passwd a.txt
[[email protected] ~]# ls a.txt  
a.txt

View inode number:

Common sense: each file has an inode number.

[[email protected] ~]# ls -i a.txt
440266 a.txt

View file attributes in inode; View the contents contained in the inode through the stat command

[ [email protected] ~]#Stat a.txt # view inode information:
[[email protected] ~]# ls -l a.txt
-rw-r--r-- 1 root root 1720 Oct 25 10:21 a.txt

Block block: the place where data is actually stored
Logical deletion:
Why is deletion faster than replication?
Linux VIM editor and restoring files deleted by mistake under ext4 - xmanager tool
Linux VIM editor and restoring files deleted by mistake under ext4 - xmanager tool
What is the first thing to do after deleting a file by mistake??? You don’t want to delete the blockbuster that has been saved for decades.
Avoid overwriting the contents of accidentally deleted files. How to avoid?
Unmount the partition that needs to recover files or mount it as read-only

Actual combat: recover files deleted by mistake on ext4 file system

Download * * extundelete
http://sourceforge.net/**
Prepare test partition:
Open source software distribution center
Add a hard disk first
Linux VIM editor and restoring files deleted by mistake under ext4 - xmanager tool

[ [email protected] /]#Fdisk / dev / SDB # creates an 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):  p
#View existing partition tables
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 for help):  n
Command action
#Create a new partition
e
extended
p
primary partition (1-4)
p
#Create a primary partition
Selected partition 4
First cylinder (1428-2610, default 1428):
Using default value 1428
Last cylinder, + cylinders or + size {K, m, G} (1428-2610, default 2610): + 1g # specifies the partition size
Command (m for help): W # save
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 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.
[[email protected] ~]#reboot
or
[ [email protected] ~]#PartX - A / dev / SDB # get new partition table

Extension:
What if you delete a file under the root and want to restore it?
Method 1: power off immediately, and then mount the disk in read-only mode to another computer for recovery
Method 2: put the extundelete on the virtual machine (the virtual machine system should be the same as the server version), install it in advance, and then copy it to the U
Insert the USB flash disk into the server. During recovery, the recovered files should be saved to the USB flash disk (do not write / download the recovered data, which will overwrite the previously deleted files)

Use the new partition table:

[ [email protected] /]#MKDIR / TMP / sdb1 # create mount point
[ [email protected] ~]#Mkfs.ext4 / dev / sdb1 # format
[ [email protected] ~]#Mount / dev / sdb1 / TMP / sdb1 # mount

Prepare test environment

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

[[email protected] ~]# cp /etc/passwd /tmp/sdb1
[[email protected] ~]# cp /etc/hosts /tmp/sdb1
[[email protected] ~]# echo aaa > a.txt
[[email protected] ~]# mkdir -p /tmp/sdb1/a/b/c
[[email protected] ~]# cp a.txt /tmp/sdb1/a
[[email protected] ~]# cp a.txt /tmp/sdb1/a/b
[[email protected] ~]# touch /tmp/sdb1/a/b/kong.txt
Install tree command:
[[email protected] ~]# rpm -ivh /mnt/Packages/tree-1.5.3-2.el6.x86_64.rpm
[[email protected] ~]# tree /tmp/sdb1
/tmp/sdb1/
├──  a
├──  a.txt
└──  b
├──  a.txt
C - C # empty directory
└ -- kong.txt # empty file
├──  hosts
├──  lost+found
└──  passwd

lost+found
A directory generated by using the standard ext3 / ext4 file system format. The purpose is to set a directory when an error occurs in the file system
Put some missing fragments into this directory.

RM - RF lost + found can be deleted
You can create mklost + found

Delete file:

[[email protected] ~]# cd /tmp/sdb1/
[[email protected] sdb1]# ls
a  hosts   lost+found    passwd
[[email protected] sdb1]# rm -rf a hosts passwd

What is the first thing to do after deleting a file by mistake???
How to avoid overwriting the contents of deleted files???
Unmount the partition where files need to be recovered: or mount it as read-only

[[email protected] ~]#cd /root
[[email protected] ~]# umount /tmp/sdb1

Install extundelet

Upload extundelete to Linux:

Upload the extundelete file from windows to Linux, and install xmmanager V5 or CRT
[[email protected] ~]# rpm -ivh /mnt/Packages/lrzsz-0.12.20-27.1.el6.x86_64.rpm
After installation, there are RZ and SZ commands:
Upload files from windows to Linux
SZ file name: Download and transfer the files in Linux to windows

Unzip and install extundelet

[[email protected]~]#mount /dev/sr0 /mnt
[[email protected]~]# vim /etc/yum.repos.d/Centos-6.repo
[CentOS6]
name=CentOS-server
baseurl=file:///mnt
enabled=1
gpgcheck=0
[[email protected]]# yum -y install e2fsprogs-devel gcc gcc-c++ gcc-g77
[[email protected] extundelete-0.2.4]# tar xf extundelete-0.2.4.tar.bz2
[[email protected] ~]# cd extundelete-0.2.4
[ [email protected] Extendelete-0.2.4]#. / configure # check the system installation environment
[[email protected] extundelete-0.2.4]# make
-J 4 # compilation, which compiles the source code into executable binary files.
-J 4 use 4 processes to compile at the same time to improve the compilation speed or use 4-core CPU to compile at the same time.
[ [email protected] Extundelete-0.2.4]# make install
What is the difference between install and CP?
You can specify permissions during install replication. CP cannot
Example:
[[email protected] ~]# install -m 777 /bin/find /opt/find
[[email protected] ~]# ll /opt/

Recover data

Method 1: restore through inode node
Method 2: restore by file name
Method 3: restore a directory, such as all files in directory a:
Method 4: restore all files

[[email protected] ~]# umount /tmp/sdb1/
[ [email protected] ~]#MKDIR test # creates a directory for storing recovered data
[[email protected] ~]# cd test/

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

[[email protected] test]# extundelete /dev/sdb1 --inode 2
lost+found  11 
passwd     12   Deleted
hosts   13     Deleted
a  7313   Deleted

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

[ [email protected] Test]# LS - ID / #xfs file system
64  /
[[email protected] test]# mount /dev/sdb1 /tmp/sdb1/
[[email protected] test]# ls -id /tmp/sdb1/
2  /tmp/sdb1/
[[email protected] test]# umount /tmp/sdb1/

Method 1: restore through inode node

[email protected] 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.
[[email protected] test]# ls
RECOVERED_FILES
[[email protected] test]# diff /etc/passwd RECOVERED_FILES/file.12
#No output, the description is the same

Method 2: restore by file name

[[email protected] test]# extundelete /dev/sdb1 --restore-file hosts
[[email protected] test]# diff /etc/passwd RECOVERED_FILES/hosts
#No output, the description is the same

Method 3: restore a directory, such as all files in directory a:

[[email protected] test]# extundelete /dev/sdb1 --restore-directory  a
[[email protected] test]# tree RECOVERED_FILES/a/
RECOVERED_FILES/a/
├──  a.txt
└──  b
└──  a.txt
The following is the original directory structure:
[[email protected] ~]# tree /root/sdb1-back/a/
/root/sdb1-back/a/
├──  a.txt
└──  b
├──  a.txt
├──  c
└──  kong.txt

Method 4: restore all files

[[email protected] test]# rm -rf RECOVERED_FILES/
[[email protected] test]# extundelete /dev/sdb1 --restore-all
[[email protected] test]# tree RECOVERED_FILES/
Data before and after deletion:

Linux VIM editor and restoring files deleted by mistake under ext4 - xmanager tool

Actual combat: use xmmanager and other remote connection tools to manage Linux

1. Introduction to common remote connection tools under Linux
Linux VIM editor and restoring files deleted by mistake under ext4 - xmanager tool
2. Usage of xmmanager
Linux VIM editor and restoring files deleted by mistake under ext4 - xmanager tool
Open after installation
Linux VIM editor and restoring files deleted by mistake under ext4 - xmanager tool
1. Xshell usage
Example 1: connect a new server
Linux VIM editor and restoring files deleted by mistake under ext4 - xmanager tool
Example 2: adjust xshell font size
Linux VIM editor and restoring files deleted by mistake under ext4 - xmanager tool
Example 3: adjust the default path of RZ and SZ commands
Linux VIM editor and restoring files deleted by mistake under ext4 - xmanager tool
2. Xftp usage
Example 1: upload a folder to a Linux server
Linux VIM editor and restoring files deleted by mistake under ext4 - xmanager tool
3. How to use xstart
Method 1: use xshell to directly run the program with graphical interface
Example 1:[ [email protected] ~]# gnome-terminal
Example 2:[ [email protected] ~]# firefox &
Method 2: use xstart to call the desktop
Linux VIM editor and restoring files deleted by mistake under ext4 - xmanager tool

Note: after using xmangaer5 installed by MK, it will run later and prompt to update to the new version. Do not update, update, it is possible
The serial number cannot be used.
https://www.netsarang.com/zh/…
You can also download the official free version

You guys want to learn video + notes and materials
                                 Add our little sister and you can get it!

Recommended Today

Supervisor

Supervisor [note] Supervisor – H view supervisor command help Supervisorctl – H view supervisorctl command help Supervisorctl help view the action command of supervisorctl Supervisorctl help any action to view the use of this action 1. Introduction Supervisor is a process control system. Generally speaking, it can monitor your process. If the process exits abnormally, […]