Click the blue text above to follow us


1. Basics of the Linux File System
1. Characteristics of the Linux File System
-
The Linux file system adopts a tree structure, starting from the root directory root (/).
-
The Linux virtual file system allows many different types of file systems to coexist and supports operations across file systems.
-
Linux files are unstructured character stream files, which do not consider the logical structure inside the file, simply viewing files as a sequence of characters.
-
Linux files can be protected by access permissions set by the file owner or superuser.
-
Linux treats all external devices as files, allowing the same system calls used for file systems to read and write external devices.
2. Components of the Linux File System
Linux File System Directory Structure:

2. Operations of the Linux File System
1. Basic Concepts of Files
Files are collections of related elements defined by the creator, identified by a filename; files can be text documents, images, programs, etc.
The length of filenames in the Linux system varies depending on the type of file system; naming files must adhere to the following rules:

2. Wildcards and Shortcut Operations in the Linux System

3. File Operation Commands
(1) Display File Command
cat command:
-
Format: cat [options] …[files]…
-
Description: Outputs the contents of multiple files to the screen; if “> filename” is added, it outputs to another file.
-
Common options:

more command:
-
Format: more [options] [files]
-
Description: This command displays the contents of a text file, one screen at a time; it stops when the screen is full, and can continue with the following keys:

-
Common options:

less command:
-
Format: less [options] [files]
-
Description: Similar to the more command, displays file contents screen by screen; the less command allows users to browse the file forwards or backwards, pressing the q key to exit.
-
Common options:

head command:
-
Format: head [options] …[files]…
-
Description: Used to view the beginning part of a text file, the number of lines is determined by the parameter value, default is 10
-
Common options:

tail command:
-
Format: tail [options] …[files]…
-
Description: Used to view the last few lines of a text file, the number of lines is determined by the parameter value, default is 10. If the specified file has more than one, a filename header is added before each displayed file.
-
Common options:
-c: Displays the last num bytes of the file (e.g., to display the last 5 bytes of a.txt, the command would be: tail -c5 a.txt).
-n: Displays the last num lines of the file; if this parameter is not specified, it displays 10 lines (e.g., to display the last 5 lines of a.txt, the command would be: tail -n5 a.txt).

(2) Search, Sort, and Remove Duplicate Lines Command
grep command:
-
Format: grep [options] search_pattern [files]
-
Description: The grep family includes grep, egrep, and fgrep. egrep is an extension of grep that can use extended string patterns for searching, while fgrep treats all letters as words and does not recognize regular expressions. If no filename is specified, the grep command searches standard input.
-
Common options:

-
Using regular expressions in the grep command allows for pattern matching to add more rules, enabling more choices when extracting information.
sort command:
-
Format: sort [options] …[files]…
-
Description: The sort command sorts all lines in the specified file, displaying the results on standard output; if no input file is specified or using “-”, it indicates that the sorting content comes from standard input.
-
By default, it sorts by line in ASCII character order.
-
Common options:

-
Options for changing the default sorting rules include:

uniq command:
-
Format: uniq [options]… [input_file [output_file]]
-
Description: Checks and removes duplicate lines from a text file.
-
Common options:

(3) Commands for Comparing File Contents
comm command:
-
Format: comm [options]… file1 file2
-
Description: The comm command compares two already sorted files, reading the text lines from file1 and file2 to generate three lines of output: lines appearing in both files, lines only appearing in file1, and lines only appearing in file2.
-
Common options:

diff command:
-
Format: diff [options]… file_list
-
Description: The diff command compares text files and identifies their differences.
-
Common options:

(4) Commands for Copying, Deleting, and Moving Files
cp command:
-
Format: cp [options] source_file or directory target_file or directory
-
Description: The cp command copies files. If the source is a regular file, this command copies the source file to the specified target file or directory; if the source is a directory, and the target is an existing directory, this command copies all files and subdirectories under the source directory to the target directory; if the source is a directory and the target is not an existing directory, the command returns an error message.
-
Common options:

rm command:
-
Format: rm [options] …files or directories…
-
Description: The rm command can delete files or directories; to delete a directory, the “-r” option must be added.
-
Common options:

mv command:
-
Format: mv [options] source_file or directory target_file or directory
-
Description: The mv command can move files or directories; it can also rename files or directories.
-
Common options:

(5) File Content Statistics Command
wc command:
-
Format: wc [options] …[files]…
-
Description: The wc command counts the number of bytes, words, and lines in the specified file.
-
Common options:

(6) Commands for Finding Files and Directories
find command:
-
Format: find [pathnames…] [expressions]
-
Description: The find command is used to search for files and directories that meet certain conditions; pathnames are a list of directory names to search for files, separated by spaces; expressions contain matching criteria or descriptions for the files to find. Expressions are evaluated from left to right, and as long as the test result in the expression is true, the next test is performed.
-
Common expressions:

(7) File Compression and Backup Commands
bzip2 command:
-
Format: bzip2 [options] [file to compress]
-
Description: The compression program for .bz2 files, which deletes the original file.
-
Common options:

gzip command:
-
Format: gzip [options] [files…]
-
Description: Gzip compression produces compressed files with a “.gz” extension and deletes the original files.
-
Common options:

