Pipes are used to redirect a stream from one program to another program. The output of one command redirect it to the input of another. It allows us to combine multiple commands.
Pipe Syntax
We use |
symbol to separate two commands. By using the |
operator, the output of the first command is passed/redirected to the second command. Syntax: command1 | command2
To find the no of files present in a directory.
1
Input: ls -1 | wc -l
To combine both file content using cat and sort it
Suppose we have two files:- namex.txt and country.txt
1 2 3 4 5 6 7 8 9
#names Leonard Sheldon Raj Amy Howard Bernadette Penny Leonard
#country Spain England Italy France Brazil Argentina
1 2 3
Now, to combine both file content using cat and sort it.
Input: cat names.txt country.txt | sort > abc.txt Output:
Amy Argentina Bernadette Brazil England France Howard Italy Leonard Leonard Penny Raj Sheldon Spain ```Find unique records from a file
To use the
uniq
command first the data must be in a sorted order.1 2 3 4 5 6 7 8 9
Input: cat namex.txt | sort | uniq Output: Amy Bernadette Howard Leonard Penny Raj Sheldon
How to see only range of lines in a file
1 2 3 4 5 6 7
Input: cat fakenames.txt | head -5 Output: 1,Gregory Mueller 2,Mark Jacobs 3,Amanda Cooper 4,Isaac Conrad 5,Ashley Berg
1 2 3 4 5 6 7
Input: cat fakenames.txt | head -10 | tail -5 Output: 6,Nicholas Clark 7,Jamie Wright 8,Kevin Landry 9,Stefanie Johnson 10,Annette Johnson
How to use
more
andless
commandWe use these command when there is large amount of output/data to see in the terminal.
- The
more
command displays the output/data from the start and then shows page by page afterwards.
1
Input: ls -1 | more
- The
less
command displays the output/data from the start and then shows in a file type.
1
Input: ls -1 | less
we can also search by presing the
/
and the typing the search keyword.- The
tee
command in LinuxThe
tee
command reads the standard input and copies it both to stdOutput and to a file. We can see the information going through pipeline.
1
2
3
```
Input: ls | tee files.txt
```
XARGS
command in LinuxThe
xargs
command converts the standard input into command line arguement.1 2 3 4 5
Input: ls | xargs echo Output: country.txt fakenames.txt files.txt names.txt sorted_names_country.txt Input: ls | xargs echo "Hello" Output: Hello country.txt fakenames.txt files.txt names.txt sorted_names_country.txt
Example: Suppose we have a file named “FileNames.txt”
1 2 3 4 5 6
#FileNammes file1 file2 file3 file4 file5
Now, suppose we want to take those names from ‘FileNames.txt’ and create a new file for each of those filenames.
1
Input: cat FileNames.txt | xargs touch
Here, the output of
cat FileNaes.txt
is converted into an command line argument using the xargs and passed it to thetouch
command using the|
operator.