🧭 What is Input and Output Redirection in Linux?
📌 Redirection means:
Sending the output or taking the input from a source other than default.
By default:
- Input is taken from: Keyboard
- Output is shown on: Screen (Monitor)
But Redirection allows:
- Taking input from a file instead of keyboard
- Sending output to a file instead of screen
🎯 Types of Redirection:
🔢 No. | Type of Redirection | Symbol | Meaning | Example |
---|---|---|---|---|
1. | Input Redirection | < | Takes input from a file | wc < myfile.txt |
2. | Output Redirection | > | Sends output to a file (overwrite) | ls > list.txt |
3. | Append Output | >> | Sends output to a file (append to end) | echo "Hi" >> notes.txt |
4. | Error Redirection | 2> | Sends error messages to a file | ls wrongfile 2> errorlog.txt |
5. | Output and Error Both | &> | Sends both output and error to a file | command &> all_output.txt |
📘 Examples:
✅ Input Redirection:
wc < input.txt
- It reads input from
input.txt
instead of keyboard.
✅ Output Redirection:
ls > files.txt
- The result of
ls
is saved in files.txt, not shown on screen.
✅ Append Output:
echo "New line" >> log.txt
- Adds “New line” to the end of
log.txt
.
📌 Short Exam Note:
Redirection in Linux means changing the default input/output behavior of commands.
📍 Types of Redirection:
<
→ Input from file>
→ Output to file (overwrite)>>
→ Output to file (append)2>
→ Error to file&>
→ Output and Error both to file
Examples:
ls > out.txt
wc < input.txt
echo "done" >> notes.txt
🧭 Linux में Input और Output Redirection क्या है?
📌 परिभाषा:
Redirection (रिडायरेक्शन) का मतलब है —
किसी कमांड की इनपुट या आउटपुट को डिफ़ॉल्ट जगह से हटाकर किसी दूसरी जगह से लेना या भेजना।
🎯 सामान्यतः:
क्र. | प्रकार | डिफ़ॉल्ट |
---|---|---|
✅1 | Input | कीबोर्ड |
✅2 | Output | स्क्रीन |
लेकिन Redirection के द्वारा:
- इनपुट को फाइल से लिया जा सकता है।
- आउटपुट को स्क्रीन की जगह किसी फाइल में सेव किया जा सकता है।
🔁 Redirection के प्रकार और उनके चिन्ह
🔢 क्र.सं. | प्रकार | चिन्ह (Symbol) | कार्य (फंक्शन) | उदाहरण |
---|---|---|---|---|
1. | Input Redirection | < | इनपुट को फाइल से लेना | wc < data.txt |
2. | Output Redirection | > | आउटपुट को फाइल में सेव करना (ओवरराइट करता है) | ls > file.txt |
3. | Append Output | >> | आउटपुट को फाइल के अंत में जोड़ना (Append करना) | echo "Hello" >> notes.txt |
4. | Error Redirection | 2> | एरर मैसेज को किसी फाइल में भेजना | ls wrongfile 2> error.txt |
5. | Output और Error दोनों | &> | आउटपुट और एरर दोनों को एक ही फाइल में सेव करना | command &> result.txt |
🧪 प्रमुख उदाहरण:
✅ इनपुट रिडायरेक्शन:
bashCopyEditwc < input.txt
यह कमांड input.txt से डेटा पढ़ेगा न कि कीबोर्ड से।
✅ आउटपुट रिडायरेक्शन:
bashCopyEditls > files.txt
यह कमांड ls
का आउटपुट files.txt में सेव करेगी, स्क्रीन पर नहीं दिखेगा।
✅ अपेंड (जोड़ना):
bashCopyEditecho "नया लाइन" >> log.txt
यह log.txt फाइल के अंत में नई लाइन जोड़ देगा।
🧠 याद रखने की ट्रिक (Symbols)
चिन्ह | अर्थ |
---|---|
< | इनपुट फाइल से लो (कमांड में अंदर) |
> | आउटपुट को फाइल में दो (बाहर भेजो) |
>> | फाइल में जोड़ो (Append करो) |
2> | एरर को फाइल में भेजो |
&> | आउटपुट + एरर दोनों फाइल में भेजो |
📝 परीक्षा के लिए शॉर्ट नोट:
Redirection Linux में उस प्रक्रिया को कहते हैं, जिसमें कमांड की इनपुट या आउटपुट को डिफ़ॉल्ट से बदलकर किसी फाइल या डिवाइस में भेजा जाता है।
मुख्य प्रकार:
<
→ Input फाइल से लेना>
→ Output फाइल में देना (overwrite)>>
→ Output जोड़ना (append)2>
→ Error फाइल में भेजना&>
→ Output और Error दोनों को सेव करना