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

Q1. Find number of files in a directory

Breakdown of the command used above

The command lists all files in a directory in a single column i.e. a single line represents name of one file.

Output of ls command

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

sorted output of concatenation of two files

Q3. Unique names in a file

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

writes and displays output of the ls command

Last updated