Document operation notes


A file is an ordered collection of related data. In linux system, the relationship between the data in the file and the data is established and interpreted by the application using the file. They can have a relationship in a file.

File system refers to the file data structure and the program collection for managing files. In addition, it also includes two aspects: ext2,ext3 and other partition formats and file permissions.

File properties

In linux, every file has its attributes, including file type and file permission.

file type

Files can be divided into buffer files and non buffer files according to the processor.

Buffer file: refers to that the system automatically creates a buffer for each file in use in the memory area.

Non buffer file: refers to the program itself setting the buffer for each file without automatically opening up a certain buffer.

Files can be divided into text files and binary files according to their data organization.

Text file: ASCII file, each byte of which stores an ASCII code, representing a character, one-to-one correspondence, takes up a lot of storage space, and takes time to convert to binary form.

Binary file: output the data in the memory to the disk as it is stored in the memory. It takes up less characters and does not need to be converted, but it is not one-to-one correspondence. It cannot output the character form directly.

Files can be divided into ordinary files, directory files, link files, equipment files and pipeline files according to their data storage functions.

Common files: files that do not contain the structure information of the file system, such as graphic files, data files, document files, sound files, etc.

Directory file: a file used to store file names and related information. It is the basic node in the kernel used to organize the file system.

Link file: a link to a real file, pointing to the file to be called.

Device file: it is the most special file of Linux system. In Linux, external hardware devices can be accessed through device files.

Pipeline file: used for information transfer between different processes.

file right

Linux adopts the principle of multi-user. Different users have different permissions for different files, which is conducive to protecting the security of the system.

There are three types of permissions: read r, write w, and execute x

The user who owns each file is called the file owner. In Linux, users are based on groups. Each user exists in one group or belongs to multiple groups at the same time. Therefore, in addition to the users who have ownership of files, there are users in the same group of file owners and other users. These three file centric users have different access rights to files.

In Linux, root is the system administrator and the user with the highest authority.

Information about documents

In Linux, each file is placed in a directory, and some attribute information of the file is saved through an index node associated with the file (mainly including the directory structure of the file, the index node, and the data stored in the file)

Directory structure of files

All files of the system are stored in the root directory root(/). The directory file is like a tree. Many subdirectories are separated from the root directory, and directories are separated under the subdirectories.


In Linux, the information contained in the inode is encapsulated in the stat structure

