INTERACTIVE LAB

systemsVIP0 次浏览 更新于 2026-09-29

重定向

系统讲解“重定向”的核心概念、常用方法与实践要点。

重定向

重定向指的是将命令行输出写入指定位置。

  • cmd1 | cmd2:Pipe; take standard output of cmd1 as standard input to cmd2.
  • > file:Direct standard output to file.
  • < file:Take standard input from file.
  • >> file:Direct standard output to file; append to file if it already exists.
  • >| file:Force standard output to file even if noclobber is set.
  • n>| file:Force output to file from file descriptor n even if noclobber is set.
  • <> file:Use file as both standard input and standard output.
  • n<> file:Use file as both input and output for file descriptor n.
  • << label:Here-document; see text.
  • n > file:Direct file descriptor n to file.
  • n < file:Take file descriptor n from file.
  • n >> file:Direct file descriptor n to file; append to file if it already exists.
  • n>&:Duplicate standard output to file descriptor n.
  • n<&:Duplicate standard input from file descriptor n.
  • n>&m:File descriptor  n is made to be a copy of the output file descriptor.
  • n<&m:File descriptor  n is made to be a copy of the input file descriptor.
  • &>file:Directs standard output and standard error to file.
  • <&-:Close the standard input.
  • >&-:Close the standard output.
  • n>&-:Close the output from file descriptor  n.
  • n<&-:Close the input from file descriptor  n.
  • n>&word:If  n is not specified, the standard output (file descriptor 1) is used. If the digits in word do not specify a file descriptor open for output, a redirection error occurs. As a special case, if n is omitted, and word does not expand to one or more digits, the standard output and standard error are redirected as described previously.
  • n<&word:If word expands to one or more digits, the file descriptor denoted by  n is made to be a copy of that file descriptor. If the digits in word do not specify a file descriptor open for input, a redirection error occurs. If word evaluates to -, file descriptor n is closed. If n is not specified, the standard input (file descriptor 0) is used.
  • n>&digit-:Moves the file descriptor digit to file descriptor  n, or the standard output (file descriptor 1) if n is not specified.
  • n<&digit-:Moves the file descriptor digit to file descriptor  n, or the standard input (file descriptor 0) if n is not specified. digit is closed after being duplicated to n.

>用来将标准输出重定向到指定文件。

$ ls -l /usr/bin > ls-output.txt

如果重定向后的指定文件已经存在,就会被覆盖,不会有任何提示。

如果命令没有任何输出,那么重定向之后,得到的是一个长度为0的文件。因此,>具有创建新文件或改写现存文件、将其改为长度0的作用。

该实验为 VIP 专享内容,
请开通后继续阅读后续 70% 并运行完整示例~

DISCUSSION

评论 0

还没有评论,来留下第一条吧。