A detailed analysis of the basis of the classification of Linux commands, Xiaobai can understand in seconds (introduction to command composition, help commands, basic operation commands for directories and files)

1. Introduction to the format of Linux commands

1. Classification of commands

1.1, what is a Linux command

In the Linux system, any string of specific operations and tasks that can be entered in the character interface can be called a "command". Strictly speaking, "commands" usually only represent the names of instructions or programs that implement a certain type of function.
The execution of Linux commands must rely on the Shell command interpreter. Shell is actually a special program that runs in the Linux operating system. It is located between the operating system kernel and the user. It is responsible for receiving and interpreting commands entered by the user, and passing the operations that need to be performed to the system kernel for execution. Acting as a "translator" between the kernel and the core. When a user logs in to a Linux system, a Shell program is automatically loaded to provide the user with an operating system that can enter commands.

1.2 Two types of commands
  • Internal commands : refer to some special commands integrated in the Shell interpreter program (such as Bash), also known as built-in commands. The internal commands are part of the Shell, so there is no separate corresponding system file. As long as the Shell interpreter is run, the internal commands are automatically loaded into the memory and the user can use them directly. Internal commands do not need to re-read files from the hard disk, so the execution efficiency is higher.
  • External command : Refers to a script file or binary program that can complete a specific function in the Linux operating system. Each external command corresponds to a file in the system. It is a command outside the Shell interpreter program, so it is called an external command. The Linux operating system must know the file location corresponding to the external command before it can be loaded and executed by the Shell.

2. Command format: command word [option] [parameter]

2.1, command word

The command word is the command name, which is the most critical part of the entire command. In the Linux character operation interface, the command word is used to uniquely determine a command, so when inputting a command, make sure that the input command word is correct. Moreover, in the Linux command environment, whether it is a command name or a file name, the processing of English characters is case-sensitive, so you need to be careful when operating.

2.2, options

The function of the option is to adjust the specific function of the command and decide how to execute this command. When the same command word is used with different options, similar but slightly different functions can be obtained. The options used by the command have the following characteristics.

  • Different command words can use different options (number and content of options).
  • The number of options can be multiple or omitted. When using multiple options at the same time, use spaces to separate the options. If the option is not used, the default function of the command word will be executed.
  • When using a single-character option, generally use the "-" symbol (half-width minus sign) before the option to guide it, which is called a short format option, such as "-l". Multiple single-character options can be used in combination, such as "-al" is equivalent to "-a -l".
  • When using multiple-character options, generally use the "–" symbol (two half-width minus signs) to guide the options, which are called long format options, such as "–help".
2.3. Parameters

Command parameters are the processing objects of command words. Generally, command parameters can be file names, directory (path) names, or user names. Depending on the command word used, the number of command parameters can be zero or more. When entering a Linux command, the corresponding command parameters should be provided according to the specific format of the command word to ensure the normal operation of the command.
**Note:** In the actual use of the Linux command line, the terms "option" and "parameter" are often confused, and even the order of the front and back is allowed to be reversed, but generally does not affect the execution effect of the command, so it is not often Make a strict distinction.

2.4. Command assistance and simple shortcut keys

When editing the Linux command line, you can use the following auxiliary operations to improve input efficiency.

  • Tab key : It can automatically fill in the inputted incomplete command words or file and directory names. For example, press the Tab key after inputting "ifcon" to automatically fill in the command word "ifconfig". This function can only be filled backwards, and it must start with a part of the characters that have been entered, which can uniquely locate a command word or file or directory name. Otherwise, you can press the Tab key twice, and the system will output a list of available names.
  • Backslash "\" : If the input line of command content is too long, the terminal will automatically wrap. Sometimes in order to display beautifully and facilitate viewing, you can also insert the "\" symbol to force a new line. After the ">" prompt appears on the next line, you can continue to enter the content as a continuation of the previous command.
  • Ctrl+U key combination: quickly delete all characters before the current cursor.
  • Ctrl+K key combination: quickly delete all characters from the current cursor to the end of the line.
  • Ctrl+L key combination: quickly clear the display on the current screen, and only display the command prompt in the upper left corner.
  • Ctrl+C key combination: Cancel the editing of the current command line and switch to a new line of command prompt.
2.5, simply understand a few commands
  • hostnamectl set-hostname the name to be changed
  • su: Reload the bash environment without restarting to make the changed name take effect
  • Tab: auto completion: tab
  • history: view history commands
  • history -c: Clear history command

Two, command help

1. Use the help command to help

The help command itself is a built-in command in Linux Shell, and its purpose is to view the help information of each Shell internal command. When using the help command, you only need to add the name of the internal command as a parameter.
For example: help cd (cd is the command to switch directories in Linux)
Insert picture description here

