


1. Overview
(1) Redirect command list
Command | Description |
---|---|
Redirect output to file | |
Redirect output to append The method redirects to file | |
Redirects the file with file descriptor n to file | |
Redirect the file with file descriptor n to file | |
Merge output files m and n | |
(2) File description Symbol
- 0: Usually standard input (STDIN)
- 1: Standard output (STDOUT)
- 2: It is the standard error output (STDERR)
(1) Command analysis
command > file #执行command然后将输出的内容存入file。
Note:
The content in the file will be replaced by the new content. If you don’t want to replace everything but append to the end of the file, use > ;> operator.(2) Example analysis
[root@localhost ~]# w 20:41:36 up 55 days, 5:17, 1 user, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 223.166.20.53 20:24 0.00s 0.05s 0.00s w [root@localshost ~]# w > users #w命令执行后的结果输出到users文件中 [root@localshost ~]# ll -rw-r--r-- 1 root root 204 Jan 3 20:41 users [root@localshost ~]# cat users #查看users文件内容,正是w命令执行后的输出结果 20:41:58 up 55 days, 5:17, 1 user, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 223.166.20.53 20:24 6.00s 0.05s 0.00s w [root@localshost ~]# who root pts/0 2021-01-03 20:24 (223.166.20.53) [root@localshost ~]# who > users #将who命令执行结果输出重定向到users文件 [root@localshost ~]# cat users #发现users文件中原先w命令的输出内容被who命令覆盖了 root pts/0 2021-01-03 20:24 (223.166.20.53) [root@localshost ~]# echo "Hello world" >> users #使用>>操作符则会追加在后面输出 [root@localshost ~]# cat users root pts/0 2021-01-03 20:24 (223.166.20.53) Hello world [root@localshost ~]#
Note:
There are two #s in many command lines in the above examples:- The first # means
The current user is the root user (when it is another user, there will be a $ sign here) ;
- The second # above means the meaning of
comment .
command < file #获取file文件中的内容作为输入内容,并用于commmand执行
Note:
The output redirection is the greater than sign (>), and the input redirection is the less than sign (2. Example analysis[root@localhost ~]# wc -l users
2 users
#wc指令可以计算文件的Byte数、字数、或是行/列数,若不指定文件名称、或是所给予的文件名为"-",则wc指令会从标准输入设备读取数据。
[root@localhost ~]# wc -l < users
2
#将输入重定向到 users 文件,上面命令作用就是将users文件内容作为输入重定向计算行数了
Note:
The first example will output the file name; The second one No, because it only knows to read from standard input.command < infile > outfile #同时替换输入和输出,执行command,从文件infile读取内容,然后将输出写入到outfile中。 #理解成,从标准输入中获取内容(输入重定向到infile)作为标准输出(输出重定向到outfile)。4. In-depth understanding of redirectionThe standard input, standard output and standard error output have been briefly introduced in the above file descriptor, and will be explained in detail below. 1. IntroductionGenerally, three files will be opened when each Unix/Linux command is run:
1) Standard input file (stdin) :
The file descriptor of stdin is 0, and Unix programs read data from stdin by default.2) Standard output file (stdout):
The file descriptor of stdout is 1, and Unix programs output data to stdout by default.3) Standard error file (stderr):
The file descriptor of stderr is 2, and Unix programs will write error information to the stderr stream. By default,command > file redirects stdout to file, command
2. Detailed explanation of commandscommand 2>file
#stderr 重定向到 file
command 2>>file
#stderr 追加到 file 文件末尾
command > file 2>&1
command >> file 2>&1
#stdout 和 stderr 合并后重定向到 file
command < file1 >file2
#对 stdin 和 stdout 都重定向
#command 命令将 stdin 重定向到 file1,将 stdout 重定向到 file2
5. Here DocumentHere Document is a special redirection method in Shell, used to redirect input to an interactive shell script or program. 1. Grammar Its basic form is as follows: command << delimiter document delimiter #作用是将两个 delimiter 之间的内容(document) 作为输入传递给 command。
Note: The delimiter at the end of
must be written in top case , there cannot be any characters in front of it, nor can there be any characters in the back, including spaces and tab indentations. The spaces before and after the starting delimiter will be ignored. 2. Example analysisEOF is the abbreviation of END Of File, which represents a custom terminator. Since it can be customized, EOF does not have a fixed value and you can set an alias at will. For example, in Linux, pressing Ctrl-D can be used as EOF instead. EOF usually works with cat to output multi-line text.The examples are as follows:
[root@localhost ~]# wc -l << EOF > a > b > c > d > e > EOF 5 #输入内容为5行 [root@localhost ~]# cat << EOF > a > b > c > d > e > f > EOF a b c d e fEOF can also be customized as follows:
[root@iZ2ze95cxr3kx9il409khtZ ~]# cat << CCC > a > b > c > d > CCC a b c dWhen executing script input, you can use the following form:
#拥有大量输入的时候可以用下面的形式,将标准输入的内容重定向到(输入到)test.sh文件中。 [root@localhost ~]# cat << EOF >test.sh > 123123123 > 3452354345 > asdfasdfs > EOF [root@localhost ~]# cat test.sh 123123123 3452354345 asdfasdfs [root@localhost ~]#
The above is the detailed content of How to solve the redirection problem in linux operation. For more information, please follow other related articles on the PHP Chinese website!

MaintenanceModeinLinuxisaspecialbootenvironmentforcriticalsystemmaintenancetasks.Itallowsadministratorstoperformtaskslikeresettingpasswords,repairingfilesystems,andrecoveringfrombootfailuresinaminimalenvironment.ToenterMaintenanceMode,interrupttheboo

The core components of Linux include kernel, file system, shell, user and kernel space, device drivers, and performance optimization and best practices. 1) The kernel is the core of the system, managing hardware, memory and processes. 2) The file system organizes data and supports multiple types such as ext4, Btrfs and XFS. 3) Shell is the command center for users to interact with the system and supports scripting. 4) Separate user space from kernel space to ensure system stability. 5) The device driver connects the hardware to the operating system. 6) Performance optimization includes tuning system configuration and following best practices.

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.

Linux maintenance mode can be entered through the GRUB menu. The specific steps are: 1) Select the kernel in the GRUB menu and press 'e' to edit, 2) Add 'single' or '1' at the end of the 'linux' line, 3) Press Ctrl X to start. Maintenance mode provides a secure environment for tasks such as system repair, password reset and system upgrade.

The steps to enter Linux recovery mode are: 1. Restart the system and press the specific key to enter the GRUB menu; 2. Select the option with (recoverymode); 3. Select the operation in the recovery mode menu, such as fsck or root. Recovery mode allows you to start the system in single-user mode, perform file system checks and repairs, edit configuration files, and other operations to help solve system problems.

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools