Introduction to Linux File System

Source: Internet
Author: User

File system is a basic knowledge of Linux and a necessary knowledge of Linux.

This article will take a high-level view to understand the Linux File System, this section describes Linux disk partitions and directories, basic mounting principles, file storage structures, soft links, and common directories. I believe that having these knowledge will be of some help for the in-depth study of Linux. The example is based on the Ubuntu release.

If there are any mistakes, please point them out more.

1. Linux disk partitions and directories

There are few differences between Linux releases. The differences are mainly manifested in the special tools of system management and the different management methods of software packages. The directory structure is basically the same. Windows File structure is a parallel tree structure with different disks (partitions) at the top, such as C, D, E, and F.

The file structure of Linux is a single tree structure. tree can be used for display. Install the tree (SUDO apt-Get install tree) in Ubuntu and run the command to view it.

Every time we install the system, we will partition. The relationship between disk partitions and directories in Linux is as follows:

-Any partition must be mounted to a directory.

-Directories are logically differentiated. Partitions are physically differentiated.

-All Linux partitions on a disk must be mounted to a specific directory in the directory tree for read and write operations.

-The root directory is where all Linux Files And Directories are located. You need to mount a disk partition.

The following is a possible directory and partition relationship:


Figure 1: Directory and partition relationships

Q: How can I view partitions, directories, and usage?

-Fdisk: view the hard disk partition table

-DF: View partition usage

-Du: view the space occupied by the file

Q: Why partition? How to partition?

-You can manage different data in different partitions to reduce risks.

-Large Hard Disk search range, low efficiency

-Disk cooperation can only be used to set partitions

-/Home/var/usr/local is often partitioned separately, because operations are often performed and fragments are easily generated.

 

2. Introduction to mount mounting and NFS

Concept of mounting: When you want to use a device, for example, to read a formatted partition, CD, or software in the hard disk, you must first map these devices to a directory, this directory is called "mount point" to read these devices. The corresponding actions are "Mount ". Blocks physical partition details. Users only have a unified logical concept. Everything is a file. Mount command:

Mount [-fnrsvw] [-T vfstype] [-O options] device dir

Q: How can I mount all the hard disk partitions on my machine before they can be used?

A: This is mainly because it uses the/etc/fstab file. Each time the kernel loads it, the file system is mounted from here. Each time the system starts, it is automatically mounted according to the file definition. Partitions cannot be used if they are not automatically mounted. The following is my/etc/fstab definition, mainly based on the installed partition:

# <File system> <mount point> <type> <Options> <dump> <pass>

Proc/proc defaults 0 0

#/Dev/sda1 is automatically mounted/

UUID = cb1934d0-4b72-4bbf-9fad-885d2a8eeeb1/ext3 relatime, errors = remount-ro 0 1

#/Dev/sda5 is automatically mounted to the partition/home

UUID = c40f813b-bb0e-463e-aa85-5092a17c9b94/home ext3 relatime 0 2

#/Dev/sda7 is automatically mounted to/work

UUID = 0f918e7e-721a-41c6-af82-f92352a568af/work ext3 relatime 0 2

# Partition/dev/sda6 is automatically mounted to swap

UUID = 2f8bdd05-6f8e-4a6b-b166-12bb52591a1f none swap SW 0 0

 

Q: How to mount a mobile hard disk? How to mount a new partition?

A drive module of a mobile hard disk is automatically mounted. If a new hard disk exists, you must first partition it and mount it to a folder by running the mount command. To automatically mount the file, you can modify the/etc/fstab file.

NFS introduction:NFS is believed to be widely used in many places and is a very good way to share files. The upload service used by our company is to upload files to a network server, which is implemented through NFS in the middle.

The NFS client can transparently access files on the server. NFS is also implemented through mount, and the underlying layer is implemented through the NFS communication protocol. Basic Principles:

Figure 2: Basic Principles of NFS

 

Ubuntu examples

Server:

$ Apt-Get install nfs-kernel-Server

VI/etc/exports add NFS Directory:/personal/nfs_share

10.1.60.34 (RW, sync, no_root_squash)

$ Sudo exportfs-R

$ Sudo/etc/init. d/Portmap start

$ Sudo/etc/init. d/nfs-kernel-Server start

Client:

$ Sudo apt-Get install nfs-common

$ Sudo Mount 10.19.34.76:/personal/nfs_share ~ /Nfsshare example:

3. File Type

The file types in Linux are as follows:

A)Common File: C code, shell scripts, binary executable files, etc. It can be divided into plain text and binary.

B)Directory files: Directory, the only place to store files.

C)Link file: A file pointing to the same file or directory.

D)Special files: Related to system peripherals, usually under/dev. It can be divided into Block devices and character devices.

You can run the LS-L, file, and stat commands to view the file type and other related information.

4. File Storage Structure

A Linux orthodox File System (such as ext2 and ext3) consists of directory items, inode, and data blocks.

Directory item: Includes the file name and inode node number.

Inode: Also known as the file index node, it is the place where the basic information of the file is stored and the data block pointer is stored.

Data Block: The location where the file content is stored.

 

 

Linux orthodox file systems (such as ext2 and 3) partition hard disks into Directory blocks, inode table blocks, and data block data areas. A file consists of a directory item, inode, and data area block. Inode contains file attributes (such as read/write attributes, owner, and pointers to data blocks), and data area blocks are file content. When viewing a file, the system first detects the file attributes and data storage points from the inode table, and then reads data from the data block.

 

The file storage structure is roughly as follows:

