Whenever we write any command on command line. There may be 3 streams:

  • Input
  • Output
  • Error



Piping and redirection is the means by which we may connect these streams between programs and files to direct data in interesting and useful ways.

  1. Wildcards

  2. Pipes

  3. Redirection

Wildcard in Linux

Wildcards are a set of building blocks that allow you to create a pattern defining a set of files or directories.

Here is the basic set of wildcards:

  • * – represents zero or more characters
  • ? – represents a single character
  • [] – represents a range of characters

 Wildcards may be used with any command.

  • *

It represents zero or more characters. If you want to list all the files and directories then this wildcard is very beneficial.In simple words, whenever you use * means it will include all the character.

ls

E.g.  if you want to list all the directory which starts with D. Then you just have to write.

ls D*

it will list all the directories and files of those directories which starts with D.

wildcard in linux

suppose you want to search all files with extension png.

ls path *.png

Here path is from where you want to list all png.

wildcard

  • ? –

it represents one character. This wildcard is used as a filter like if you want to search a directory whose second letter is o or whose extension is of 3 words.. etc. It is mainly used with * wildcard.

how to filter in linux

it means list all the file whose first letter is unknown but second character is o and I do not care about remaining characters in the file name.

wildcard in linux

it means I do not care about the file name. I care about extension only whose length consists of 3 characters.

  • [] –

It represents the range of characters. so, it is also called range operator.

suppose you want to see all files or folders whose name starts with M, V, and f.

[] in linux

or you want to list all files which are in the range between A-F.

* in linux

I guess you got the basic understanding about wildcard.

 PIPE

It is used for sending data from one program to another and the operator we use is ( | ). What this operator does is feed the output from the program on the left as input to the program on the right.


The general syntax for pipes is:command_1 | command_2 [| command_3 . . . ]

This chain can continue for any number of commands or programs.

What this operator does is feed the output from the program on the left as input to the program on the right.

pipe in linux

First, we have listed all the directory using ls command. Then we have used pipe. So, all the output of ls is now as input for head -3 command.

This chain can continue for any number of commands or programs with any commands.

Redirection

linux output to file

  
whenever we execute any command, it shows its output on the terminal window only. But, if you want this output to be saved in a file then the concept of redirection comes in picture.

It is mainly of 3 types: 

  • STDOUT


> (Greater than) is used for redirecting output to a file


stdout in linux

so whenever we use >, the output is saved to a new file. But if you want to redirect its output to an existing file then you have to use >> else your previous data will be lost.

linux output to file

TO verify this you can see below screenshot.

stdout


 Now appended content screenshot

append content in files

    • STDIN 

       

       < (less than) is used for redirecting input to file.


      if you want to do some operation on the file. like counting number of words, lines.. etc in a file.

       

      stdin in linux


      Note the difference, when you are using redirection, your file name is not shown because during redirection file is sent anonymously.

       

you can also easily combine STDIN and STDOUT. e.g. you want to process a file and want to save the output into a new file then this combination is useful.

stdin and stdout

Note: you can also use 0> for STDIN and >1 for STDOUT.

  • STDERR

This stream has value 2 and it will be used as 2>.

 

ls video.mp4 2> error.txt

If video.mp4 is not present in your file directory then you will get an error- 

ls: cannot access ‘video.mp4’: No such file or directory

 

and this error will be saved to a new file error.txt.

 

If you want to append the errors to an existing file then you must use 2>>.

 

if you want to save output and error both then you can do this like

 

ls -l video.mp4 file.txt > myoutput  2>&1

video.mp4 is not in your system and file.txt is inside your home directory. so whenever you will run above code. it will make a new file myoutput and save the error as well as output in file named myoutput.

LEAVE A REPLY

Please enter your comment!
Please enter your name here