Additional Commands and Functionality

Transcript
Wildcards

"Wildcards" in file and/or directory names allow display of selected file or directory names rather than listing all of them. * and ? are the most commonly used ones. "*" represents 0 or more characters so

[hes@vm18-165 ~]$ ls first*
will display the names of all files and directories starting with the letters "first" with 0 or more characters following in the name.

"?" represents exactly one character. So

[hes@vm18-165 ~]$ ls first-c?t
would display the file first-cat but not the file first-file. It would also display first-cot, if there was such a file.
Searching for Patterns
Transcript

The grep command format is

grep pattern file(s)

The pattern is a regular expression. The simplest one is just a character string, and grep looks for a match in the file(s). It doesn't have to be a complete word, just a string of characters. grep prints the lines of file(s) matching a pattern (a regex).

[hes@vm18-165 ~]$ grep line first-file
A regular expression example using a wildcard is sec.ond where the "." means that any one character is considered a match for that position. The "*" is commonly used to search many files, e.g. *html will include all the html files in the directory. (The notation differs between wildcards and regexes.)

[hes@vm16-203 ~]$ grep se.ond first-file
will look for 6 characters appearing anywhere in the file, where the first 2 are specified as are the last 3, but the 3rd can be any character. E.g. "se9ond" or "se+ond" appearing in the file would also match.

Regexes can be, and usually are, much more complicated than just using wildcards. Learning to formulate all the types of regular expressions is a more advanced topic, which can be very useful, and whole books have been written on this topic. E.g. Mastering Regular Expressions by Jeffrey Friedl. A much shorter treatment and more.


Piping Command Output to a Command Input
Transcript

The symbol for pipe is |
A very simple task illustrating its use is to search two files for a pattern. (It doesn't have to be in both files, and when it isn't, nothing is output.) We could search them individually and "print" the output of each command on the screen:

[hes@vm18-165 ~]$ grep se.ond first-cat [hes@vm18-165 ~]$ grep se.ond first-file
or put them together and do one search:
[hes@vm18-165 ~]$ cat first-cat first-file > tmp-file [hes@vm18-165 ~]$ grep se.ond tmp-file [hes@vm18-165 ~]$ rm tmp-file
where the last command removes the no longer needed temporary file, and both approaches work, but using the | command makes it more straight forward.

[hes@vm18-165 ~]$ cat first-cat first-file | grep se.ond
will more directly arrive at the needed result. This is a very simple example of using the pipe command to redirect the output of one command directly into the input of another command.
Return to Glide main page
Copyright 2019, 2020 by Henry E. Schaffer. Comments and suggestions are welcome, and should go to hes@ncsu.edu
Licensed under the Creative Commons Attribution-NonCommercial 4.0 International Public License https://creativecommons.org/licenses/by-nc/4.0/legalcode
Last updated 1/9/2020