Pipes are an important feature in Linux and Bash scripting. A pipe allows you to send the output of one command as input to another command. This makes it easy to connect multiple commands together and create powerful one-line operations.
If you are learning Bash scripting, understanding pipes is very useful. Pipes are commonly used for text processing, log analysis, command filtering, report generation, and cybersecurity automation. Instead of saving output to a temporary file, you can pass it directly from one command to another.
A pipe is represented by the | symbol.
Basic syntax:
command1 | command2
Here, the output of command1 becomes the input of command2.
Example:
ls | grep ".txt"
This command lists files using ls and then filters only files containing .txt using grep.
Create some sample files:
touch file1.txt file2.log file3.txt
Now run:
ls | grep ".txt"
Output:
file1.txtfile3.txt
The ls command lists all files, and grep filters only the text files.
Create a Bash script:
nano pipe-example.sh
Add the following code:
#!/bin/bashecho "Text files in current directory:"ls | grep ".txt"
Save and run it:
chmod +x pipe-example.sh./pipe-example.sh
This script displays only files that contain .txt in their name.
You can connect more than two commands using pipes.
Example:
cat /etc/passwd | grep "/bin/bash" | cut -d: -f1
This command reads /etc/passwd, finds users with /bin/bash, and prints only the usernames.
A better version without unnecessary cat is:
grep "/bin/bash" /etc/passwd | cut -d: -f1
Pipes are useful for counting results.
ps aux | grep apache | wc -l
This checks running processes, filters lines containing apache, and counts them.
Another example:
ls *.txt 2>/dev/null | wc -l
This counts .txt files in the current directory.
#!/bin/bashlog_file="/var/log/auth.log"echo "Top failed SSH login IPs:"grep "Failed password" "$log_file" | awk '{print $(NF-3)}' | sort | uniq -c | sort -nr | head This script searches failed SSH login attempts, extracts IP addresses, counts them, sorts the results, and shows the top entries.
The tee command allows you to show output on the screen and save it to a file at the same time.
ls -la | tee file-list.txt
This displays the file list and saves it into file-list.txt.
Pipes in Bash are powerful for command chaining. They allow you to connect commands and process output step by step without creating temporary files.
For beginners, learning pipes is important because many real-world Bash scripts use them for filtering, counting, sorting, log analysis, and cybersecurity reporting. Once you understand pipes, you can write shorter, cleaner, and more powerful Bash scripts.
Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…
Introduction A self-signed SSL certificate is a certificate that is created and signed by the…
Introduction Debugging is an important part of Bash scripting. When a script does not work…
Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…
Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…
Introduction Working with files and directories is one of the most important skills in Bash…