pipes and tees
As I concern as these commands are most powerful Linux commands. You might heard about pipes and tees used for plumbing works. The tees and pipes used for diverge water into different direction. The pipe and tee commands will do the same job for you. The pipes and tees are used for take the output one command and apply it on another command.
A pipe allows output from one command to be used as the input for another command. The pipe symbol is vertical line | . For example:
will create a new text file named network_intetfaces on your Desktop also it make a catalog on your terminal window. You can also try this command on other directories.pipe
Pipe |
- To show first ten entries of ls command it can be pipe through head command like ls | head.
cat /etc/network/interfaces | network_interfaces.txt
tees
Tee |
Plumbers are using tees to diverge water flow in different directions. In Linux we are using tees to make the output of one command as the input of different command.
For example if you want two copies of network interfaces file you can use this command
cat /etc/network/interfaces |tee file.txt file1.txt
to create to text files containing two text files named file and file1. Also this make a catalog on your terminal window. If you are using Desktop as working directory you can see two text files(file.txt and file1.txt) poping out from Desktop immediately.
Using wildcards
A wildcard is a character that can be used as a substitute for any of a class of characters in a search, thereby greatly increasing the flexibility and efficiency of searches.
Wildcards are commonly used in Linux and other Unix operating systems. The terminal is a text only user interface and whose main function is to execute commands typed in by users and display their results.
Wildcards are also used in regular expressions and programming languages. Regular expressions are a pattern matching system that uses strings (i.e., sequences of characters) constructed according to pre-defined syntax rules to find desired strings in text.
The term wildcard or wild card was originally used in card games to describe a card that can be assigned any value that its holder desires. However, its usage has spread so that it is now used to describe an unknown or unpredictable factor in a variety of fields.
Wildcards are commonly used in Linux and other Unix operating systems. The terminal is a text only user interface and whose main function is to execute commands typed in by users and display their results.
Wildcards are also used in regular expressions and programming languages. Regular expressions are a pattern matching system that uses strings (i.e., sequences of characters) constructed according to pre-defined syntax rules to find desired strings in text.
The term wildcard or wild card was originally used in card games to describe a card that can be assigned any value that its holder desires. However, its usage has spread so that it is now used to describe an unknown or unpredictable factor in a variety of fields.
Star wildcard
Three types of wildcards are used with Linux commands. The most frequently employed and usually the most useful is the star wildcard, which is the same as an asterisk (*). The star wildcard has the broadest meaning of any of the wildcards, as it can represent zero characters, all single characters or any string.
As I mentioned earlier a wild card can represent any string, we can use wildcards even as the extension of file. For example we can use Raspberry.* to represent any type of file that has name Raspberry. If your directory is containing following files Raspberry.jpg, Raspberry.php, Raspberry.py, Raspberry.py, Raspberry.html etc. You can represent all these files only by using Raspberry.* . It is very useful to manipulate the file with same name.
Also it useful to manipulate files with similar names for example, text1.txt, text2.txt, text3.txt.... text100.txt. All these 100 text file can be manipulated only using text*.txt.
Question Mark Wildcard
The question mark (?) is used as a wildcard character in shell commands to represent exactly one character, which can be any single character. Thus, two question marks in succession would represent any two characters in succession, and three question marks in succession would represent any string consisting of three characters.
from the above example will list the 100 text files. Also from the above example ls Raspberry.??? will lists the all kind of files thats extension has only 3 letters. From above example it lists Raspberry.jpg and Raspberry.png only.
The question mark wildcard can also be used in combination with other wildcards when separated by some other character. For example, the following would return a list of all files in the current directory that have a three-character filename extension:
ls *.???
Thus, for example, the following would return data on all objects in the current directory whose names, inclusive of any extensions, are exactly three characters in length:
ls text?.txtfrom the above example will list the 100 text files. Also from the above example ls Raspberry.??? will lists the all kind of files thats extension has only 3 letters. From above example it lists Raspberry.jpg and Raspberry.png only.
The question mark wildcard can also be used in combination with other wildcards when separated by some other character. For example, the following would return a list of all files in the current directory that have a three-character filename extension:
Square Brackets Wildcard
The square bracket [ is interpreted by the shell as a sign to generate filenames, matching any of the characters between [ and the first subsequent ]. The order in this list between the brackets is not important. Each pair of brackets is replaced by exactly one character.
Thus, for example, the following would provide information about all objects in the current directory that have an j, p and/or t in them:
Raspberry*[jpt]*
And the following would list all files that had an extension that begins with j, p or t:
ls *.[jpt]*
The same results can be achieved by merely using the star and question mark wildcards. However, it is clearly more efficient to use the bracket wildcard.
The bash shell will also understand ranges of characters between brackets. When a hyphen is used between two characters in the square brackets wildcard, it indicates a range inclusive of those two characters. For example, the following would provide information about all of the objects in the current directory that begin with any letter from a through f and trying to list following file and observe what is going to happen:
ls Raspberry[a-f]*.*
Try above example with [a-z] and observe result. And the following would provide information about every object in the current directory whose name includes at least one numeral:
ls text[0-9].*
Lists all the 9 files text1.txt to text9.txt. Try the above command with:
ls text[0-100].*
ls *.[jpt]*
The same results can be achieved by merely using the star and question mark wildcards. However, it is clearly more efficient to use the bracket wildcard.
The bash shell will also understand ranges of characters between brackets. When a hyphen is used between two characters in the square brackets wildcard, it indicates a range inclusive of those two characters. For example, the following would provide information about all of the objects in the current directory that begin with any letter from a through f and trying to list following file and observe what is going to happen:
ls Raspberry[a-f]*.*
Try above example with [a-z] and observe result. And the following would provide information about every object in the current directory whose name includes at least one numeral:
ls text[0-9].*
Lists all the 9 files text1.txt to text9.txt. Try the above command with:
ls text[0-100].*
Discussion
How can I store result of a command?
The result of a command can be stored using as explained above using pipes and tees. There is another way to store result of a command as follows:
cat /etc/network/interfaces > network.txt
The > symbol is known as stdout of I/O redirection. You can see that if you redirect output to network.txt again, it will overwrite your network.txt file. So use >> instead > to append output of another command to network.txt.
Comments
Post a Comment