Section 10.3. GNU and Unix Commands


10.3. GNU and Unix Commands

10.3.1. Objective 1: Work Effectively on the Command Line

10.3.1.1. The interactive shell and shell variables
  • 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.

10.3.1.2. Entering commands
  • 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.

10.3.1.3. Command history, editing, and substitution
  • 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.

Table 10-2. Shell expansion, editing, and substitution examples

History type

Examples

Expansion

!!

 

!n

 

^string1^string2

Editing

Ctrl-P, previous line

 

Ctrl-K, kill to end of line

 

Ctrl-Y, paste (yank) text

Substitution

VAR=$(command)


10.3.1.4. Recursive execution
  • Many commands contain either a -r or -R option for recursive execution through a directory hierarchy.

  • The find command is inherently recursive, and is intended to descend through directories looking for files with certain attributes or executing commands.

10.3.2. Objective 2: Process Text Streams Using Filters

10.3.2.1. The Commands

The following programs modify or manipulate text from files and standard input:


cut [files]

Cut out selected columns or fields from one or more files.


expand files

Convert tabs to spaces in files.


fmt [files]

Format text in files to a specified width by filling lines and removing newline characters.


head [files]

Print the first few lines of files.


join file1 file2

Print a line for each pair of input lines, one each from file1 and file2, that have identical join fields.


nl [files]

Number the lines of files, which are concatenated in the output.


od [files]

Dump files in octal, hexadecimal, ASCII, and other formats.


paste files

Paste together corresponding lines of one or more files into vertical columns.


pr [file]

Convert a text file into a paginated, columnar version, with headers and page fills.


split [infile] [outfile]

Split infile into a specified number of line groups; the output will go into a succession of files of outfileaa, outfileab, and so on.


tac [file]

Print file to standard output in reverse line order.


tail [files]

Print the last few lines of one or more files.


tr [string1 [string2]]

Translate characters by mapping from string1 to the corresponding character in string2.


wc [files]

Print counts of characters, words, and lines for files.

10.3.2.2. The stream editor, sed

sed is a popular text-filtering program found on every Unix system; it has the following syntax:


sed command [files]


sed -e command1 [-e command2] [files]


sed -f script [files]

Execute sed commands, or those found in script, on standard input or files.

10.3.3. Objective 3: Perform Basic File Management

10.3.3.1. Concepts
  • 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.

10.3.3.2. File and directory management commands

The following commands are essential for the management of files and directories:


cp file1 file2


cp files directory

Copy file1 to file2, or copy files to directory.


mkdir directories

Create one or more directories.


mv source target

Move or rename files and directories.


rm files

Delete one or more files from the filesystem. When used recursively (with the -r option), rm also removes directories.


rmdir directories

Delete directories, which must be empty.


touch files

Change the access and/or modification times of files by default to the present time.

10.3.3.3. File-naming wildcards

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.

[characters]

Match any single character from among characters listed between brackets.

[!characters]

Match any single character other than characters listed between brackets.

[a-z]

Match any single character from among the range of characters listed between brackets.

[!a-z]

Match any single character from among the characters not in the range listed between brackets.

{frag1,frag2,frag3,...}

Brace expansion: create strings frag1, frag2, and frag3, etc., such that file_{one,two,three} yields file_one, file_two, and file_three.


10.3.4. Objective 4: Use Unix Streams, Pipes, and Redirects

10.3.4.1. Concepts
  • 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.

10.3.4.2. Standard I/O
  • 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.

10.3.4.3. Pipes and redirection
  • 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 file.

$ cmd > file

 

$ cmd 1> file

Send stderr to file.

$ cmd 2> file

Send both stdout and stderr to file.

$ cmd > file 2>&1

Receive stdin from file.

$ cmd < file

Append stdout to file.

$ cmd >> file

 

$ cmd 1>> file

Append stderr to file.

$ cmd 2>> file

Append both stdout and stderr to file.

$ cmd >> file 2>&1

Pipe stdout from cmd1 to cmd2.

$ cmd1 | cmd2

Pipe stdout and stderr from cmd1 to cmd2.

$ cmd1 2>&1 | cmd2

Pipe stdout from cmd1 to cmd2 while simultaneously writing it to file1 using tee.

$ cmd1 | tee file1 | cmd2


10.3.5. Objective 5: Create, Monitor, and Kill Processes

10.3.5.1. Concepts
  • Processes have:

    • A lifetime.

    • A PID.

    • A UID.

    • A GID.

    • A parent process.

    • An environment.

    • A current working directory.

10.3.5.2. Monitoring commands

ps

Generate a one-time snapshot of the current processes on standard output.


pstree

Display a hierarchical list of processes in a tree format.


top

Generate a continuous, formatted, real-time process activity display on a terminal or in a terminal window.

10.3.5.3. Signaling processes
  • 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.

Table 10-5. Common signals

Signal

Number

Meaning

HUP

1

Hangup, reread configuration.

INT

2

Interrupt, stop running.

