繁体   English   中英

是否有一个等效于Windows> log.txt的Linux,用于将命令输出记录到文件中?

[英]Is there a Linux equivalent to Windows >log.txt for logging command output to a file?

字符>(或>>)可与Windows中的命令一起使用,以将命令结果记录到文件中。 Linux有此功能吗? 你怎么做呢?

示例:$ find /?>find.txt

此命令将在当前目录中创建一个名为find.txt的文件。 键入命令后,此文件将包含在术语窗口中找到的内容:

Searches for a text string in a file or files.

FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][path]filename[ ...]]

  /V         Displays all lines NOT containing the specified string.
  /C         Displays only the count of lines containing the string.
  /N         Displays line numbers with the displayed lines.
  /I         Ignores the case of characters when searching for the string.
  /OFF[LINE] Do not skip files with offline attribute set.
  "string"   Specifies the text string to find.
  [drive:][path]filename
             Specifies a file or files to search.

If a path is not specified, FIND searches the text typed at the prompt
or piped from another command.

它在Linux中的工作方式相同。 >将覆盖输出文件, >>将追加到该文件。 有些程序会将错误输出到STDERR,您可以使用2>捕获错误。 有时,您会看到使用2>&1将STDERR重定向到与STDOUT相同的位置,以便可以立即捕获所有输出。

是的,您也可以在Linux上的Shell中使用>>>重定向命令的输出。 请参阅外壳程序文档中有关重定向的部分(链接指向POSIX标准,其他外壳程序可能支持更高级的重定向类型)。

echo "This is a test" > file.txt

如果要将输出打印到文件和终端,可以使用tee

echo "This is a test" | tee file.txt

请注意,给定链接到的文档, grep可能与所列的find命令最接近。 在Linux / Unix上, find会递归搜索名称或其他符合条件的元数据的文件; grep将在单个文件中搜索与给定模式匹配的行。

是的,看起来也一样:

find --help > find.txt  # write to a new file
find --help >> find.txt # append to the file

它的工作原理相同。

find / > log.txt

可以使用tee命令找到额外的奖励,这将保存到文件中并同时显示输出。

find / | tee log.txt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM