Linux

  1. Home
  2. Computing & Technology
  3. Linux

From Authors, for About.com

cut/copied

Saving and quitting (from the command mode):

:w write (=save)

:w filename write the contents to the file "filename"

:x save and exit

:q quit (it won't let you if changes not saved)

:q! quit discarding changes (you will not be prompted if changes not saved)


nano

This is a brand new (March 2001) GNU replacement for pico. Works and looks like pico, but it is smaller, better, and licenced as expected for a decent piece of Linux software (i.e., General Public Licence, GPL).


khexedit

(in X terminal) Simple hexadecimal editor. Another hexadecimal editor is hexedit (text based, less user friendly). Hex editors are used for editing binary (non-ASCII) files.


diff file1 file2 > patchfile

Compare contents of two files and list any differences. Save the output to the file patchfile.


sdiff file1 file2

Side-by-side comparison of two text files. Output goes to the "standard output" which normally is the screen.


patch file_to_patch patchfile

Apply the patch (a file produced by diff, which lists differences between two files) called patchfile to the file file_to_patch. If the patch was created using the previous command, I would use: patch file1 patchfile to change file1 to file2.


grep filter

Search content of text files for matching patterns. It is definitely worth learning at least the basics of this command.

A simple example. The command:

cat * | grep my_word | more

will search all the files in the current working directory (except files starting with a dot) and print the lines which contain the string "my_word".

A shorter form to achieve the same may be:

grep my_word * |more

The patterns are specified using a powerful and standard notation called "regular expressions".

There is also a "recursive" version of grep called rgrep. This will search all the files in the current directory and all its subdirectories for my_word and print the names of the files and the matching line:

rgrep -r my_word . | more

Regular expressions (regexpr)

Regular experessions are used for "pattern" matching in search, replace, etc. They are often used with utilities (e.g., grep,sed) and programming languages (e.g., perl). The shell command dir, uses a slightly modifed flavour of regular expressions (the two main differences are noted below). This brief writeup includes almost all the features of standard regular expression--regexpressions are not as complicated as they might seem at first. Definitely worth a closer look at.

In regular expressions, most characters just match themselves. So to search for string "peter", I would just use a searchstring "peter". The exceptions are so-called "special characters" ("metacharacters"), which have special meaning.

The regexpr special characters are: "\" (backslash), "." (dot), "*" (asterisk), "[" (bracket), "^" (caret, special only at the beginnig of a string), "$" (dollar sign, special only at the end of a string). A character terminating a pattern string is also special for this string.

The backslash, "\" is used as an "escape" character, i.e., to quote a subsequent special character.

Thus, "\\" searches for a backslash, "\." searches for a dot, "\*" searches for the asterisk, "\[" searches for the bracket, "\^" searches for the caret even at the begining of the string, "\$" searches for the dollar sign even at the end of the string.

Backslash followed by a regular (non-special) character may gain a special meaning. Thus, the symbols \< and \> match an empty string at the beginning and the end of a word, respectively. The symbol \b matches the empty string at the edge of a word, and \B matches the empty string provided it's not at the edge of a word.

The dot, ".", matches any single character. [The dir command uses "?" in this place.] Thus, "m.a" matches "mpa" and "mea" but not "ma" or "mppa".

Any string is matched by ".*" (dot

Explore Linux

About.com Special Features

Linux

  1. Home
  2. Computing & Technology
  3. Linux

©2009 About.com, a part of The New York Times Company.

All rights reserved.