2. Use the "–help" option

For most Linux external commands, you can use a general command option "–help" to display the format and options of the corresponding command word and other help information. If the command word does not have the "-help" option, generally only a simple command format will be prompted.
For example: ls (used to display file or directory listing information) --help
Insert picture description here

3. Use the man page

The manual page (Manual Page) is the most commonly used form of online help in the Linux operating system. Most of the external software provides detailed help manual pages for executing programs and configuration files during installation. The information in these manual pages is organized in a specific format and read through the unified manual page browser program man.
When reading the man page, it will be displayed in full-screen text mode and provide an interactive operating environment. Press ↑, ↓ arrow keys to scroll up and down a line of text content; press Page Up and Page Down keys to scroll up and down to display; press Q or q to exit the manual page reading environment at any time; press / You can search the contents of the manual after pressing the key. For example, enter "/-v" to find the help information of the "-v" option. If you find multiple results, you can also press the n key or the N key to go down and up respectively Positioning selection.
Example: man ls
Insert picture description here

3. Basic operation commands for directories and files

1. View directories and files

Common commands for viewing directories in Linux include pwd, cd, ls, and du commands. These commands all operate on file directories, but their functions are slightly different.

pwd command-view the current working directory (Print Working Directory)

The pwd command is used to display the current working directory location of the user. The working directory is the default starting point for the user to manipulate files or other subdirectories.
Insert picture description here

ls command-list (List) display the contents of the directory

The ls command is mainly used to display the contents of a directory, including related attribute information of subdirectories and files. The parameter used can be a directory name or a file name, allowing multiple parameters to be used in the same command.

  • ls: Can display the sub-directory and file list information contained in the current directory.
  • ls -l: Display the list of files and directories in long format (Long), including detailed information such as permissions, sizes, and last update time
  • ls -a: Display the information of all (All) subdirectories and files, including hidden directories and hidden files whose names begin with a dot ".".
  • ls -d: Display the attributes of the directory (Directory) itself, instead of displaying the contents of the directory.
  • ls -R: Display all contents in the specified directory and its subdirectories in a recursive manner.
  • ls --color: Distinguish different files by color in character mode. Normally, dark blue indicates directories, white indicates general files, green indicates executable files, yellow indicates device files, and red indicates compressed files.
    Insert picture description here
    Insert picture description here
cd command-change working directory (Change Directory)

The cd command is used to change the user's working directory to another location, usually using the target location (folder path) to be switched to as a parameter. If you do not specify the target location, the default will switch to the current user's home directory (home directory), which is the default working directory after Linux users log in to the system.
Insert picture description here

du command-statistic directory and file space occupation (Disk Usage)

The du command can be used to count the size of the disk space occupied by a specified directory (or file), using the name of the directory or file as a parameter

  • -a: Including all files when counting disk space usage, not just counting directories.
  • -s: Only the total (Summary) size of the space occupied by each parameter is counted, not the size of each subdirectory and file.
  • -h: Display the statistical results in the unit with file size (the default count is KB, but the unit is not displayed). After using the -h option, it will display the units such as KB and MB.
    Insert picture description here
Two paths of directories (relative path, absolute path)

When expressing the location of a certain directory (or file) in the Linux operating system, two different forms can be used according to the starting directory it refers to, called absolute path and relative path respectively.

  • Absolute path : This method uses the root directory "/" as the starting point. For example, "/boot/grub" means the grub directory in the boot subdirectory of the root directory. If you want to show that grub is a directory (not a file), you can add a directory separator at the end, such as "/boot/grub/". Because there is only one root directory in the Linux operating system, no matter which directory you are currently in, using an absolute path can very accurately indicate the location of a directory (or file). But if the path is long, it will be cumbersome to input.
  • Relative path : This method generally uses the current working directory as the starting point, and does not use the "/" symbol at the beginning, so it is shorter and more convenient to use.
    1. Directly use the directory name or file name to indicate the location of the subdirectory and file in the current working directory.
    For example: "grub.conf" can represent the grub.conf file in the current directory.
    2. Use a dot "." at the beginning to clearly indicate that the current working directory is the starting point.
    For example: "./grub.conf" can also mean the grub.conf file in the current directory.
    3. Use two dots "..." at the beginning to indicate that the upper-level directory (parent directory) of the current directory is used as the starting point.
    For example: if you are currently in the /boot/grub/ directory, ".../vmlinuz" is equivalent to "/boot/vmlinuz".
    4. Use the form "~~user name" at the beginning to indicate that the home directory of the specified user is used as the starting point. When the user name is omitted, it will default to the current user. For example, "~teacher" represents the home directory of the teacher user, and "~" can represent the home directory of the current user.

Fourth, create directories and files

1. mkdir command-create a new directory (Make Directory)

