Linux file Management

Source: Internet
Author: User
Tags parent directory

Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!

For computers, the so-called data is the sequence of 0 and 1. Such a sequence can be stored in memory, but the data in memory disappears as it shuts down. To save the data for a long time, we store the data on a CD-ROM or hard disk. According to our needs, we usually save the data separately into small units such as files (so-called small, relative to all data). But if the data can only be organized into files, but not classified, the files will be disorganized. Every time we search for a file, we need a file and a file to check, too much trouble. The file system is the logical organization of a file, which stores individual files in a clearer way.

Introduction to Paths and files

files are organized into file system, Usually becomes a tree structure. Linux has a root directory /, which is the file, and this tree's directory (directory, which is the equivalent of the folder we see in the Windows interface). What you see in Figure 1 is the entire tree of files. If we intercept part of the tree, say, starting from the directory Vamei, it actually forms a filesystem.

To find a file, you need to know all the directory names from the tree root to the file, in addition to the file name of the file. The directory name and file name of all paths starting from the root directory make up a path (path). For example, we look for a file in Linux file.txt, not only to know the file name (file.txt), but also to know the full path, that is, the absolute Path (/home/vamei/doc/file.txt). From the root directory/, that is, the top of the tree structure, go through the directory home, Vamei, Doc, finally see the file file.txt. The entire file system layer hierarchy (hierarchy), Vamei is the home subdirectory, and home is the parent directory of Vamei.

In Linux, we use the LS command to display all files in the directory, such as $ls/home/vamei/doc

Figure 1 File Tree

As shown in the figure, the file system, the Green tree. At the top of the root directory (/), along the red arrow marked the path, we finally find the file file.txt.

Catalogue

In a Linux system, a directory is also a file. So/home/vamei is the absolute path to the directory file Vamei.

This file contains at least the following entries:

. Point to current directory

.. Point to Parent directory

In addition, the directory file contains the file name of the file that belongs to the directory, such as the following entry in Vamei, pointing to the file belonging to that directory:

Doc

Movie

Photo

Linux interprets an absolute path as follows: Locate the root directory file, read the home directory file from the directory file, and then read the location of the Vamei from the home file ... Until the location of the file.txt in the directory doc is found.

Because the directory file has. and. Entries, we can add in the path. Or. To represent the current directory or parent directory, such as/home/vamei/doc/. Equivalent to/home/vamei.

In addition, Linux maintains a variable of working directory (present working directory) in the process. In the shell, you can always query to the working directory (enter $pwd at the command line ). This is to eliminate the hassle of entering a very long absolute path each time. For example, we change the working directory to/home/vamei ($cd/home/vamei), then we can go to file.txt to save/home/vamei/($ls doc/file.txt), so that the path is called relative path (relative path), the above doc/file.txt is such a relative path.

When the file appears in a directory file, we access the file to the file system, we call the creation of a hard link to the file. A file is allowed to appear in multiple directories, so that it has multiple hard links. When the number of hard links (link count) is reduced to 0 o'clock, the files are deleted by Linux. So a lot of times, unlink and remove are meant to be in the Linux operating system. Because of the extensive use of soft links (soft link) (soft link does not affect link count and can span the file system), it is now relatively rare to manually establish a hard connection.

file Operations

For files, we can read (read), write, and run (execute). Reading is getting data from a file that already exists. Writing is writing data to a new file or to an old file. If the file is stored in an executable binary code, it can be loaded into memory and run as a program. In a Linux file system, if a user wants to perform an action on a file, the user must have permission to do so on that file . The file permissions information is saved in the file information (metadata), see the next section.

file additional information (metadata)

The file itself contains only data. The file name is actually stored in the catalog file. In addition to these, there are file extensions maintained by the operating system, such as file type, file size, file permissions, file modification time, file read time, and so on. You can query the file information ($ls-l file.txt) with the LS command to get the following result:

-rw-r--r--1 Vamei Vamei 8445 Sep 8 07:33 file1.txt

The meanings of each part are as follows:

  • Let's start with the first one, which represents the file type, which indicates that File1.txt is a regular file (d should be shown if it is a catalog file).
  • followed by nine characters, For rw-r--r--, they are used to represent file permissions. These nine characters are divided into three groups, rw-, R--, r--, corresponding to the owner (owner), the owning group (owner), and all other people (other). review Linux boot up, login, I'll have a user identity and a , Equivalent to my business card . The first group indicates that if the user ID on my business card proves that I am the file's owner Then I can have read (r) to the file, write ( W) The permissions of the file, but does not have permission to execute (-if you have execute permissions, or x) for that file. The second group indicates that if the group on my business card proves that the group I am in is the owns the group I have permission to read from the file. The third group says that if my business card shows that I am neither an owner nor a member of a group, then I only have permission to read in. When I want to perform a read operation, Linux will first see if I am the owner. The following will further explain the owner and the owning group.
  • The following 1 is the number of hard links (link count).
  • The subsequent Vamei indicates that the user Vamei is the owner (owner) of the file and that the owner of the file has permission to change the file permissions (for example, rwxrwxrwx). The owning group of the later Vamei files is the group Vamei. The owner and the owning group of the file are appended to the file when the file is created (equivalent to locking the file, only the user with the appropriate business card can open the operation). Note that Linux has a superuser root (also known as the root user) and that the user owns all the files.
  • The subsequent 8445 represents the file size in bytes (byte).
  • Sep 8 07:33 represents the time of the last write of the file (modification times). In fact, the file additional information contains the last read time of the file , which is not displayed.

Soft links (soft link, or symbolic link)

As mentioned in the discussion of hard links, soft links do not affect the file's link count. If you remember the Windows system shortcuts, Linux soft links (soft link, also called symbolic Link) are shortcuts to Linux. A soft link is essentially a file, and its file type is symbolic link. In this file, contains the absolute path to the file that the link points to. When you read the data from this file, Linux directs you to the file you are pointing to and then reads it from that file (as if you double-clicked the shortcut). Soft links can be conveniently built anywhere and point to any absolute path.

The soft link itself is also a file and can perform the actions that the file can take. When we operate on a soft link, be aware that we operate on the soft link itself, or on the target operation that the soft link points to. If it is the latter, we say that the operation follows the link guide (follow).

umask

When we create a file, such as using touch, it tries to create the new file as permission 666, which is rw-rw-rw-. But the operating system should refer to the permissions mask to see if the file is actually created as 666. The permission mask indicates that the operating system does not allow permission bits to be set, such as 037 (----wxrwx), which means that setting the WX bit and other rwx bits of the group is not allowed. If this is the permission mask, the final file permission is rw-r-----(the W bit and other RW bits of the group are mask).

We can pass

$umask 022

The way to change the permissions mask.

Summary

A computer is essentially a tool for processing data, and a file is a logical carrier of data storage, so it is important to understand the Linux file system. The knowledge of the file system should be organically studied in conjunction with other aspects of Linux, such as user management.

File permissions, owner, owning group, Superuser root

Hard links, soft links, follow the link

Welcome to the "Linux Concepts and Systems " article series

Linux file Management

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.