Shell command to operate Linux file system

Introduction to folders

  The Linux file system is a key component of the computer operating system, which is used to manage and organize data and information on the computer. First go to the root directory, and then print the files in the current directory:

cd / ; ls

Insert image description here
  Some of the more commonly used folders are introduced below:

  /bin--Contains common Linux user commands such as ls, sort, date, and l chmod. ——Contains the directories assigned to each ordinary user with a login account (the root user is an exception, using /root as the home directory). --Contains shared libraries required by applications in the /bin and /sbin directories to start the system. --The /mnt directory was a common mount point for many devices before it was replaced by the standard /media directory. Some bootable Linux systems still use this directory to mount hard disk partitions and remote file systems. In addition, many people also use this directory to temporarily mount local or remote file systems that do not need to be permanently mounted. —Indicates the home directory of the root user. For security reasons, this home directory is not located under the /home directory. /sbin - Contains administrative commands and daemons. -- Contains temporary files used by applications. ——Contains user documentation, games, graphics files (X11), libraries (lib), and other commands and files that are not required during startup. The files in the /usr directory cannot be changed after installation (theoretically, /usr can be mounted read-only).
  /home
  /lib
  /mnt
  /root
  /tmp
  /usr

General commands for folders

  1. cd (Change Directory): cd command is used to change the current working directory. It is possible to switch to a different directory by providing the path to the target directory as argument.
Insert image description here

  2. pwd (Print Working Directory): pwd command is used to display the full path of the current working directory. After execution pwd, the current directory path will be displayed.
Insert image description here

  3. ls (List): ls command is used to list files and subdirectories in the current directory. By default, it lists the contents of the current directory. Different options are available to control which details are listed and how they are sorted.
Insert image description here

  4. mkdir (Make Directory): mkdir command is used to create a new directory. The name of the directory to be created can be specified as an argument.

  5. chmod (Change Mode): chmod command is used to change the permissions of a file or directory. It allows controlling which users can execute files, read files, or write files. chmodThe command is typically used with permission modes such as 755 or 644 to determine permission settings.

Although we usually do not need permission control on a Linux platform for one person, once it involves multi-person scenarios or corporate scenarios, permission control is necessary, and the commands must be studied carefully chmod. See the next section for details

Folder permission control⭐

  First enter in any directory:

ll

Insert image description here
  The output is as shown in the figure, with a total of 7 columns:

  · File Type and Permissions column: Displays the file type and access permissions. Usually includes characters like -rw-r--r--or drwxr-xr-x, where the first character indicates the file type, and the following nine characters are divided into three groups, each group of three characters representing the file access permissions. For example, -rw-r--r--it represents an ordinary file whose permission settings allow the owner to read and write, but only allow other users to read.

  · Hard Links: This column indicates the number of hard links associated with the file or directory. A hard link refers to a situation where multiple file names point to the same data block. Usually, the number of hard links for ordinary files is 1, and the number of hard links for directories is at least 2 (because each directory has at least two links: one pointing to itself and one pointing to the superior directory).

  · Owner column (Owner): Displays the username of the owner of the file or directory.

  · Group column: Displays the name of the user group to which the file or directory belongs.

  · File Size column (File Size): Displays the size of the file or directory, usually in bytes. For directories, the size is usually the sum of all files and subdirectories in the directory.

  · Modification Time or Creation Time column (Modification Time or Creation Time): Displays the last modification time of the file or directory.

  · File Name or Directory Name column (File Name or Directory Name): This column displays the name of the file or directory.

