05Linux basis -vim editor and recover accidentally deleted files under ext4

1 vim main mode of presentation, vim command mode

1.1vim editor mode

  For the first time into the file -------- command mode

  Appears "INSERT" -------- edit mode

  Input: ------------------- command mode

A: How to switch to edit mode from the command line?

Edit mode -> esc-> Command Mode ->: -> Command Line Mode

B: Character Operation (how to enter the edit mode?)

Enter edit mode aio AIO

Description:

i Insert (before the cursor) prior to the current character

I insert the first row (first line)

After inserting a current character (the cursor)

A line is inserted into the end (end of line)

o inserting the next line (new line)

Insert the O line (one line insert)

x delete a character backward is equivalent to delete

X to delete a character forward     

u Undo one step each time you press it once revoked

r Replace

1.2 In order to do the operation mode:

  Position the cursor

  hjkl lower left on the right

0 home button and a handoff to the first line, and end keys $ handoff to the end of line

gg quickly navigate to the first line of the document, G is not targeted to the line

3gg or 3G to quickly locate row 3

/ String (string) ----- or locate words or find what you're looking for, if the contents are more consistent, we can be upwards by N, n look down, and vi will find the contents highlight, canceled by: noh

/ ^ D ---- ^ intention to begin with what ,, Find content beginning with the letter d

/ T $ ----- $ intention to what end ,, Find content ends with the letter t

vim + a.txt open the file after the cursor is automatically located in the last line

 

 

How to edit text

Delete, copy, paste, undo

y copy (in characters): represents a single character to copy, if you want to copy the entire line, with yy (in units)   

Copy N rows: Nyy, for example: 2yy, 2 represents copy OK

dd (delete, in units, delete the current cursor line)

Delete N lines: Ndd, such as: 2dd, means to delete two lines

p: P ​​Paste

Cut: dd

x delete the character at the cursor position

Remove from D point to the end of line

u undo

ctrl + r withdrawn through the reduction operation, the reduction operation was done undo back, that is to say what is revoked before, then reduced to what

r Alternatively, or used to modify a character

 

Summary: vim how to enter other modes

a A o O i I can all be inserted into edit mode

: Enter the command line mode

v to enter visual mode

ctrl + v enters Visual block mode

V line mode to enter Visual

R erase, rewrite, replacement mode enters

After you enter these modes, you want to quit , press esc

 

 

Operation of the insertion mode: Extended

ctrl + p can be completion, the contents must be required in the currently open file in the memory, it is only for the current file

 

 

1.3 V mode (column)

Entering v cursor movement mode selection area,

When programming the need for multi-line comments:
1) , Ctrl + v to enter column edit mode

2), to move the cursor up or down, the need to comment, to edit the selected beginning of the line up

4), and then press the uppercase I

5), then insert a comment you need to insert a symbol or symbols, such as "#"

6), press Esc, or will all add a comment

 

Delete: Press ctrl + v column enters edit mode; upward or downward to move the cursor  ; selected comment section and press d, the comment is deleted symbol.

 

1.4 Command line mode

: W save save

:! W Force Save

: q did not make any changes, exit quit

:! Q modified without saving, force-quit

: Wq to save and exit

:! wq  forced to save and exit

: X save and exit

Example :! Wq  forced to save and exit

[root@localhost ~]# ll /etc/shadow

----------. 1 root root 1179 9月  19 12:57 /etc/shadow

[root@localhost ~]# vim /etc/shadow

 

Call an external file or command

Assumptions: I want to write my network card MAC address, and I want to look at the current edit documents in vim, shining write. So good trouble.

In the command mode:

:! Ifconfig call system commands

! + Command

Read other files. (The contents of other documents appended to the current document)

:r /etc/hosts

 

Text Replacement

Formatrange (where % of all content) S old content delimiter new content separators (separators can be customized)

The default is the first to meet the requirements of each line word   (/ g all)

