Home
XHTML
CSS
PHP
MySQL
SEO
JavaScript
Computer Basics
Number Systems
LINUX


LINUX Expressions

LINUX | Files | Directories | Programs | Expressions | Utilities | Shell Scripts

Expressions are condition statements for searching patterns in text. Conditions are set using metacharacters. Metacharacters are character symbols declared as operators. Expressions are used in many programming languages.


The  *  asterisk is a wildcard that substitutes one or more character spaces...

$ ls fi*

The ls command outputs the contents of the PWD. All files that start with fi output to the screen.




The  ?  question mark is a wildcard that substitutes an individual character...

$ ls fi???

All files that start with fi and five characters long output to the screen.




The  \  backslash turns the metacharacter feature off, escaping the character that follows it...

$ ls file\*

The  *  outputs a text character instead of a metacharacter.




The double quotes connect multiple text characters together as a sentence or paragraph, including spaces...

$ ls "These are text characters"

The single quotes are similar to double quotes, but do not allow metacharacter substitution.




The  $  dollar sign precedes a shell variable name when called. A variable is substituted when declared within double quotes...

$ ls "This is a $variable"





The  [ ]  square brackets enclose characters that can be matched...

$ ls fi[34]

All files that begin with fi and have a 3 or 4 at the end are diplayed.



The  -  hyphen connects a range of characters...

$ ls fi[3-6]

All files that begin with fi and has a number between 3 and 6 at the end output to the screen.



Other conditions...

[^file] matches any single character
[Ff]ile  matches lines containing File or file
[0-3]  is the same as [0123]
[a-k]  is the same as [abcdefghijk]
[A-C]  is the same as [ABC]
[A-Ca-k]  is the same as [ABCabcdefghijk]

[[:alpha:]]  is the same as [a-zA-Z]
[[:upper:]]  is the same as [A-Z]
[[:lower:]]  is the same as [a-z]
[[:digit:]]  is the same as [0-9]
[[:alnum:]]  is the same as [0-9a-zA-Z]
[[:space:]]  matches any white space including tabs


The  #  pound declares any following text on the same line as a comment.

The  ;  semicolon separates multiple commands.

The  &  ampersand placed at the end of a command causes the command to execute as a background process. After the command is entered, a new shell prompt appears while the background process is running.




A regular expression is a command line syntax rule used to search string patterns. There are three commands for searching patterns in a text file...

grep  -  (global regular expression) searches a pattern using regular expressions
egrep  -  (extended) searches a pattern using regular expressions and pipe alternations
fgrep  -  (fixed) searches for fixed strings without regular expressions


The grep command searches all occurrences of a string in a file in the PWD. The file is added as an argument...

$ grep 'text' fileName1

This searches for all occurrences of text in all files in the PWD...

$ grep 'text' *

This searches for all occurrences of text in fileName1 in the PWD, but also requires that the letter t is at the start of each line...

$ grep '^text' fileName1

The options don't differ between grep, egrep, and fgrep, but there are more options available in LINUX than UNIX...

Options...

-b  outputs the block number at the beginning of each line
-c  outputs the number of matched lines
-h  outputs the matched lines, but do not output the filenames
-i  ignores case sensitivity
-l  outputs the filenames, but do not output the matched lines
-n  outputs the matched lines and their line numbers
-s  silent mode
-v  outputs all lines that do NOT match
-w  matches whole word




Multiple commands can be connected in a command statement using the  |  pipe operator, known as piping. The output of a command can be piped into the input of another command, instead of to the screen. The pipe is similar to using the  >  output redirect operator, but the pipe redirects the output of a command to another command, instead of to a file. The following example counts the words of a file, then sends the result in an email...

$ cat filename1 | wc | mail -s "Subject" user@website.com

The following example outputs lines 30 through 60 of a text file. The head command calls up the first 60 lines of filename1, then redirects the output to the tail command, which outputs the last 30 of the 60 lines...

$ cat filename1 | head -60 | tail -30

The following example searches all instances of text in filename1 using the  <  input redirect operator, then sends the output to the default printer...

$ grep "text" < filename1 | lpr



SEO Vancouver, Washington
SEO Portland, Oregon
Website Design Vancouver, Washington