← Back to Bash Course | Chapter 8: Input/Output & Redirection | Lesson 12 of 13

tee कमांड

tee command text को एक file में copy करता है जबकि इसे अभी भी screen या अगले command में pass होने देता है, एक pipe में T-junction जैसा।
Syntax
bash
command | tee file_name
command | tee -a file_name
command | tee file1 file2

Writing to a File and the Screen at Once

tee stdin से पढ़ता है और इसकी एक identical copy दोनों named file और वापस stdout पर लिखता है, इसलिए data pipeline के अगले stage की ओर flow करता रहता है और साथ ही save भी होता है।

उदाहरण: Writing to a File and the Screen at Once

bash
#!/bin/bash
echo "important result" | tee result.txt
echo "file contents:"
cat result.txt
rm -f result.txt

Appending Instead of Overwriting

Default रूप से tee target file को > की तरह ही truncate करता है। Append करने के लिए इसके बजाय -a pass करें, plain redirection के लिए > और >> के बीच के difference को mirror करते हुए।

उदाहरण: Appending Instead of Overwriting

bash
#!/bin/bash
echo "first" | tee notes.txt > /dev/null
echo "second" | tee -a notes.txt > /dev/null
cat notes.txt
rm -f notes.txt

Writing to Multiple Files

tee एक से ज़्यादा filename accept करता है और अपने input की एक identical copy हर एक को लिखता है, जो एक साथ कई logs में output फैलाने के लिए handy है।

उदाहरण: Writing to Multiple Files

bash
#!/bin/bash
echo "shared output" | tee file_a.txt file_b.txt > /dev/null
echo "file_a: $(cat file_a.txt)"
echo "file_b: $(cat file_b.txt)"
rm -f file_a.txt file_b.txt

Using tee to Inspect a Pipeline

क्योंकि tee अपने input को unchanged pass करता है, आप इसे एक existing pipeline के बीच में सिर्फ debugging के लिए एक intermediate snapshot capture करने के लिए insert कर सकते हैं, pipeline का final result बदले बिना।

उदाहरण: Using tee to Inspect a Pipeline

bash
#!/bin/bash
printf "3\n1\n2\n" | sort -n | tee sorted.txt | tr '\n' ',' 
echo
echo "snapshot saved by tee:"
cat sorted.txt
rm -f sorted.txt
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि tee हमेशा file overwrite करता है; default रूप से यह truncate करता है, लेकिन append करने के लिए tee -a चाहिए।
  2. यह भूल जाना कि tee अब भी stdout पर print करता है, इसलिए उस output को भी redirect करना confusing double-writes cause कर सकता है।
  3. यह न जानना कि tee कई filenames list करके एक साथ कई files में लिख सकता है।
चैप्टर सारांश
  • tee file stdin को file और stdout दोनों में simultaneously लिखता है।
  • tee -a file overwrite करने के बजाय append करता है।
  • tee एक साथ कई files में output duplicate करने के लिए कई filenames ले सकता है।
  • tee pipe तोड़े बिना data inspect करने के लिए mid-pipeline उपयोगी है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.