tee कमांड
In this page:
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
#!/bin/bash
echo "important result" | tee result.txt
echo "file contents:"
cat result.txt
rm -f result.txt
Login to try C/C++/Java/PHP code in the editor
Appending Instead of Overwriting
Default रूप से tee target file को > की तरह ही truncate करता है। Append करने के लिए इसके बजाय -a pass करें, plain redirection के लिए > और >> के बीच के difference को mirror करते हुए।
उदाहरण: Appending Instead of Overwriting
#!/bin/bash
echo "first" | tee notes.txt > /dev/null
echo "second" | tee -a notes.txt > /dev/null
cat notes.txt
rm -f notes.txt
Login to try C/C++/Java/PHP code in the editor
Writing to Multiple Files
tee एक से ज़्यादा filename accept करता है और अपने input की एक identical copy हर एक को लिखता है, जो एक साथ कई logs में output फैलाने के लिए handy है।
उदाहरण: Writing to Multiple Files
#!/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
Login to try C/C++/Java/PHP code in the editor
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
#!/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
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
teeहमेशा file overwrite करता है; default रूप से यह truncate करता है, लेकिन append करने के लिएtee -aचाहिए। - यह भूल जाना कि
teeअब भी stdout पर print करता है, इसलिए उस output को भी redirect करना confusing double-writes cause कर सकता है। - यह न जानना कि
teeकई filenames list करके एक साथ कई files में लिख सकता है।
tee filestdin कोfileऔर stdout दोनों में simultaneously लिखता है।tee -a fileoverwrite करने के बजाय append करता है।teeएक साथ कई files में output duplicate करने के लिए कई filenames ले सकता है।teepipe तोड़े बिना data inspect करने के लिए mid-pipeline उपयोगी है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- Function Basics
- The read Command and Here-Strings
- Function Arguments
- stdin, stdout, stderr and File Descriptors
- Return Values
- Redirection Operators
- Local Variables
- Pipes and Chaining Commands
- Recursive Functions
- Here-Documents (<<EOF)
- Function Libraries
- The tee Command
- Discarding Output with /dev/null