File types and permissions

  file type:

  1. Regular File: Regular files contain information such as text, binary data or program code. In the file type representation, -starts with . For example, -rw-r--r--represents a normal file.

  2. Directory: A directory is a special file type used to organize and store files and other directories. In the file type representation, dstarts with . For example, drwxr-xr-xrepresents a directory.

  3. Symbolic Link: A symbolic link is a reference to another file or directory. They are a special type of file used to create links between files and directories. In the file type representation, lstarts with . For example, lrwxrwxrwxrepresents a symbolic link.

  4. Device File: Device files are used to communicate with computer hardware devices. There are two types of character device files and block device files. Character device files are used to process character stream data, and block device files are used to process data blocks. In the file type representation, character device files cbegin with and block device files bbegin with .

  5. Pipe file (Named Pipe): Pipe files are used for inter-process communication, allowing one process to transfer data to another process. In the file type representation, pstarts with .

  6. Socket file (Socket): Socket file is used for network communication, allowing processes to communicate through network connections. In the file type representation, sstarts with .

  Permissions:

  Permissions on files and directories determine which users can perform the following operations:

  • Read: Allows the user to view the contents of a file or a listing of directories.
  • Write: Allows the user to modify the contents of a file or the structure of a directory.
  • Execute: For files, users are allowed to execute the programs contained in them; for directories, users are allowed to access the contents.

  Permissions are expressed as a set of three characters, representing the permissions of the file owner, the permissions of the user group to which it belongs, and the permissions of other users. Each permission can be represented by the following characters:

  • r (Read): Indicates read permission.
  • w (Write): Indicates write permission.
  • x (Execute): Indicates execution permission.

  For example, -rw-r--r--means that the file's permissions are set to allow the owner to read and write, but only allow other users to read only.

  使用 chmod 命令可以更改文件和目录的权限设置,以控制对它们的访问和操作。权限设置是Linux和Unix系统中的安全重要组成部分,它确保只有经过授权的用户能够访问和修改文件和目录。

修改文件权限

  要修改文件或目录的权限,需要使用 chmod 命令。 chmod 命令允许添加或删除文件的读取(read)、写入(write)和执行(execute)权限,以及设置文件的所有者、所属用户组和其他用户的权限。

  1. 基本 chmod 语法:

chmod [选项] 权限设置 文件或目录

  2. 权限设置格式:

  • 使用数字表示权限:例如,chmod 755 myfile.txt 表示将文件 “myfile.txt” 的权限设置为所有者可读写执行,所属用户组和其他用户只有读和执行权限。
  • 使用符号表示权限:例如,chmod u+r myfile.txt 表示为文件 “myfile.txt” 添加所有者的读取权限。

设置权限有2种方式。第一种,r=4、w=2、x=1,那么7指的就是rwx,4指的是r–,3指的是-wx。第二种,通过u、g、o、a四种权限标识符结合+、-、=三种权限操作符,进行增加、删除、修改权限。

  3. 常见选项:

  • -R:递归地修改目录及其内容的权限。
  • -v:显示每个文件权限修改的详细信息。

  4. 权限标识符:

  • u:文件所有者。
  • g:所属用户组。
  • o:其他用户。
  • a:所有用户(等同于 ugo)。

  5. 权限操作符:

  • +:添加权限。
  • -:删除权限。
  • =:设置权限。

  下面举个例子展示 chmod 命令的用法,首先go.sh的权限是drwxr-xr-x
Insert image description here

  • Remove execution permissions for other users:
chmod o-x go.sh

Insert image description here

  • Add write permission to the user group:
chmod g+w go.sh

Insert image description here

  • Set the file "go.sh" to executable permissions using numeric mode:
chmod 755 go.sh

Insert image description here

Move, copy, delete folders

  Move a folder (rename a folder):Use mvcommands to move folders or rename them. If a new destination path is provided, the folder will be moved to the new location. If you use a new name as the target path, the folder will be renamed. For example, to move the folder "myfolder" to a new location or rename it to "newfolder", you can execute the command:

mv myfolder newfolder

  Copy the folder:Use cpthe command to copy the folder and its contents. To copy the folder "myfolder" to the new location "backup", you can execute the command:

cp -r myfolder backup

  -roption means to copy the folder and its contents recursively. If you want to leave the original folder unchanged and copy its contents to the new location, you can omit -rthe option.

  Delete folder:Use rmthe command to delete the folder and its contents. To delete the folder "myfolder", you can execute the command:

rm -r myfolder

  -roption means to recursively delete the folder and its contents. Be careful with rmthe command as it will permanently delete the folder and its contents. Make sure to confirm our deletion before performing the deletion.

Guess you like

Origin blog.csdn.net/qq_43592352/article/details/132732606