pipe, greater, smaller ( |, >, < )

What is pipe "|" ?

The pipe | is used most often within Linux shell commands or scripts. The use of the pipe command is to pass data from 1 tool to another, underneath are some practical examples.

Note that this is possible not only with the file /etc/passwd, but with all files or output from tools!

How to use |

##### Simple examples
cat /etc/passwd | head -n 5 # print the 5 first lines of the file /etc/passwd
cat /etc/passwd | head -n 5 | tail -n 1 # print the last line of the 5 first lines of the file /etc/passwd

cat /etc/passwd | wc -w # counts how many words are present in the file /etc/passwd
cat /etc/passwd | head -n 2 | wc -c # counts how many characters are present in the first 2 lines of /etc/passwd
cat /etc/passwd | more # pipes the text to the tool : more

##### Medium examples
cat /etc/passwd | cut -c 1-50 > test.txt # get the first 50 characters of all lines in the file /etc/passwd and output it to test.txt
cat /etc/passwd | cut -d : -f  1-3 | tee test.txt # get the first 3 fields of the file /etc/passwd and make a copy of that in the file test.txt

##### Hard examples
cat /etc/passwd | sed 's/nologin/NOLOGIN/g' | tee test.txt | cut -d : -f 1-4 | tee test2.txt # get 1 file with all "nologin" changed to "NOLOGIN" and 1 file with the first 4 fields of the file /etc/passwd
cat /etc/passwd | grep -i -e root -e www-data | tee test.txt | sort -r | tee test2.txt # search for the users root and www-data, append that to a file, reverse sort it and append that to a file

what is stdout (">") - greater then

Greater then, within a Linux environment is used to write results to files.

Note that when the command > is not once but, 2 times used on the same file, the results of the first write will be overwritten with the results of the second time the command has been used. In order to not overwrite, but append more results to the same file, the command ">>" can be used.

how to use >

##### example 1
echo "Hamburgers in de aanbieding" > test.txt # file contains 1 line, with the text : Hamburgers in de aanbieding 
echo "Hamburgers zijn duurder geworden" > test.txt # file contains 1 line, with the text : Hamburgers zijn duurder geworden

##### example 2
echo "hamburgers met korting" > test.txt # file contains 1 line, with the text : hamburgers in de aanbieding 
echo "Neem maar een salade" >> test.txt # file contains 2 lines, with the text : hamburgers met korting \n Neem maar een salade

##### example 3
cat /etc/passwd | wc -c > test.txt # the total characters of the file /etc/passwd will be written in the file test.txt

Write a file with ">" (Not the best option)

In order to write a file with the > operator, checkout the following.

cat > test.txt # from this point, you have an interactive shell, type something like test123, end the session with CTRL+D 
test 1234

cat test.txt # output : test 1234

what is stdin ("<") - smaller then

Smaller then < , within a linux environment is used to get input from files or commands. This is not needed very often.

echo "I Just came to say hello!" > test.txt
cat < test.txt 
# this tells cat to take input from the file first instead of the keyboard (this is of course the same as cat test.txt)

Last updated