tar (tape archive) command:
-
Format: tar [options…] [files or directories]…
-
Description: Tar is a tool program for creating and restoring files; it can add and unpack files within backup files.
-
Common options:

-
For example (this may be a bit difficult to understand, here’s an example):
-
Back up files 1.txt, 2.txt, and 3.txt in the current directory to file 123.tar, displaying the execution process of the backup command, the command is: tar -cvf 123.tar 1.txt 2.txt 3.txt, the execution result is as follows; you can see that the backup file 123.tar has been generated using the command ls -l

Restore the backup file 123.tar by first deleting files 1.txt, 2.txt, and 3.txt, then using the command tar -xvf 123.tar to restore; using the ls -l command, you can see that 123.tar has been restored to 1.txt, 2.txt, and 3.txt, the execution result is as follows:

4. Directory Operation Commands
(1) Commands for Switching Working Directories and Displaying Directory Contents
cd command:
-
Format: cd [target directory]
-
Description: Switches the directory to the target directory, provided the user has permission to enter the target directory. The target directory can be an absolute or relative path; if the directory name is omitted, it switches to the user’s home directory.
pwd command:
-
Format: pwd
-
Description: The pwd command without any options or parameters shows the complete path of the “current directory” in the Linux system.
ls command:
-
Format: ls [options]… [files or directories]…
-
Description: The ls command lists the contents of a directory, including the names of files and subdirectories.
-
Common options:

(2) Commands for Creating and Deleting Directories
mkdir command:
-
Format: mkdir [options]… directory_name…
-
Description: This command creates a directory named after the directory name, while setting the permissions for that directory. The user creating the directory must have write permission in the current directory, and the directory name cannot be the same as any existing directory or file name in the current directory.
-
Common options:

rmdir command:
-
Format: rmdir [options]… directory_name…
-
Description: This command deletes one or more directories from a directory. To delete a directory, the user must have write permission on the parent directory, and the directory to be deleted should be empty.
-
Common options:

(3) Commands for Changing File or Directory Access Permissions
Before introducing this part of the commands, we need to understand the concepts of users, user groups, and access permissions in the Linux system.
The Linux system is a multi-user, multi-tasking time-sharing operating system. Any user wanting to use system resources must first apply for an account from the system administrator and then log into the system with that account. Users are identified internally by UID.
A user group (group) is a collection of users with similar characteristics; each user in the Linux system belongs to a user group, which allows the system to manage all users in a user group centrally. User groups are identified internally by GID.
Linux defines four types of users: file owner, group user, other user, and superuser.
Linux defines three types of access permissions: read (r), write (w), and execute (x).
The ls -l command can display detailed information about files or directories, where the first field is the file attribute field. In this field, the first letter indicates the file type; the three segments following the “-” symbol consist of “r”, “w”, and “x”, which represent the file owner permissions, group user permissions, and other user permissions, respectively.
ls -l.

After understanding these concepts, we can introduce the related commands.
chmod command:
-
Format: chmod [who] [opt] [mode] file or directory name…
-
Description: Used to change the access permissions of files or directories. Who represents the object, which can be u (file owner), g (group user), o (other users), or a (all users).
Opt represents the operation, which can be symbols such as + (add a permission), – (remove a permission), = (assign given permissions and remove existing permissions).
Mode represents permissions, which can be a combination of r (read), w (write), x (execute).
Permissions can also be changed using numeric notation with the chmod command.
-
Format: chmod [mode] file or directory name…
-
Description: Treat rwx as a binary number; if a permission exists, it is represented by 1, and if it does not exist, it is represented by 0. Thus, rwx r-x r– can be represented as 111 101 100, and converting each three bits to an octal number gives 754. The common permission number representation table is shown below:
For example: Change the permissions of the files test.txt and test1.txt in the current directory so that the file owner can read and write, the group user can execute, and other users have no access.
Using the first method, the command should be: chmod u=rw,g=x test.txt, the execution result is as follows:

Now using the second method to operate on test1.txt, the command should be: chmod 610 test1.txt, the execution result is as follows:

(4) Commands for Changing User Group and File Owner
chgrp command:
-
Format: chgrp [options]… group [file or directory]…
-
Description: The chgrp command changes the user group of the specified file; the group can be the group ID or group name. The file is a list of files to change the group, separated by spaces, supporting wildcards. Generally, only superusers can change the group of the file in Linux.
-
Common options:

chown command:
-
Format: chown [options]… [user][:[group]] file…
-
Description: The chown command changes the owner of the specified file to the specified user or group. The user can be a username or user ID; the group can be a group name or group ID. The file is a list of files to change permissions, separated by spaces, supporting wildcards. Generally, only superusers can use this command in Linux.
-
Common options:

-
For example: Change the owner of the directory/home/user/dest and all files and directories within it to root; the command is: chown -R root /home/user/dest
(5) Commands for Linking Files
ln command:
-
Format: ln [options]… source_file or directory target_file or directory
-
Description: The ln command is used to link files or directories.
-
Common options:
-
-s: Creates a soft link (symbolic link) to the source file instead of a hard link.
Receive Benefits

Source: Internet; infringement will be deleted.