1. What is Pipe?
In Linux, a pipe (`|`) is used to connect the output of one command to the input of another command.
It passes the output from the first command as input to the second command.
In simple words:
A pipe works like a water pipe — it carries the result from one command and gives it to another command.
2. Syntax of Pipe:
command1 | command2
– command1: The first command (gives output).
– | : Pipe symbol.
– command2: The second command (takes input from the first one).
3. Examples of Pipe:
- Example 1:
ls | sort
– ls: shows the list of files.
– sort: arranges the list in alphabetical order.
This command shows the files in sorted order.
- Example 2:
cat file.txt | grep “apple”
– cat file.txt: Shows the content of the file.
– grep “apple”: Filters and shows only lines that contain the word “apple”.
This command displays only the lines with “apple” in the file.
4. Why Use Pipe? (Advantages)
– Connects two or more commands easily.
– Saves time.
– Makes the commands powerful and flexible.
– Reduces the need to use temporary files.
5. Example with Multiple Pipes:
ls | sort | head -n 5
– ls: List all files.
– sort: Sorts them alphabetically.
– head -n 5: Shows only the first 5 files.
This command gives you the first 5 files in sorted order.
Exam Answer Format:
Question: What is Pipe in Linux? Explain with example.
- Answer:
In Linux, a pipe (`|`) is used to pass the output of one command as input to another command.
Syntax:
command1 | command2
Example:
ls | sort
This command lists all files and sorts them in alphabetical order.
Pipes are useful for combining simple commands to perform complex tasks efficiently.
Linux में Pipe
1. Pipe क्या है?
Linux में pipe (`|`) का इस्तेमाल एक कमांड के output को दूसरी कमांड के input से जोड़ने के लिए किया जाता है।
यह पहली कमांड का output लेता है और उसे दूसरी कमांड को input के रूप में दे देता है।
सरल भाषा में:
Pipe एक पानी की पाइप की तरह काम करता है — यह एक कमांड से निकला डेटा दूसरी कमांड तक पहुँचाता है।
2. Pipe का सिंटैक्स:
command1 | command2
– command1: पहली कमांड (output देती है)।
– | : Pipe symbol।
– command2: दूसरी कमांड (पहली कमांड से input लेती है)।
3. Pipe के उदाहरण:
- उदाहरण 1:
ls | sort
– ls: फाइलों की लिस्ट दिखाता है।
– sort: उस लिस्ट को वर्णानुक्रम में सजाता है।
यह कमांड फाइलों को sorted order में दिखाता है।
- उदाहरण 2:
cat file.txt | grep “apple”
– cat file.txt: फाइल का कंटेंट दिखाता है।
– grep “apple”: सिर्फ वही लाइनें दिखाता है जिनमें “apple” शब्द होता है।
यह कमांड केवल उन लाइनों को दिखाएगी जिनमें “apple” शब्द है।
4. Pipe के फायदे:
– दो या ज्यादा कमांड्स को आसानी से जोड़ सकते हैं।
– समय की बचत होती है।
– कमांड्स को ज़्यादा पावरफुल और flexible बनाता है।
– Temporary files की ज़रूरत कम हो जाती है।
5. एक से ज्यादा Pipe का उदाहरण:
ls | sort | head -n 5
– ls: सभी फाइलों की लिस्ट देता है।
– sort: उन्हें वर्णानुक्रम में सजाता है।
– head -n 5: केवल पहली 5 फाइलें दिखाता है।
यह कमांड sorted लिस्ट में से पहली 5 फाइलें दिखाती है।
परीक्षा उत्तर प्रारूप:
प्रश्न: Linux में Pipe क्या है? उदाहरण सहित समझाइए।
- उत्तर:
Linux में pipe (`|`) का उपयोग एक कमांड के output को दूसरी कमांड के input के रूप में देने के लिए किया जाता है।
Syntax:
command1 | command2
उदाहरण:
ls | sort
यह कमांड सभी फाइलों को लिस्ट करता है और उन्हें वर्णानुक्रम में sort करता है।
Pipe का उपयोग करके हम कई साधारण कमांड्स को जोड़कर जटिल काम आसान बना सकते हैं।