The tee command Linux

Sometimes we need to save the command output to a file to later use the pin for other purposes. Team tee in Linux you need to record the output of any command in one or more files.

The essence of her work can be described by the capital letter T — the linux tee command takes data from one source and can save their output in a few places. How it can be used in Linux, well illustrated by several examples, which are given below.

The syntax of the command tee

The command syntax is fairly simple:

$ tee options file

Command options:

  • -a or -append – Used to output records to the end of existing file.
  • -i or -ignore-interrupts – Used to ignore interrupt signals.
  • -help – Used to display all possible operations.
  • -version – Used to display the current version of this team.

To save the command output, you can transfer one or more files.

Examples of usage

1. Easy to use tee command

The command ls-la is needed in Linux to display the list of files in the current directory. Here the team tee is used to save the output of ls-la in the file вывод.txt Type the following commands in the terminal to check how the team tee.

ls-la | tee вывод.txt

cat вывод.txt

Here, the first command showed the list of files in the current directory in the terminal and recorded the data to the file вывод.txt

The second team showed the contents of the file вывод.txt

2. Add output to an existing file

If the output of any command to write to an existing file by using the command tee and operations -a, the contents of the file will not be overwritten. Here the output of the command pwd will be added to the end of the file output.txt Run the following commands using terminal:

pwd | tee -a вывод.txt

cat вывод.txt

Here the first command shows the output of pwd into the terminal and writes the output to the end of the file вывод.txt the Second command is used to select an output file. It is shown that the file вывод.txt contains both of the result from the previous example and this one.

3. Record output in several files

The command tee can be used to output any commands to multiple files. This requires you to specify the names of the files separated by a space. Use the following commands to save the output of date in two files: вывод1.txt and вывод2.txt

date | tee вывод1.txt вывод2.txt

cat вывод1.txt вывод2.txt

Here the first command displays the current system time and stores the results in two files вывод1.txt and вывод2.txt the Second command shows the same content both files.

4. Ignore the interrupt signal

Team tee linux with the option -i is used in this example to ignore any interrupts during its execution. Thus, the command is executed correctly even if the user presses CTRL+C. Run the following commands in the terminal.

wc-l вывод.txt | tee -i вывод3.txt

cat вывод.txt

cat вывод3.txt

Here, the first command counts the number of lines in the file вывод.txt and saves the result to a file вывод3.txt the Second command shows the contents of the file вывод.txtthat contains 37 lines. The third command shows the contents of the file вывод3.txtthat indicates that it actually contains 37 lines.

5. The transfer of the output of tee to another team

The output of the command tee can be moved to another team. In this example, the output of the first command is transferred to the tee, and its output in another command. Run the following commands in the terminal:

ls | tee вывод4.txt | wc-lcw

 ls

cat вывод4.txt

Here, the first command is used to record the output of ls to a file вывод4.txt and counting the total number of lines, words and characters in a file вывод4.txt the Second command is used to display the output of the command lsand the third for displaying the contents of the file вывод4.txt

6. The tee command and scripts

The command tee can also be used to record the output of the bash script to a file. Create a bash file with the following code, which take two input numbers from the command-line arguments and displays the sum of these numbers. The team tee is used in this example to record the output add.sh in file результат.txt

add.sh

#!/bin/bash
a=$1
b=$2
((result=$a+$b))
echo "The addition of $a+$b=$result"

Run the following command in terminal to write to a file and display its content.

bash add.sh 40 80 | tee результат.txt

cat результат.txt

Here, the numbers 40 and 80 are transferred as command-line arguments to the script add.sh and the output is written to a file результат.txt the Command cat displays the result.

7. How to hide command output in the terminal

If you want to record the output straight to file and not show it in the terminal, use /dev/null with the command tee. To do this, run the following command.

df | tee вывод5.txt > /dev/null

cat вывод5.txt

Here, the first command is used to record the output of the command df to a file вывод5.txtwhile the output is not displayed in the terminal. The second command shows the result.

The results

The output of any command can be used for several purposes. Using the command tee with different options, you can save the output in multiple files. Use of tee in the most common cases is well shown in this statement with examples. I hope the reader will be given useful exercises.

Source: losst.ru

(Visited 17 times, 1 visits today)