A shell provides the command prompt and interprets commands.
A shell variable holds a value that is accessible to shell programs.
PATH
is a shell variable that contains a listing of directories that hold executable programs.
Commands must be bash built-ins, found in the PATH
, or explicitly defined in order to succeed.
When shell variables are exported, they become part of the environment.
Commands are comprised of a valid command, with or without one or more options and arguments, followed by a carriage return.
Interactive commands can include looping structures more often used in shell scripts.
Shell sessions can be viewed as a conversation. History, expansion, and editing make that dialog more productive.
Commands can be reissued, modified, and edited. Examples are shown in Table 10-2.
Command substitution allows the result of a command to be placed into a shell variable.
The following programs modify or manipulate text from files and standard input:
files
]Cut out selected columns or fields from one or more files
.
files
Convert tabs to spaces in files
.
files
]Format text in files
to a specified width by filling lines and removing newline characters.
files
]Print the first few lines of files
.
file1 file2
Print a line for each pair of input lines, one each from file1
and file2
, that have identical join fields.
files
]Number the lines of files
, which are concatenated in the output.
files
]Dump files
in octal, hexadecimal, ASCII, and other formats.
files
Paste together corresponding lines of one or more files into vertical columns.
file
]Convert a text file into a paginated, columnar version, with headers and page fills.
infile
] [outfile
]Split infile
into a specified number of line groups; the output will go into a succession of files of outfilea
a
, outfilea
b
, and so on.
file
]Print file
to standard output in reverse line order.
files
]Print the last few lines of one or more files.
string1
[string2
]]Translate characters by mapping from string1
to the corresponding character in string2
.
files
]Print counts of characters, words, and lines for files
.
Filesystem creation prepares a disk device (or partition) for use. Linux usually uses the native ext2 (second extended) filesystem or the journalled ext3 filesystem, but it supports many other filesystem types.
The Linux filesystem is arranged into a hierarchical structure anchored at the root directory, or /. Beneath this is a tree of directories and files.
Identification information for a filesystem object is stored in its inode, which holds location, modification, and security information. Filesystems are created with a finite number of inodes.
The following commands are essential for the management of files and directories:
file1 file2
files directory
Copy file1
to file2
, or copy files
to directory
.
directories
Create one or more directories
.
source target
Move or rename files and directories.
files
Delete one or more files
from the filesystem. When used recursively (with the -r option), rm also removes directories.
directories
Delete directories
, which must be empty.
files
Change the access and/or modification times of files
by default to the present time.
Wildcards (also called file globs) allow the specification of many files at once. A list of commonly used wildcards can be found in Table 10-3.
Table 10-3. File-naming wildcards
Wildcard |
Function |
---|---|
|
Match zero or more characters. |
|
Match exactly one character. |
|
Match any single character from among |
|
Match any single character other than |
|
Match any single character from among the range of characters listed between brackets. |
|
Match any single character from among the characters not in the range listed between brackets. |
|
Brace expansion: create strings |
A central concept for Linux and Unix systems is that everything is a file.
Many system devices are represented in the filesystem using a device file, such as /dev/ttyS0 for a serial port.
The shell provides the standard I/O capability, offering three default file descriptors to running programs.
Standard input (stdin) is a text input stream, by default attached to the keyboard.
Standard output (stdout) is an output stream for normal program output.
Standard error (stderr) is an additional output stream meant for error messages.
It is possible to tie the output of one program to the input of another. This is known as a pipe and is created by joining commands using the pipe symbol (|
).
Pipes are a special form of redirection, which allows you to manage the origin of input streams and the destination of output streams. Redirection syntax for various shells differs slightly. See Table 10-4 for examples of common redirection operators.
Table 10-4. Common redirection operators
Redirection function |
Syntax for bash |
---|---|
Send stdout to |
$ cmd > file |
$ cmd 1> file | |
Send stderr to |
|
Send both stdout and stderr to |
|
Receive stdin from |
|
Append stdout to |
$ cmd >> file |
$ cmd 1>> file | |
Append stderr to |
|
Append both stdout and stderr to |
|
Pipe stdout from |
|
Pipe stdout and stderr from |
|
Pipe stdout from |
|
Processes have:
A lifetime.
A PID.
A UID.
A GID.
A parent process.
An environment.
A current working directory.
Generate a one-time snapshot of the current processes on standard output.
Display a hierarchical list of processes in a tree format.
Generate a continuous, formatted, real-time process activity display on a terminal or in a terminal window.
Processes listen for signals sent by the kernel or users using the kill command:
kill
-sigspec
[pids
]
Send sigspec
to pids
.
Common kill signals are listed in Table 10-5.
Shells can run processes in the background, where they execute on their own, or in the foreground, attached to a terminal. Each process handled in this way is known as a job. Jobs are manipulated using job control commands:
jobspec
]Place jobspec
in the background as if it had been started with &
.
jobspec
]Place jobspec
in the foreground, making it the current job.
jobspecs
]List jobspecs
on standard output.
Regular expressions are used to match text. The term is used to describe the loosely defined text-matching language as well as the patterns themselves. A regular expression is often called a regex or a regexp.
Regular expressions are made up of metacharacters (with special meaning) and literals (everything that is not a metacharacter).
The backslash character () turns off (escapes) the special meaning of the character that follows, turning metacharacters into literals. For non-metacharacters, it often turns on some special meaning.
The operators in Table 10-6 match line position.
Table 10-6. Regular expression position anchors
Regular expression |
Description |
---|---|
|
Match the beginning of a line. |
|
Match the end of a line. |
|
Match word boundaries. Word boundaries are defined as whitespace, the start of line, the end of line, or punctuation marks. The backslashes are required and enable this interpretation of |
The operators in Table 10-7 match text.
Table 10-7. Regular expression character sets
Regular expression |
Description |
---|---|
|
Match any single character from among listed characters (abc) or from among the characters comprising a range (a-z). |
|
Match any single character not among listed characters or ranges. |
|
Match any single character except a newline. |
The operators in Table 10-8 modify the way other operators are interpreted.
Table 10-8. Regular expression modifiers
Basic regular expression |
Extended regular expression |
Description |
---|---|---|
|
|
Match zero or more of the character that precedes it. |
|
|
Match zero or one instance of the preceding |
|
|
Match one or more instances of the preceding |
|
|
Match a range of occurrences of the single character or regex that precedes this construct. |
|
|
Match the character or expression to the left or right of the vertical bar. |
|
|
Matches |
Start vi with vi
file1
[file2
[...]]. See Table 10-9.
Table 10-9. Basic vi editing commands
Command |
Description |
---|---|
[a] | |
[b] | |
|
Exit insert mode and put the editor back into command mode. |
|
Move left one character. |
|
Move down one line. |
|
Move up one line. |
|
Move right one character. |
|
Move to the top of the screen. |
|
Move to the bottom of the screen. |
|
Move to the end of the file. |
|
Move forward one word. |
|
Move backward one word. |
|
Move to the beginning of the current line. |
|
Move to the first nonwhitespace character on the current line. |
|
Move to the end of the current line. |
Ctrl-B |
Move up (back) one screen. |
Ctrl-F |
Move down (forward) one screen. |
|
Insert at the current cursor position. |
|
Insert at the beginning of the current line. |
|
Append after the current cursor position. |
|
Append to the end of the current line. |
|
Start a new line after the current line. |
|
Start a new line before the current line. |
|
Replace the character at the current cursor position. |
|
Start replacing (overwriting) at the current cursor position. |
|
Delete the character at the current cursor position. |
|
Delete the character immediately before (to the left) of the current cursor position. |
|
Delete the character at the current cursor position and go into insert mode. (This is the equivalent of the combination |
|
Delete the contents of the current line and go into insert mode. |
|
Given a movement command |
|
Cut the entire current line. |
|
Cut from the current cursor position to the end of the line. (This is equivalent to |
|
Given a movement command |
|
Cut the entire current line and go into insert mode. |
|
Cut from the current cursor position to the end of the line and enter insert mode. (This is equivalent to |
|
Given a movement command |
|
Copy the entire current line. |
|
Paste after the current cursor position. |
|
Paste before the current cursor position. |
|
Repeat the last command. |
|
Undo the last command.[b] |
|
Search forward for |
|
Search backward for |
|
Find the next match. |
|
Find the previous match. (In other words, repeat the last search in the opposite direction.) |
|
Next file; when multiple files are specified for editing, this command loads the next file. Force this action (if the current file has unsaved changes) with |
|
Load |
|
Insert the contents of |
|
Quit without saving changes. Force this action with |
|
Write the current buffer to |
|
Write the file contents and quit. Force this action with |
|
Write the file contents (if changed) and quit (the ex equivalent of |
|
Write the file contents (if changed) and quit. |
|
Execute |
[a] Emacs users should be careful not to confuse the vi definition of yank (copy) with that of Emacs (paste). [b] Many of the popular vi implementations support multi-level undo. Vim breaks compatibility with traditional vi by making a repeated |