: 1,3 s / bin / xuegod replace the first bin in the line of 1-3 occur are replaced xuegod

: 1,3 s / bin / xuegod / g to replace the first three rows to find all of the bin is replaced xuegod

: 3 s / xue / aaaaa # only the third row replaces the

:% S / do / xuegod / g text do all replaced xuegod

:% S / do / xuegod / gi do all text replaced xuegod, and ignoring capitalization do

:% S @ a @ b @ g a text all replaced b

 

1.5 Customizing vim environment

Temporary settings

: Set nu set the line number

: Set nonu unset line number

: Noh Cancel Highlight

Permanently set the environment

After the / etc / vimrc vim settings will affect all users of the system

~ / .Vimrc # in the user's home directory, create a .vimrc. In this way affects only a single user, there is no built himself a

Example:

[root@localhost ~]# cat  /root/.vimrc  

not set

[root@localhost ~]# vim  /root/.vimrc

 

 

 

 

 

vim open multiple files

Method 1: Under the above circumstances, open two documents

[root@localhost ~]# vim -o /etc/passwd /etc/hosts

 

Method 2: Open two documents about

[root@localhost ~]# vim -O /etc/passwd /etc/hosts

Note: Ctrl + WW switch between two editing the document. Uppercase O split screen left and right, vertically split screen lowercase o

 

Compare the contents of two files

[root@localhost ~]# cp /etc/passwd mima.txt

[root@localhost ~]# echo aaa >> mima.txt

[root@localhost ~]# diff /etc/passwd mima.txt

40a41

> aaa

[root@localhost ~]# vimdiff /etc/passwd mima.txt

 

1.6 Other Editor

nano editor

emacs editor

GHOME editor gedit

Example:

[root@localhost ~]# gedit /etc/passwd

 

5.1.7 actual 1: The edited characters in a text document windows, open garbled uploaded to Linux.

Experimental environment: centos7.4 now use the system default language is Chinese. (The system must be installed Chinese package).

The same directory "a file is opened in windows normal - under linux vim to open is garbled .txt" uploaded to the Linux server. Use ssh to connect remotely on Linux, using vim open garbled.

The reason: an encoding problem.

By iconv command had not Transcoding: 1

parameter:

-f, --from-code = name of the original text encoding
-t, --to-code = output encoding
-o, --output = FILE output file name
[root @ localhost ~] # mkdir test # create a test directory

[root@localhost ~]# cd test/

Upload test files to the Linux server:

 

[Root @ localhost ~] # iconv -f gb2312 -t utf8 a normal file open in windows - to open the linux vim distortion .txt -o aa.txt

[root@localhost ~]# cat aa.txt

#!/bin/bash

echo "学神IT"

Information:
the -l, --list lists all known character sets

5.1.8 combat 2: solve the pilot script on the company's Linux server to open the windows serial issue

The reason: because the windows and linux processing Enter different methods.

Upload "b in Linux edited document to the windows does not wrap .sh"  to normal on Linux, the open display

[Root @ localhost test] # sz b in Linux edited document to the windows does not wrap to the local .sh #

In the open display on the window:

 

Solution:

[root@localhost ~]# rpm -ivh /mnt/Packages/dos2unix-6.0.3-7.el7.x86_64.rpm

NOTE: In the centos7, unix2dos this command has been integrated into dos2unix-6.0.3-7.el7.x86_64.rpm package. In the case centos6 need to install unix2dos.xxx.rpm.

[Root @ localhost test] # unix2dos b in Linux edited document to the windows does not wrap .sh

[Root @ localhost test] # sz b in Linux edited document to the windows does not wrap .sh # sent to the local display windows properly.

 

Note: dos2unix  this command is to carriage under the windows turn into linux type.

 

2 combat: to restore accidentally deleted files under ext4 file system on Centos6 / RHEL6

 

[Root @ localhost ~] # rm -Rf / # unsuccessful execution,

rm: recursive operation "/" very dangerous

rm: Use --no-preserve-root option to skip Safe Mode

[Root @ localhost ~] # rm -rf / * # that can be executed successfully. Ha ha. . .

 

Delete files on ext4 file system, you can restore: extundelete, ext3 recovery use: ext3grep

windows recover accidentally deleted files: final data v2.0 finished version and easyrecovery  

 

Extended:

Linux file system consists of three parts: a file name, inode, block

windows also consist of three parts.

a.txt          -->inode              --> block

File name       store file metadata information       actually stored data

View File name:

[root@localhost ~]# cp /etc/passwd a.txt

[root@localhost ~]# ls a.txt

a.txt

 

View inode number:

Common sense: Each file has a inode number.

[root@localhost ~]# ls -i a.txt

440266 a.txt

View inode file attributes; view the contents of the inode contained by stat command

[Root @ localhost ~] # stat a.txt # View inode information:

[root@localhost ~]# ls -l a.txt

-rw-r--r-- 1 root root 1720 Oct 25 10:21 a.txt

 

block blocks: the real place to store data

Tombstone:

Why remove the copy faster than?

 

 

 

After accidentally deleted files, What is the first thing to do? ? ?  You do not mind delete the saved decades of blockbuster deleted.

Avoid accidentally deleted content file is overwritten. How to avoid?

Uninstall the need to restore a file or partition read-only way to mount

 

5.2.2 combat: undelete files on ext4 file system

Download extundelete

http://sourceforge.net/     open source software distribution center

Ready to test partition:

[Root @ localhost /] # fdisk / dev / sda # create a partition sda4

 

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 table

 

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          26      204800   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2              26        1301    10240000   83  Linux

/dev/sda3            1301        1428     1024000   82  Linux swap / Solaris

 

Command (m for help): n # to create a new partition

Command action

   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 # partition size specified

 

Command (m for help): w  #保存

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.

[root@localhost ~]#reboot

or

[Root @ localhost ~] # partx -a / dev / sda # obtain new partition table

Extended:

If you delete a file in the root, you want to restore, how to do?

Method 1: off immediately, and then the read-only disks, mounted to another computer for recovery

Method 2: The extundelete on a virtual machine (VM systems and servers to the same version), installed in advance and then copied to the U disk, U disk into the server, recovery, file recovery to be saved to the U disk, (Do not let writes data recovery / lower, as previously deleted file is overwritten)

 

Use the new partition table:

[Root @ localhost /] # mkdir / tmp / sda4 # create the mount point

[Root @ localhost ~] # mkfs.ext4 / dev / sda4 # Format

[Root @ localhost ~] # mount / dev / sda4 / tmp / sda4 / # mount

 

5.2.3 Copy some test files, and then delete the files, and then resume the presentation:

[root@localhost ~]# cp /etc/passwd /tmp/sda4/

[root@localhost ~]# cp /etc/hosts /tmp/sda4/

[root@localhost ~]# echo aaa > a.txt

[root@localhost ~]# mkdir -p /tmp/sda4/a/b/c

[root@localhost ~]# cp a.txt /tmp/sda4/a/

[root@localhost ~]# cp a.txt /tmp/sda4/a/b/

[root@localhost ~]# touch /tmp/sda4/a/b/kong.txt

Installation tree command:

[root@localhost ~]# rpm -ivh /mnt/Packages/tree-1.5.3-2.el6.x86_64.rpm

[root@localhost ~]# tree /tmp/sda4/

/tmp/sda4/

├── a

│   ├── a.txt

│   └── b

│       ├── a.txt

│ ├── c # empty directory

│ └── kong.txt # empty file

├── hosts

├── lost+found

└── passwd

 

Delete Files:

[root@localhost ~]# cd /tmp/sda4/

[root@localhost sda4]# ls

a  hosts  lost+found  passwd

[root@localhost sda4]# rm -rf a hosts passwd

 

After accidentally deleted files, What is the first thing to do? ? ?

How to avoid accidentally deleted file contents are covered? ? ?

Uninstall need to recover files partition: mount or read-only manner

[root@localhost ~]#cd /root

[root@localhost ~]# umount /tmp/sda4

5.2.4 Installation extundelet  

Upload extundelete to linux in:

From uploading extundelete windows files to linux, installation xmanager v5 or CRT

[root@localhost ~]# rpm -ivh /mnt/Packages/lrzsz-0.12.20-27.1.el6.x86_64.rpm  

After installation, there will be a command rz and sz command

rz: upload windows to linux files

sz: download the files to linux in windows

 

Unzip and install extundelet

[root@localhost extundelete-0.2.4]# tar jxvf extundelete-0.2.4.tar.bz2

[root@localhost ~]# cd extundelete-0.2.4

[root@localhost]# yum install e2fsprogs-devel

[Root @ localhost extundelete-0.2.4] # ./configure # inspection system installation environment

[Root @ localhost extundelete-0.2.4] # make -j 4 # compile the source code into executable binary files.

-j 4    using 4 processes simultaneously compile and enhance compilation speed or 4-core CPU while compiling.

[root@localhost extundelete-0.2.4]# make install  #安装

 

install and cp What is the difference?

When the install replication can not assign permissions cp

Example:

[root@localhost ~]# install -m 777 /bin/find /opt/a.sh

[root@localhost ~]# ll /opt/

5.2.5 began to recover:

Method 1: Recovery by inode node

Method two: Restore the file name

Method three: to restore a directory, such as a directory of all the files in a:

Method four: to restore all files

 

[root@localhost ~]# umount /tmp/sda4/

[Root @ localhost ~] # mkdir test # Create a directory used to store data recovery

[root@localhost ~]# cd test/

 

Method 1:

By viewing the file name of the deleted inode node:

[root@localhost test]# extundelete /dev/sda4 --inode 2

.                                                2

lost+found                                        11

passwd                                            12             Deleted

hosts                                             13             Deleted

a                                                 7313           Deleted

Extended: the inode root partition ext4 file system is 2, inode xfs root partition is 64

[Root @ localhost test] # ls -id / boot / #xfs file system

64 /boot/

[root@localhost test]# mount /dev/sda4 /tmp/sda4/

[root@localhost test]# ls -id /tmp/sda4/

2 /tmp/sda4/

[root@localhost test]# umount /tmp/sda4/

 

Method 1: by recovery inode node

[root@localhost test]# extundelete /dev/sda4 --restore-inode 12

NOTICE: Extended attributes are not restored.

Loading filesystem metadata ... 9 groups loaded.

Loading journal descriptors ... 63 descriptors loaded.

[root@localhost test]# ls

RECOVERED_FILES

[Root @ localhost test] # diff / etc / passwd RECOVERED_FILES / file.12 # no output, as described

The second method to restore the file name

[root@localhost test]# extundelete /dev/sda4 --restore-file passwd

[Root @ localhost test] # diff / etc / passwd RECOVERED_FILES / passwd # no output, as described

Method three: to restore a directory , such as a directory of all the files in a:

[root@localhost test]# extundelete /dev/sda4 --restore-directory a

[root@localhost test]# tree RECOVERED_FILES/a/

RECOVERED_FILES/a/

├── a.txt

└── b

└── a.txt

Here is the original directory structure:

[root@localhost ~]# tree /root/sda4-back/a/

/root/sda4-back/a/

├── a.txt

└── b

    ├── a.txt

    ├── c

└── kong.txt

 

Method four: to restore all files

[root@localhost test]# extundelete /dev/sda4 --restore-all

Before and after the deletion of data:

 

Guess you like

Origin www.cnblogs.com/hackerlin/p/11270553.html