Figure 3: 2 W feet view of the file storage structure

 

The structure of the Directory items is as follows (the directory items of each file are stored in the file content of the directory to which the file belongs ):


Figure 4: Directory item Structure

 

The inode structure of the file is as follows (the file information contained in the inode can be viewed through STAT filename ):

 

 

Figure 5 inode Structure

 

The above only reflects the general structure, and the Linux file system itself is evolving. However, the above concepts remain unchanged. There are also many differences in ext2, ext3, and ext4 file systems. For more information, see the introduction of specialized file systems.

5. Soft connections and hard links

Soft links and hard links are two common concepts:

Hard connection: Creates a copy of the file and establishes a connection between the two. Modify one of them, and the connected file is modified at the same time. If you delete any [/color] of the [color = Red] files, the remaining files will not be affected.

Soft connectionIt is also called a symbolic connection. It only creates a "quick (borrow wondows frequently used words)" for the source file at a new location. Therefore, when the source file is deleted, the file connected by the symbol will become a source-> there is only one file name left. Deleting the connection will not affect the source file, but the use and reference of the connection file will directly call the source file.

For details, refer:

 

Figure 5: soft and hard links

 

The differences between hard and soft links are shown in the figure:

1: The inode numbers of the original and new files are the same. The soft link is different.

2: deleting the original file will make the soft link unavailable, but the hard link will not be affected.

3: The contents of soft and hard-link files are also modified for the modification of the original file, because they all point to the same file content.

 

6. File directory management commands

Disk and file space

Fdisk DF du

File directory and Management

Cd pwd mkdir rmdir ls cp rm mv

View File Content

Cat:
Cat
View the file content. The full-process concatenate means that the file content is continuously output to the screen. The first and last rows are displayed.
Tac:
Tac
The opposite of cat is that it is viewed from the last row to the first row.

When cat has a bad place, it cannot be clearly viewed when the file is large. In this case, you can use the more or less command.

More:
More
If you use commands such as grep or find, you can view them one page at a time. If half of them want to exit, press 'q' to exit.
Less:
Less
Less is more flexible than more and can be used to flip pages.

If you only want to read the first few lines of the file or the last few lines of the file, you can use head or tail.
Head-N: The first n rows of the file.
Tail-N: reads n rows at the end of the file.

The above commands are used to view character files, and all the binary files are garbled. To view the content of the binary file, you can use the OD command, for example, to view the content in an MP3 file:
Od shijiemori.pdf

File directory and permissions

Chmod chown chgrp umask

File Search

Which:
Which [filename]
This command is used to query the executable files in the path through the path.
For example: Which passwd: Find the executable file passwd
Whereis:
Whereis [-bmsu] [keyword]
This command is used to list the files and directories of related words. (Linux records all files in a file database. This command is used to query files from the database. Therefore, the database is updated every day)

Locate:
Locate [filename]
This command is used to list the files and directories of related words. Searching for data is extremely fast, and it is also queried through the database. However, the database is updated once a week, so some data may not be found. You can modify the configuration file.

Find:
Find [path] [parameter] [keyword]
This command is used to search for files in a specified path. Because data is not queried, the query speed is slow.

7. Explanation of common Directories

The directory structures of Linux distributions are basically the same. The directories are described as follows:

 

Directory

Description

/

Root directory

/Bin

The most basic command required for the basic system is put here. Such as LS, CP, mkdir, and other commands. The functions are similar to those of/usr/bin. files in this directory are executable and can be used by common users.

/Boot

The Linux kernel and the files required by the boot system program, such as vmlinuz initrd. IMG files, are all in this directory. In general, grub or lilo System Boot manager is also located in this directory; start the mount file storage location, such as kernels, initrd, grub. It is generally an independent partition.

/Dev

Some necessary devices, sound cards, disks, etc. For example,/dev/null./dev/console/dev/Zero/dev/full.

/Etc

The location where the system configuration files are stored. Some server configuration files are also here, such as the user account and password configuration files;

/Etc/OPT:/OPT configuration file

/Etc/X11: xwindows configuration file

/Etc/xml: xml configuration file

......

/Home

User working directory, and personal configuration files, such as personal environment variables. All accounts are allocated a working directory. It is generally an independent partition.

/Lib

Repository file storage location. The library files required by bin and sbin. Similar to Windows DLL.

/Media

Removable Media mount points, such as CD-ROMs, mobile hard disks, and USB flash disks, are mounted here by default.

/Mnt

Temporarily mount the file system. This directory is generally used to store the mounted directory of the mounted storage device, such as a directory such as CDROM. See the definition of/etc/fstab.

/OPT

Optional application packages.

/Proc

When the operating system is running, the process (Program in progress) information and kernel information (such as CPU, hard disk partition, memory information, etc.) are stored here. The/proc directory is the mounted directory of the file system proc disguised. Proc is not a real file system. For its definition, see/etc/fstab.

/Root

Root User's working directory

/Sbin

Similar to bin, it is an executable file, but not all users need it. It is generally used by system management.

/Tmp

System temporary files are generally not saved after system restart.

/Usr

Includes system user tools and programs.

/Usr/bin: non-mandatory commands that can be executed by common users

/Usr/include: standard header file

/Usr/lib:/usr/bin/and/usr/sbin/Library File

/Usr/sbin: non-mandatory Executable File

/Usr/src: kernel source code

/Usr/x11r6: X Window System, version 11, release 6.

/Srv

This directory stores the data to be extracted after the service is started.

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.