3️⃣Pipe in Linux
Pipes are used to redirect a stream from one program to another program
-> The output of one commad redirect it to the input of other command

Pipe syntax
We use '|' symbol to separate two commands. Output of first command passed to second command
command1 | command2


Q1. Find number of files in a directory
ls -1 | wc -l
Breakdown of the command used above
ls -1
The command lists all files in a directory in a single column i.e. a single line represents name of one file.

wc -l
This prints the number lines in file. Together the commands will print the number of files in a directory.

Q2. Combine two files and sort them
Combining 2 files using cat command and then sorting them using pipe
cat file1.txt file2.txt | sort


Q3. Unique names in a file
cat file3.txt | sort | uniq
We can only use uniq
command with sorted data only


Q4. See 30-37 lines in a file
head -37
to view first 37 lines , tail -7
to view last 7 lines from the 37 lines, which makes 30-37

Tee command -
tee
command reads from stdin and saves the content to a file and also displays it to stdout


Last updated