Linux file operations
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
- 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_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
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
- 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);
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:
SEEK_SET: relative to the beginning of the file.
SEEK_CUR: The current position of the relative file read and write pointer.
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
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.
- /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 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

Troubleshooting steps for failed Docker image build: Check Dockerfile syntax and dependency version. Check if the build context contains the required source code and dependencies. View the build log for error details. Use the --target option to build a hierarchical phase to identify failure points. Make sure to use the latest version of Docker engine. Build the image with --t [image-name]:debug mode to debug the problem. Check disk space and make sure it is sufficient. Disable SELinux to prevent interference with the build process. Ask community platforms for help, provide Dockerfiles and build log descriptions for more specific suggestions.

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)

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

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.
