Linux---file, soft link and hard link file

In Linux, everything is a file, so we need to have a clear understanding of the file system under Linux.

File attributes

In Linux, we can pass ls -lor llview specific file information.
Insert picture description here
Insert picture description here
Note: The ls command is used to view the file name in the current directory, but it cannot view the file attributes.

What are the specific file attributes:

  • All files beginning with'-' are ordinary files.

Insert picture description here

  • Files starting with d are directory files
  • The file starting with b is the device file (hard disk, CD-ROM drive)
  • Files starting with l are linked files
  • Files beginning with p are pipe files
    Insert picture description here
    Insert picture description here

file permission

The corresponding permissions are:

  • r readable
  • w can be written
  • x operable

If we need to view the permissions of a file, we can use it ls ltogether.

Insert picture description here
In the above file, the owner's permission is readable and writable, the group's permission is readable and writable, and the rest of the permissions are readable.

Soft link and hard link

  • The soft link file is
    similar to a shortcut, in Linux, the soft link file is similar to a text file. Similar to a text file containing the location information content of another file, we can access the source file of the soft link through this "shortcut file". When reading and writing the "shortcut", the essence is to read and write the source of the soft link. File operation, but when deleting the "shortcut", the source file will not be deleted.

  • Hard-linked file is
    similar to the alias of a file. When we operate on a file with a hard-linked file, we first find the corresponding source file through this alias, and then operate on the source file. When deleting hard-linked files, the system will not delete the hard-linked source files. If you delete the hard-linked source file, the hard-linked file will not be deleted, and its original data will be saved. Therefore, the hard-linked file can prevent the "incorrect deletion" operation.

the difference

  • Hard link files do not occupy disk space, just add an alias (directory) to it
  • The soft link occupies disk space, and because it is a shortcut in nature, it does not take up a lot of memory.
  • Cannot create hard links to directories.
  • You cannot create hard-linked files across file systems.

File descriptor

When we open or create a file, the Linux kernel sends a file descriptor to a process. When a process is started, 3 files will be opened by default (0 standard input, 1 standard input, 2 standard error)
int creat(const char* pathname,mode_t mode);

  • pathname: the path to create the file
  • mde: file permissions
#include<stdio.h>
#include<fcntl.h>

int main()
{
    
    
	int fd = -1;
	char filename[] = "/root/text.txt";//创建文件的路径
	fd = creat(pathname,066);
	if(fd == -1)
	{
    
    
		printf("文件创建失败\n");
	}
	else
	{
    
    
		printf("文件创建成功\n");
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_42708024/article/details/109477703