简体   繁体   中英

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

The character > (or >>) can be used with a command in Windows to log results of a command to file. Does Linux have this ability? How do you do it?

Example: $ find /?>find.txt

This command will make a file named find.txt in the current directory. This file will contain exactly what is found in the term window after typing the command:

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.

It works the same way in Linux. > will overwrite the output file, >> will append to it. Some programs will print errors to STDERR, which you can capture using 2> . Sometimes you will see STDERR redirected to the same location as STDOUT using 2>&1 , so that all output can be captured at once.

Yes, you can redirect output from a command using > or >> in the shell on Linux as well. See the section on redirection in your shell documentation (link goes to the POSIX standard, other shells may support more advanced types of redirection).

echo "This is a test" > file.txt

If you want to print output to both a file and the terminal, you can use tee :

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

Note that given the document you link to, grep is probably the closest equivalent to the find command listed. find on Linux/Unix will recursively search for files with names or other metadata matching conditions given; grep will search through a single file for lines matching a given pattern.

Yes, and it looks the same:

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

It work the same.

find / > log.txt

A extra bonus could be found with the tee command, this will save to a file and show the output the same time.

find / | tee log.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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