Basic Introduction to Linux [Part 4], basic introduction to linux

Source: Internet
Author: User
Tags inode usage egrep

Basic Introduction to Linux [Part 4], basic introduction to linux

Attributes and permissions of Linux Files And Directories

Command:

Column 1: inode (index node) index node number, which uniquely identifies a file or directory on a disk, in linux, you must first read the index node of the file (similar to the book directory ).

Column 2:

1st characters: file type,-common file, d directory. (Windows .jpg,. avi)

2-10 characters rw-r --: File Permission (r (read) read permission, w (write) write Permission, x (execute) Execution permission,-no permission)

11th characters. Related to SELINUX.

Column 3: Number of hard links to a file. File hard links are another entry to the file. Same as hard link inode.

Column 4: owner or user of the file.

Column 5: group or user group (group) corresponding to the file ).

Permission relationship diagram:

Column 6: file or directory size.

Column 9: Last file modification time.

Column 10: file or directory name.

Inodeinode Overview

Partition the hard disk, format it, and create a file system.

Inode: index node ). After each partition of a linux storage device or storage device (the storage device can be a hard disk, a floppy disk, or a USB flash disk) is formatted as an ext4 file system, there are generally two parts: the first part is inode (many), and the second part is block (many ).

Block is used to store actual data, such as photos, videos, and other common file data. Inode is used to store the data Property Information (that is, the result of ls-l). inode includes the file size, host, user group, read/write permission, and file type, the modification time also contains the pointer to the object (inode node-block ing), but inode does not contain the file name.

In addition to recording file attribute information, inode also performs information indexing for each file, so there is an inode value. Based on commands, the operating system can find the corresponding file entity with the fastest inode value. For relations between files, inode, and block, see:

View inode size:

View inode size:

View the disk inode usage:

View the usage of the mounted disk block:

Summary of inode:

1. A certain number of inodes and blocks are generated after the disk partition is formatted into the ext4 file system.

2. inode index nodes are used to store the file attributes and act as the file index (pointing to the object of the file ).

3. The block in the ext3/ext4 file system stores the actual content of the file.

4. inode is a storage space. The default inode size of the c6 non-startup partition is 256 bytes, and c5 is 128 bytes.

5. inode is a string of numbers. inode (a string of numbers) corresponding to different files is unique in the file system.

6. files with the same inode number are hard-linked to each other (another file entry ).

7. A file must occupy at least one inode and one block after it is created.

8. The block size is generally 1 k, 2 k, and 4 k. The pilot partition is 1 k, and other common partitions are 4 k (c6 ).

9. If a file is large, it may occupy multiple blocks.

10. If the file is small, it occupies at least one and the remaining space cannot be used by other files.

11. View inode size and total quantity.

12. Run the df-I command to view the total inode and inode usage.

13. Generate and specify the inode size mkfs. ext4-B 2048-I 256/dev/sdb1.

Block knowledge:

1. Data read from the disk is read by block.

2. A file may occupy multiple blocks. Each read block consumes one disk I/O.

3. To improve disk I/O performance, read as much data as possible at once.

4. A block can only store the content of one file, no matter how small the content is. If the block is 4 k, 1 k of files will be saved, and the remaining 3 k will be wasted.

5. The larger the block, the better. The block size is too large, which wastes disk space for storing small files. For example, a file of 250 KB uses 4 k blocks, 1000 blocks, 1 k blocks, and blocks. The I/O consumption is 250 and 1000 respectively.

6. Large files (larger than 16 k) are generally set to a larger block, while small files (smaller than 1 k) are generally set to a smaller block.

7. The block size is too large, for example, 4 k. The file size is 0.1k, which wastes a lot of disk space.

8. When block is set to format partitions, mkfs. ext4-B 2048-l 256/dev/sdb.

9. When the file size is large, setting a larger block will increase the disk access efficiency.

10. The ext3/ext4 file is generally set to 4 k.

The current production environment is generally set to 4 k. Special services such as videos can increase the block size.

File Types and extensions in linux

In linux, everything is a file. File types include common files, directories, character device files, block device files, and symbolic link files.

Regular file: The first attribute is "-". According to the file content, there are three types of attributes:

1. plain text file (ascii): The file content can directly read data, such as letters and numbers. You can use the cat command to read the file content.

2. binary: in linux, executable files (commands) belong to this format. For example, the cat execution file is a binary file.

3. data Files: Some programs read certain files in specific formats during running. These files can be called data files. For example, when a user logs on to linux, the login data is recorded in the/var/log/wtmp file, which is a data file. Read it by using the last command. The cat command reads garbled characters. Because it belongs to a special format file.

Summary of file types:

1.-regular file: Common file

Plain text, binary, and data files.

2. d directory.

3. l link symbolic link or soft link file.

Shortcut, pointing to the object of the file.

4. character files and block Device Files

C character serial port device, cat

B block hard drive and optical drive.

5. Communication between sock Processes

S sock

6. MPS queue files

P

Functions of linux Extension

1、source code .tarw.tar.gzw..tgzw..zipw.tar. bz indicates the compressed file, and create commands such as tar, gzip, and zip.

2.. sh indicates a shell script file, a program developed in shell language.

3. pl indicates a perl language file, which is a program developed using the perl language.

4.. py indicates a python file, which is a program developed using the python language.

52.16.html#.htm,. php,. jsp, And. do indicate the webpage language files.

6.. conf indicates the configuration file of the system service.

7. rpm indicates the rpm installation package file.

Linux system hard links and soft links

In linux, there are two types of links: Hard Link and Soft Link ).

Create a hard link: ln source file target file

Create soft link: ln-s source file target file (the target file cannot exist beforehand)

Hard Link

Hard link refers to the link through the index node (Inode. In linux (ex2, ex3, and ex4) file systems, files stored in the disk partition are assigned a number no matter what type they are. This number is called index inode) inode is the number of the file in the system.

