Linux file operations

Sep 13, 2018 pm 06:24 PM
linux

The example of this article describes the method of interactive execution of python file reading and writing operations and Linux shell variable commands. Share it with everyone for your reference. The details are as follows:

Related system calls for file operations

Create

int creat(const char *filename, mode_t mode);
Parameter mode specifies new The access permission of the file, which together with umask determines the final permission of the file (mode & umask), where umask represents some access permissions that need to be removed when the file is created. It only affects read, write and execution permissions. The calling function is int umask (int newmask).

Open

int open(const char *pathname, int flags);
pathname is the file name we want to open (including the path name, the default is the current path)

flags Open flag

  • O_RDONLY Open the file in read-only mode


  • O_WRONLY Open the file for writing only


  • O_RDWR Open the file for reading and writing Open the file as


  • O_APPEND Open the file as append


  • O_CREAT Create a file


  • ##O_EXEC If O_CREAT is used And the file already exists, an error will occur


  • ##O_NOBLOCK Open a file in a non-blocking manner

  • O_TRUNC If the file already exists, delete the contents of the file
  • int open(const char *pathname, int flag, mode_t mode)

When flag is O_CREATE, specify the mode flag to indicate file access permissions

    S_IRUSR Users can read

  • ##S_IWUSR User can write

  • S_IXUSR User can execute

  • ##S_IRWXU User can read, write and execute

  • ##S_IRGRP group can read

  • S_IWGRP group can write

  • S_IXGRP group can execute

  • The S_IRWXG group can read, write, and execute

  • S_IROTH Others can read

  • ## S_IWOTH Others can write


  • #S_IXOTH Others can execute


  • S_IRWXO Others can Read, write, execute


  • S_ISUID Set the user’s execution ID


  • S_ISGID Set the execution ID of the group


  • The mode flag can also use numbers to represent file permissions:
  • Each number can take the value of 1 (execution permission), 2 (write permission), 4 (read permission), 0 (none), or the sum of these values.

The first digit indicates setting the user ID

  • The second digit indicates Set the group ID


  • The third digit represents the user’s own permission bit


  • The fourth digit indicates the group’s permissions


  • The fifth digit represents the permissions of others


  • open("test", O_CREAT, 10705);
  • The above statement is equivalent to:

    open("test", O_CREAT , S_IRWXU | S_IROTH | S_IXOTH | S_ISUID );

Read and write

int read(int fd, const void *buf, size_t length);

int write (int fd, const void *buf, size_t length);

Parameter fd file descriptor, buf is the pointer to the buffer, length is the size of the buffer (in bytes), and the return value is The actual number of bytes read and written.


read() reads length bytes from the file specified by the file descriptor fd into the buffer pointed by buf. The return value is the actually read bytes. Number

  • write() implementation will write length bytes from the buffer pointed to by buf to the file descriptor In the file pointed to by fd, the return value is the number of bytes actually written.


  • Positioning

  • For random files, we can randomly specify the location to read and write:
int lseek(int fd, offset_t offset, int whence);

lseek() moves the file read-write pointer relative to whence by offset (can be a negative value) bytes. When the operation is successful, the position of the file pointer relative to the file header is returned.

The parameter whence can use the following values:

SEEK_SET: relative to the beginning of the file.
SEEK_CUR: The current position of the relative file read and write pointer.

SEEK_END: ​​Relative to the end of the file.

Close

int close(int fd);

File operation of C library function-independent of the specific operating system platform

Create and open

FILE *fopen(const char *path, const char *mode);

fopen() realizes opening the specified file filename, where the mode is the open mode, Linux The system does not distinguish between binary files and text files.

The value of mode


r, rb is opened in read-only mode

  • w, wb Open in write-only mode. If the file does not exist, the file is created, otherwise the file is truncated


  • a, ab are opened in append mode. If the file does not exist, create the file


  • ##r, r b, rb Open in read-write mode


  • w, w b, wh are opened in read and write mode. If the file does not exist, a new file is created, otherwise the file is truncated


  • a, a b, ab for reading and appending way to open. If the file does not exist, create a new file

Read and write

int fgetc(FILE *stream);

int fputc(int c, FILE *stream);
char *fgets(char *s, int n, FILE *stream);
int fputs(const char *s, FILE *stream);
int fprintf(FILE * stream, const char *format, ...);
int fscanf (FILE *stream, const char *format, ...);
size_t fread(void *ptr, size_t size, size_t n, FILE * stream);
size_t fwrite (const void *ptr, size_t size, size_t n, FILE *stream);
int fsetpos(FILE *stream, fpos_t *pos);
nt fsetpos(FILE *stream, const fpos_t *pos);
int fseek(FILE *stream, long offset, int whence);

  • fread() implements reading n fields from stream, each The field is size bytes, and the read fields are put into the character array pointed by ptr, and the actual number of fields read is returned.


  • write() implements writing n fields from the array pointed to by buffer ptr to stream, each time The length of each field is size bytes, and the number of fields actually written is returned.

Close

int fclose (FILE *stream);

Linux file system directory structure

Linux file operations

  • /bin----stores the most commonly used basic commands, such as ls , cp, mkdir, etc., the files in this directory are all executable.


  • /boot----Some core files used when starting Linux, including some connection files and image files, Such as vmlinuz, initrd.img


  • /dev----device file storage directory, the application program passes these files Read, write and control the actual device.


  • /etc----Configuration files and subdirectories required for system management, such as user account and password configuration files .


  • /home----The home directory of ordinary users. Each user has his own directory. Generally, The directory name is named after the user's account.


  • /lib----Library file storage directory, the most basic dynamic link shared library of the system, similar to Windows DLL file inside.


  • /lost found----usually empty, when the system crashes unexpectedly or the machine shuts down unexpectedly Some file fragments will be generated and placed here.


  • /mnt----It is convenient for users to temporarily mount other file systems, such as mounting the optical drive on /mnt/, enter this directory to view the contents of the CD-ROM drive


  • media----automatically recognize some Devices are mounted to this directory, such as USB drives, CD-ROM drives, etc.


  • /opt----The directory where additional installation software is stored on the host


  • /proc----When the operating system is running, process and kernel information (such as CPU, hard disk partition, memory information, etc.) are stored here. It is a mapping of system memory and exists in memory. System information can be obtained by directly accessing this directory.


  • /root----The home directory of the super privileged user


  • /sbin----The directory where executable commands for super privileged users are stored. Ordinary users do not have permission to execute commands in this directory


  • /tmp-----Storage temporary files.


  • /usr-----Directory where system applications and files (such as commands and help files) store programs , similar to the program files directory under Windows.


  • /var-----Directories that are frequently modified are placed in this directory, such as log files


  • /sys----

    An intuitive reflection of the kernel device tree. When a kernel object is created, the corresponding files and directories are also created in the kernel object subsystem.


  • /initrd---If the initrd image is used as a temporary root file system during the boot process, After executing /linuxrc to mount the real root file system, the original initial RAM file system is mapped to the /initrd directory.

Linux file system and device driver

Related recommendations:

linux Parent directory permissions affect subdirectory files Operation

How to interactively execute python file read and write operations with linux shell variable commands

The above is the detailed content of Linux file operations. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

vscode terminal command cannot be used vscode terminal command cannot be used Apr 15, 2025 pm 10:03 PM

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)

See all articles