The mkdir command is used to create a new empty directory, using the location of the directory to be created as a parameter (there can be more than one).
If you need to create nested multi-level directories at once, you must combine the "-p" option, otherwise you can only create one level of subdirectories in an existing directory.
Insert picture description here
Insert picture description here

2, touch-create an empty file

The touch command was originally used to update the time stamp of a file, but in actual use it is often used to create a new test file. This command uses the file name as a parameter, and multiple files can be created at the same time. When the target file already exists, the time stamp of the file will be updated, otherwise an empty file with the specified name will be created.
Insert picture description here

Five, copy, delete, move directories and files

1. cp——Copy files or directories

cp [options]... source file or directory... target file or directory
cp command is used to copy files or directories, rebuild a file or directory (source) that needs to be copied and save it as a new file or directory (can be saved to other directories Middle) When multiple files or directories need to be copied, the target location must be a directory, and the target directory must already exist.

  • -f: Do not remind when overwriting the target file or directory with the same name, but directly force the copy.
  • -i: Remind the user to confirm when overwriting the target file or directory with the same name (Interactive).
  • -p: Keep (Preserve) the permissions, owner, and time stamp of the source file while copying.
  • -r: This option must be used when copying directories, which means that all files and subdirectories are copied recursively.
  • -a: This option is usually used when copying directories. It retains links, file attributes, and copies all contents under the directory.
  • -d: Keep the link when copying. The link mentioned here is equivalent to a shortcut in the Windows system.
  • -l: Do not copy files, just generate link files.

Insert picture description here
Insert picture description here

2. The rm command-remove (Remove) files or directories

The rm command is used to delete the specified file or directory. In the Linux command line interface, the deleted file is difficult to recover. Therefore, you need to be careful when deleting the file with the rm command. The rm command uses the name of the file or directory to be deleted as a parameter

  • -f: Do not remind when deleting files or directories, but directly force deletion.
  • -i: remind the user to confirm when deleting files or directories.
  • -r: This option must be used when deleting a directory, which means that the entire directory tree is deleted recursively (should be used with caution).
    For data (including directories and files) that have been determined to be no longer used, usually combined with the "-rf" option to delete directly without prompting
    Insert picture description here
3. The mv command-Move a file or directory

mv [Options]… source file or directory… target file or directory When
multiple files or directories need to be moved, the target location must be a directory, and the target directory must already exist. If you move a file or directory in the same directory, it is equivalent to performing a rename operation.
When multiple files or directories need to be moved, the target location must be a directory, and the target directory must already exist. If you move a file or directory in the same directory, it is equivalent to performing a rename operation.
Insert picture description here

Six, find directories and files

1. Which command-find the directory where the command file executed by the user is stored

The which command is used to find the Linux command program and display the specific location. The search range is mainly determined by the user's environment variable PATH (you can execute the "echo $PATH" command to view). This range is also when the Linux operating system executes commands or programs The default search path.
The which command uses the name of the command or program to be searched as a parameter. By default, the search will not continue when the first target is found. If you want to search in all search paths, you can add the "-a" option.
Insert picture description here
Note : Since there is no corresponding program file for the built-in command of Shell, when using which to find the internal command, the corresponding program will not be found.

2. Find command-find files or directories

find [Search range] [Search condition expression] The
find command is a very powerful search command in the Linux operating system. It can perform a fine search according to the target's name, type, size and other attributes. The find command uses a recursive method when searching, and its usage is quite flexible and can be quite complex.

  • Search by name: The keyword is "-name", search is performed according to the name of the target file, and wildcards "*" and "?" are allowed.
    "*" Means 0 or more special characters
    "?" Means any character
  • Search by file size: The keyword is "-size", and the search is performed according to the size of the target file. Generally, the "+" and "-" signs are used to set the size greater than or less than the specified size as the search condition. Commonly used capacity units include kB (note that k is lowercase), MB, and GB.
  • Search by file owner: The keyword is "-user", and search is performed according to whether the file belongs to the target user.
  • Search by file type: The keyword is "-type", and the search is performed according to the type of the file. The type here refers to ordinary file (f), directory (d), block device file (b), character device file (c) Wait. Block devices refer to devices that read data in blocks (such as hard disks, memory, etc.), while character devices refer to devices that read data in a single character (such as keyboard, mouse, etc.).
    Note: When setting the search condition of the find command, if you need to use the "*" or "?" wildcard, it is best to enclose the file name in double quotes to avoid the interference of the search results with the qualified files in the current directory. You
    Insert picture description here
    need to use multiple When searching for conditions, logical operators "-a" and "-o" can be used between expressions to indicate and (and) or (or) respectively.
    Insert picture description here

Guess you like

Origin blog.csdn.net/wulimingde/article/details/107560793