KILL

9

Exit immediately.

TERM

15

Terminate nicely.

TSTP

18

Stop executing.


10.3.5.4. Shell job control

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:


bg [jobspec]

Place jobspec in the background as if it had been started with &.


fg [jobspec]

Place jobspec in the foreground, making it the current job.


jobs [jobspecs]

List jobspecs on standard output.

10.3.6. Objective 6: Modify Process Execution Priorities

10.3.6.1. Concepts
  • A process's execution priority is managed by the kernel.

  • You can bias the execution priority by specifying a nice number in the range of -20 to +19 (default is 0).

  • Positive nice numbers reduce priority; negative nice numbers increase priority and are reserved for the superuser.

10.3.6.2. Commands

nice -adjustment [command]

Apply nice number adjustment to the process created to run command.


renice [+|-]nicenumber targets

Alter the nicenumber, and thus the scheduling priority, of one or more running target processes.

10.3.7. Objective 7: Search Text Files Using Regular Expressions

10.3.7.1. Concepts
  • 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.

10.3.7.2. Position anchors

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 < and >.


10.3.7.3. Character sets

The operators in Table 10-7 match text.

Table 10-7. Regular expression character sets

Regular expression

Description

[abc]

[a-z]

Match any single character from among listed characters (abc) or from among the characters comprising a range (a-z).

[^abc]

[^a-z]

Match any single character not among listed characters or ranges.

.

Match any single character except a newline.


10.3.7.4. Modifiers

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 regex.

\+

+

Match one or more instances of the preceding regex.

\{n,m\}

{n,m}

Match a range of occurrences of the single character or regex that precedes this construct. \{n\} matches n occurrences, \{n,\} matches at least n occurrences, and \{n,m\} matches any number of occurrences between n and m, inclusively.

\|

|

Match the character or expression to the left or right of the vertical bar.

\(regex\)

(regex)

Matches regex, but it can be modified as a whole and used in back-references. (\1 expands to the contents of the first \(\) and so on up to \9.)


10.3.8. Objective 8: Using vi

10.3.8.1. Subcommands
  • Start vi with vi file1 [file2 [...]]. See Table 10-9.

Table 10-9. Basic vi editing commands

Command

Description

Esc

Exit insert mode and put the editor back into command mode.

h

Move left one character.

j

Move down one line.

k

Move up one line.

l

Move right one character.

H

Move to the top of the screen.

L

Move to the bottom of the screen.

G

Move to the end of the file.

w

Move forward one word.

b

Move backward one word.

0 (zero)

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.

i

Insert at the current cursor position.

I

Insert at the beginning of the current line.

a

Append after the current cursor position.

A

Append to the end of the current line.

o

Start a new line after the current line.

O

Start a new line before the current line.

r

Replace the character at the current cursor position.

R

Start replacing (overwriting) at the current cursor position.

x

Delete the character at the current cursor position.

X

Delete the character immediately before (to the left) of the current cursor position.

s

Delete the character at the current cursor position and go into insert mode. (This is the equivalent of the combination xi.)

S

Delete the contents of the current line and go into insert mode.

dX

Given a movement command X, cut (delete) the appropriate number of characters, words, or lines from the current cursor position.

dd

Cut the entire current line.

D

Cut from the current cursor position to the end of the line. (This is equivalent to d$.)

cX

Given a movement command X, cut the appropriate number of characters, words, or lines from the current cursor position and go into insert mode.

cc

Cut the entire current line and go into insert mode.

C

Cut from the current cursor position to the end of the line and enter insert mode. (This is equivalent to c$.)

yX

Given a movement command X, copy (yank[a]) the appropriate number of characters, words, or lines from the current cursor position.

yy or Y

Copy the entire current line.

p

Paste after the current cursor position.

P

Paste before the current cursor position.

.

Repeat the last command.

u

Undo the last command.[b]

/regex

Search forward for regex.

?regex

Search backward for regex.

n

Find the next match.

N

Find the previous match. (In other words, repeat the last search in the opposite direction.)

:n

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 :n!.

:e file

Load file in place of the current file. Force this action with :e! file.

:r file

Insert the contents of file after the current cursor position.

:q

Quit without saving changes. Force this action with :q!.

:w file

Write the current buffer to file. To append to an existing file, use :w >>file. Force the write (when possible, such as when running as root) with :w! file.

:wq

Write the file contents and quit. Force this action with :wq!.

:x

Write the file contents (if changed) and quit (the ex equivalent of ZZ).

ZZ

Write the file contents (if changed) and quit.

:! command

Execute command in a subshell.

[a]

[b]


[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 u perform another level of undo. Nvi uses . after u to do multi-level undo, and, like traditional vi, uses a repeated u to redo (undo the undo, so to speak). This can cause some confusion when moving between Linux distributions that have different default implementations of vi.



LPI Linux Certification in a Nutshell
LPI Linux Certification in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596005288
EAN: 2147483647
Year: 2004
Pages: 257

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net