Skip to main content

How to Use the tee Command in Linux: Step-by-Step Guide

HardReset.InfoDecember 31, 20256 min14 views
2 connections·4 entities in this video→

Understanding the tee Command

  • πŸ’‘ The tee command in Linux is used to display output on the terminal while simultaneously writing that same output to one or more files.
  • 🎯 It's incredibly useful when you need to see command results in real-time and also preserve them for later review.

Basic Usage and Overwriting

  • πŸš€ To use tee, you typically pipe the output of another command into it, followed by the desired filename (e.g., ls | tee output.txt).
  • ⚠️ By default, tee will overwrite the content of the specified file if it already exists.
  • πŸ“ For example, piping echo hello to tee file.txt will result in file.txt containing only 'hello'.

Appending Output with tee -a

  • βž• To append output to a file instead of overwriting it, use the -a option (e.g., echo next line | tee -a file.txt).
  • πŸ“„ This ensures that new output is added to the end of the file, preserving previous content.

Writing to Protected System Files with sudo tee

  • πŸ”‘ When you need to write to protected system directories (like /etc/), you can use sudo tee.
  • βš™οΈ The syntax echo test | sudo tee /etc/test.conf allows you to write content to system files after entering your password.

Writing to Multiple Files and Saving Disk Usage

  • πŸ—„οΈ The tee command can write output to multiple files simultaneously by listing them after the command (e.g., ls | tee file1.txt file2.txt).
  • πŸ“Š A practical example is saving disk usage information using df -h | tee disk-usage.txt, which displays the output and saves it to disk-usage.txt.
Knowledge graph4 entities Β· 2 connections

How they connect

An interactive map of every person, idea, and reference from this conversation. Hover to trace connections, click to explore.

Hover Β· drag to explore
4 entities
Chapters3 moments

Key Moments

Transcript25 segments

Full Transcript

Topics11 themes

What’s Discussed

tee commandLinuxLinux CommandsTerminalPipesFile AppendingFile Overwritingsudo teeSystem FilesDisk UsageCommand Output
Smart Objects4 Β· 2 links
ProductsΒ· 3
ConceptΒ· 1