In a linux File System, it is normal and allowed to direct multiple file names to the same index node (inode. In this case, files are called hard links. Tip: a hard link file is equivalent to another file entry. One of the functions of hard links is to allow a file to have multiple valid path names (multiple entries), so that the snapshot function in user storage applies this principle, adding a snapshot adds a hard link.

Why does setting up a hard link to a file prevent accidental deletion of data?

Because the principle of the file system (ex2) is that as long as the file index node (inode index) has more than one hard link. Deleting only one of the hard links (that is, deleting only the links to the file) does not affect the index Node itself and other links (that is, the data file entity is not deleted ), only when the last link of the file is deleted, if there is new data to be stored on the hard disk or the system checks the disk through a disk similar to fsck. The data block and directory link of the deleted file will be released, and the space will be occupied and overwritten by new data. At this time, the data can no longer be retrieved. In other words, in linux, the condition for deleting static files (no process call) (directories are also files) Is that all hard-link files related to the files are deleted.

Hard link schematic:

Hard link:

Hard link summary:

1. Multiple files with the same inode node number are hard-linked files.

2. delete a hard-link file or delete any source file. The object is not deleted.

3. Only the source file and all corresponding hard-link files are deleted.

4. When all the hard-link files and source files are deleted and new data is stored, the space of the files will be occupied or the disk fsck will be checked, the deleted data will also be recycled by the system.

5. A hard-link file is another entrance to the file (equivalent to the supermarket's front door and backdoors ).

6. You can set hard-link files for files to prevent accidental deletion of important files.

7. Run the "ln source file hard link file" command to create a hard link.

8. You can use the rm command to delete a hard-link file.

9. For static files (files that no process is calling), the files are deleted when the number of hard links is 0 (I _link. I _link (the third column of ls-l results ).

Soft link

Soft Link is also called Symbolic Link ). Linux soft-link files are similar to shortcuts in windows. The soft link file in linux is actually a special file, the file type is I. A soft link file can be understood as a text file, which contains information about the location where the soft link points to another source file. Therefore, by accessing this "shortcut", you can quickly locate the source object pointed to by the soft link.

Soft link schematic:

Note: creating a soft link file must exist. The soft link file to be created cannot exist. Use the ln command to create a soft link file.

Soft link summary:

1. Soft links are similar to windows shortcuts (you can view them through readlink ).

2. Soft links are similar to a text file, which stores the path of the source file and points to the source object.

3. Delete the source file. The soft link file still exists, but cannot access the content of the source file path.

4. When it fails, it is usually indicated by a white-text flash.

5. Run the "ln-s source file soft link file" command to create a soft link (the target cannot exist ).

6. Soft links and source files are different types of files, but also different files, with different inode numbers.

7. Run the rm command to delete soft link files.

Directory Creation soft link:

Conclusion: After the oldboyfile of the original file is deleted, the oldboyfile_hard_link file of the hard link file is not affected and the corresponding data still exists. However, the corresponding soft link file oldboyfile_soft_link is invalid and the source file cannot be found, in this case, you can delete the soft link file and recreate it, or create an oldboyfile_hard_link hard link file oldboyfile.

Enterprise Production soft links:

1. Specify the version number (/application/apache2.2.23) when compiling the software. If you want to remove the version number (/application/apache) when accessing the software, you can set the soft link to the compiling path. All programs access the soft link file (/application/apache). After the software is upgraded to a higher version, you only need to delete the soft link from the linked file and recreate it to the later version path (/application/apache ).

2. When enterprise code is released (php Program), all code must be uploaded to a newly released temporary directory or a new site directory. You can either use a music video or recreate a soft link to the new temporary directory or site directory.

3. It is inconvenient to move directories. ln-s is used.

Summary of file links

1. Delete the soft link oldboyfile_soft_link, which has no effect on oldboyfile and oldboyfile_hard_link;

Global conclusion: deleting soft-link files has no effect on the source files and hard-link files.

2. Deleting the hard link oldboyfile_hard_link does not affect oldboyfile or oldboyfile_soft_link;

Global conclusion: deleting a hard-link file does not affect the source file or soft-link file.

3. Deleting the source file oldboyfile does not affect the hard link oldboyfile_hard_link, but it will invalidate the soft link oldboyfile_soft_link;

Global conclusion: deleting the source file does not affect the hard-link file, but it will cause the soft-link file to become invalid and the file will flash in red and white.

4. Delete the source file oldboyfile and the hardlink file oldboyfile_hard_link. The entire file will be deleted.

5. the snapshot function of many hardware devices uses the hard link principle.

6. The source file and the hard-link file have the same index node number and can be considered as multiple entries of the same file or file.

7. The index node numbers of source files and soft-link files are different. Soft-link is equivalent to the shortcut of source files and contains the location of the source files.

Summary of directory links:

1. Hard links cannot be created for directories, but soft links can be created.

2. Soft links to directories are commonly used in O & M in production scenarios.

3. Hard links of directories cannot be cross-file systems (you can understand the principle of hard links ).

4. Each directory contains a hard link "." And a hard link ".." corresponding to the parent directory "..".

5. Create a sub-directory in the parent directory, and Add 1 to the number of connections in the parent directory (sub-directories contain... to direct to the parent directory ). However, the number of links in the parent directory does not increase when files are created in the parent directory.

Principles of File Deletion in linux

The file name in linux exists in the parent directory block and points to the inode node of the file. The inode node of the file then marks the data block that points to the block where the file is stored. Deleting an object is not clear about inode nodes and block data. Delete the file name in the block in the parent directory of the file, so that the file name disappears and cannot point to the inode node of the file, when no file name points to this inode node, the inode node and the data block that stores this file are released at the same time, update inode MAP and block MAP so that these locations can be used to place other file data in the future.

How files are deleted in linux

File Deletion control variables:

I _link: number of file hard links

I _count: reference count (one program uses I _count plus 1)

File Deletion conditions:

I _link = 0 and I _count = 0

Note: I _link = 2 and I _count = 1.

In linux, the number of links is used to control file deletion. Only when a file does not have any links will the file be deleted. Generally, each file has two link counters, I _link and I _count.

I _count indicates the number of users (or called) of the current file, and I _link indicates the reference counter of the disk. When a file is referenced by a process, the number of I _count files increases. When a hard link is created, the number of I _links increases.

For the DELETE command rm, it is actually to reduce the disk reference count I _link. There is a problem here. If a file is being called by a process, but the user deletes the file by executing the rm operation, what will happen?

After you delete a file by executing the rm operation, you cannot find the file by executing the ls or other file management commands. However, the process that calls the deleted file continues to run normally, you can still correctly read and write content from the file. Why?
This is because the rm operation only reduces the I _link of the file. If there is no other link, I _link is 0, but the file is still referenced by the process. Therefore, the I _count corresponding to this file is not 0, so even if you perform the rm operation, the system does not actually Delete this file. When both I _link and I _count are 0, this file will be deleted. That is to say, you also need to cancel the process's call to the file.

I _link and I _count are the true conditions for object deletion. But when the object is not called, can the deleted object be retrieved after the rm operation is executed?

The rm operation only reduces the I _link of the file, or sets it to 0. The file name is actually deleted from the inode link, the object (block data block) of the file is not deleted. At this time, if the machine stops working in time, the data can be retrieved. If the data is written at this time, then the new data may be allocated to the block data block of the deleted data, and the file will be actually recycled.

Experiment: inode depletion.

View the new disk:

Format:

Cancel automatic check:

Mount:

View inode:

Create 26220 files:

The above shows that inode is exhausted and files cannot be created.

Insufficient disk space:

Inode depletion:

Delete:

View inode:

Conclusion: The ln command does not allow you to create hard links to directories, but you can create soft links to directories. Soft links to directories are also a common function in the O & M of production scenarios.

Linux users and user groups

Linux User segmentation:

1. superuser

Root uid = 0 gid = 0.

2. Virtual users

There is no practical significance in linux, and it is created to meet the needs of files or programs. You cannot log on or use it. The UID and GID ranges from 1 to 499. It ends with nologin at the end of/etc/passwd.

3. Common users

User Created by the administrator root. UID and GID start from 500.

In linux, the account files include/etc/passwd,/etc/shadow,/etc/group, and/etc/gshadow.

Brief description of each field in a line in the passwd file

Account name: account password: Account UID: account group GID: User Description: user's home directory: shell interpreter

Detailed descriptions of each field in a row in the shadow file

Field name

Description

Account name

User Account Name

Account Password

User Password. This is an encrypted password.

Last Password Change Time

From January 1, January 1, 1970 to the number of days the user recently changed the password

Days of password modification prohibited

From January 1, January 1, 1970 to the number of days that users can change their passwords

Number of days the user must change the password

From January 1, January 1, 1970 to the number of days that the user must change the password

Warning Password Change Period

How many days before the user password expires to remind the user to change the password

No activity time

The number of days after the user password expires until the account is disabled

Expiration time

From January 1, January 1, 1970 to the number of days the user is disabled (useradd-f)

Flag

Retained

 

Ls-l: The display time is the modification time.

File time:

Access: 20:05:12. 647999994 + 0800 (Access time) find-atime

Modify: 16:02:10. 162999297 + 0800 (modification time, content changed) find-mtime

Change: 16:02:10. 162999297 + 0800 (Change Time, including Modify, permission, owner, user group) find-ctime

Stat command to view time and other attributes

Linux Regular Expression

Notes for using regular expressions:

1. linux regular expressions are generally processed in behavior units.

2. Note that the character set export LC_ALL = C.

Basic regular expression: metacharacter meaning BRE (basic regular expression ).

1. ^ search for words starting with word. Vi ^ the beginning of a row.

2. Search for words ending with word. Vi $ end of a row.

3. ^ $ indicates empty rows.

4. represents and can only represent any one character.

5. \ for example, \. Only represents the vertex itself, and the escape symbol, so that characters with special identities can be removed from the vest to restore the prototype.

6. * For example, o * repeats 0 or multiple first characters.

7.. * match all characters. ^. * Starts with any number of characters.

8. [abc] matches any character in the character set combination [a-zA-Z] and [0-9].

9. [^ abc] matches any character that does not contain ^.

10. a \ {n, m \} repeats the previous repeated characters from n to m. If you use egrep, grep-E, or sed-r, you can remove the diagonal lines.

\ {N ,\} repeats at least n previous repeated characters.

\ {, M \} is invalid

\ {N \} repeats the previous character for n times.

Extended regular expression:

1. + repeat one or more previous characters.

2 ,? Repeat 0 or a character before 0.

3. | search for Multiple matching strings by using the or method.

4. () Find the "user group" string.

Set the display color of the alias filter content:

Filtering starts with m

Filtering ends with m

Filter empty rows

Filter empty rows to display row numbers

Match ended.

Exact match-o

Common grep parameters:

-A: search data in binary files as text files.

-C: calculates the number of times that "search string" is found.

-O: displays only the content that matches the regexp (used to count the number of occurrences in the text ).

-I: Case sensitivity is ignored, and all cases are considered the same.

-N: displays the row number at the beginning of the row.

-V: reverse selection, that is, the row without "search string" is displayed.

-E: Extended grep and egrep.

-- Color = auto: the matching keyword is highlighted in a specific color.

-A: After indicates that the data that matches the string and the next n rows is displayed.

-B: Before indicates the data that matches the string and the first n rows.

-C: displays the matching string and the num rows before and after it.

Retrieve the IP Address:

Method 1:

Method 2:

Retrieve HWaddr:

Method 1:

Method 2:

Stat/etc/hosts to retrieve 0644:

Related Article

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.