struct stat{
    dev_t st_dev;//Equipment No. for document use
    ino_t st_ino;//Inode number
    mode_t st_mode;//Access to files
    nlink_t st_nlink;//Number of hard connections
    uid_t st_uid;//Owner user ID
    gid_t st_gid;//User group ID
    dev_t st_rdev;//Equipment number of equipment file
    off_t st_size;//File size in bytes
    blksize_t st_blksize;//Disk block size of the file system
    blkcnt_t st_blocks;//Disk block size of the current file
    time_t st_atime;//Last time the file was accessed
    time_t st_mtime;//Last time the file was modified
    time_t st_ctime;//Last time the file state was changed

Data stored in the file

A file is a continuation of a set of related data. The data related to the file is stored in the location specified by the inode, but some special files do not have a hard disk area (device file) to store the data in the file.

File related operations

In Linux, there are many kinds of file operations, such as file I/O operations, modifying file attributes, copying file descriptors, and controlling some files. In Linux, there are two types of file I/O operations: I/O based on file descriptor and I/O based on file stream.

Modify file properties

Change the owner of the file

The system provides chown() and fchown() functions to modify the owner ID and user group ID of the specified file

int chown(const char *pathname,uid_t owner,gid_t group);
int fchown(int fd,uid_t owner,gid_t group);

Are used to modify the owner ID and user group ID of the file.

The parameter pathname in the chown() function represents the absolute or relative path of the file.

The parameter fd in the fchown() function represents the file descriptor of the file.

The owner parameter is the new owner ID of the file.

The group parameter refers to the new user group ID of the file

The function call succeeds and returns 0; Failed, return -1, and set the corresponding errno value.

In contrast, the fchown() function is safer.

Change file access

In Linux, the chmod() and fchmod() functions can be called to change the access permissions of files.

int chmod(const char *path,mode_t mode);
int fchmod(int fildes,mode_t mode);

Path: the absolute or relative path of the file needs to be modified

fildes: file descriptor of the file to be modified

mode: permission of the file to be modified

The function call succeeds and returns 0; Failed, return -1, and set the corresponding errno value.

Change the name of the file

The rename() function is provided to modify the location or name of the file

int rename(const char *oldpath,const char *newpath);

The function call succeeds and returns 0; Failed, return -1, and set the corresponding errno value.


int main(int argc,char *argv[])
	if(argc<3)	/*When the parameter passed from the terminal is less than 3, it indicates the usage of the program*/
		printf("usages:%s oldpath newpath\n",argv[0]);
		return 1;
	if(rename(argv[1],argv[2])<0)/*Call the function to change the name of argv[1] to the name of argv[2]*/
		return 1;
	{/*Function call succeeded*/

	return 0;

Change file length

The truncate() and ftruncate() functions are provided

int truncate(const char *path,off_t length);
int ftruncate(int fd,off_t length);

Path is a pointer to the file path

fd is the file descriptor of the file

Length is the new length of the file

Delete more bytes and add empty bytes ('\0') if less

The ftruncate() function requires that the file be opened in writing; The truncate() function requires that the file be writable.

The function call succeeds and returns 0; Failed, return -1, and set the corresponding errno value.

Copy file descriptor

Two functions dup() and dup2() are provided

int dup(int oldfd);
int dup2(int oldfd,int newfd);

Copy a file descriptor represented by the parameter oldfd and return the file descriptor.

The copied file points to the same file, sharing permissions, etc.

The function call succeeds, and the minimum and unused file descriptors are returned; Failed, return -1, and set the corresponding errno value.


Get file information

The system provides 3 functions

int stat(const char *path,struct stat *buf);
int fstat(int fd,struct stat *buf);
int lstat(const char *path,struct stat *buf);

Path: path name of the file to get information

fd: file descriptor

buf: pointer to a stat structure

Note: if the stat() and lstat() functions need to obtain the file information in a directory, they need to have the permission to execute all the parent directories where the file is located.


Use the stat() function to get new C file size and user ID of the file owner

    struct stat buf;
    stat("new.c",&buf);	/*Get new C file information, stored in buf*/
    printf("new.c file size=%d\n",buf.st_size);/*Output file size*/
    printf("new.c file owner UID=%d\n",buf.st_uid);/*Output file UID*/

Other operations for files

Write buffer data back to disk

fsync() function. Write buffer data back to disk

int fsync(int fd);

The function call succeeds and returns 0; Failure returns -1 and sets the corresponding errno value.

Lock file

The flock() function. Realize various locking and unlocking operations for files.

int flock(int fd,int operation);

fd is the file descriptor of the file operated

operation refers to various ways to lock or unlock files

operation value:

  • LOCK_SH: share locking. Multiple processes can share and lock a file at the same time.
  • LOCK_EX: mutex top. A file can only have one mutex lock at a time.
  • LOCK_UN: contact file lock status.
  • LOCK_NB: when the lock cannot be established, this operation cannot be blocked. Return to the process immediately. Usually with LOCK_SH or LOCK_EX performs the or (|) operation.

A single file cannot establish a mutex top and a share lock at the same time. When using the dup() function to copy the file descriptor or calling the fork() function to create a child process, the file descriptor will not inherit this lock.

The function call succeeds and returns 0; Failure returns -1 and sets the corresponding errno value.

Tags: Linux

Posted by waiwai933 on Tue, 31 May 2022 06:09:48 +0530