LPI Linux Certification in a Nutshell (15 page)

Read LPI Linux Certification in a Nutshell Online

Authors: Adam Haeder; Stephen Addison Schneiter; Bruno Gomes Pessanha; James Stanger

Tags: #Reference:Computers

BOOK: LPI Linux Certification in a Nutshell
4.32Mb size Format: txt, pdf, ePub
Name

tr

Syntax
tr [
options
] [
string1
[
string2
]]
Description

Translate characters from
string1
to the corresponding characters in
string2
.
tr
does
not
have file arguments and therefore must use
standard input and output.

Note that
string1
and
string2
should contain the same number of
characters since the first character in
string1
will be replaced with the first
character in
string2
and so on.

Either
string1
or
string2
can contain several types of
special characters. Some examples follow, although a full list can be
found in the
tr
manpage.

a
-
z

All characters from
a
to
z
.

\\

A backslash (
\
)
character.

\
nnn

The ASCII character with the octal value
nnn
.

\
x

Various
control characters:

\a      bell
\b backspace
\f form feed
\n newline
\r carriage return
\t horizontal tab
\v vertical tab
Frequently used options
-c

Use the complement of (or all characters
not
in)
string1
.

-d

Delete characters in
string1
from the output.

-s

Squeeze out repeated output characters in
string1
.

Example 1

To
change all
lowercase characters in
file1
to
uppercase, use:

$
cat file1 | tr a-z A-Z

or:

$
cat file1 | tr '[:lower:]' '[:upper:]'
Example 2

To suppress repeated
whitespace characters from
file1
:

$
cat file1 | tr -s '[:blank:]'
Example 3

To remove all
non-printable characters from
file1
(except the newline character):

$
cat file1 | tr -dc '[:print:]\n'
Name

unexpand

Syntax
unexpand [
options
] [
files
Description

Convert
spaces to
Tabs. This command performs the opposite action of
expand
. By default, Tab stops are assumed to be
every eight spaces.

Frequently used options
-a

Convert all spaces, not just leading spaces. Normally
unexpand
will work only on spaces at the
beginning of each line of input. Using the
-a
option causes it to replace spaces
anywhere in the input.

Note

This behavior of
unexpand
differs
from
expand
. By default,
expand
converts all Tabs to spaces. It
requires the
-i
option to convert only
leading spaces.

-t
number

Specify Tab stops in place of the default 8.

Name

uniq

Syntax
uniq [
options
] [
input
[
output
]]
Description

Writes
input
(or
stdin
) to
output
(or
stdout
), eliminating adjacent duplicate
lines.

Since
uniq
works only on adjacent lines of
its input, it is most often used in conjunction with
sort
.

Frequently used options
-d

Print only nonunique (repeating) lines.

-u

Print only unique (nonrepeating) lines.

Examples

Suppose
file
contains the following:

b
b
a
a
c
d
c

Issuing the command
uniq
with
no options:

$
uniq file

yields the following output:

b
a
c
d
c

Notice that the line with
c
is repeated, since the duplicate lines were not adjacent in the input
file. To eliminate duplicate lines regardless of where they appear in
the input, use
sort
on the input
first:

$
sort file | uniq
a
b
c
d

To print only lines that never repeat in the input, use the
-u
option:

$
sort file | uniq -u
d

To print only lines that
do
repeat in the
input, use the
-d
option:

$
sort file | uniq -d
a
b
c
Name

wc

Syntax
wc [
options
] [
files
]
Description

Print counts of characters, words, and lines for
files
. When multiple files are listed,
statistics for each file output on a separate line with a cumulative
total output last.

Frequently used options
-c

Print the character count only.

-l

Print the line count only.

-w

Print the word count only.

Example 1

Show all counts and totals for
file1
,
file2
, and
file3
:

$
wc file[123]
Example 2

Count the number of lines in
file1
:

$
wc -l file1
Objective 3: Perform Basic File Management

This section covers basic file and directory management,
including filesystems, files and directories, standard file management
commands, their recursive capabilities (where applicable), and wildcard
patterns (also known as file globbing).

Filesystem Objects

Nearly every operating system in history structures its
collection of stored objects in a
hierarchy
, which is a tree of objects
containing other objects. This hierarchy allows a sane organization of
objects and allows identically named objects to appear in multiple
locations, an essential feature for multiuser systems such as Linux.
Information about each object in the filesystem is stored in a table
(which itself is part of the filesystem), and each object is numbered
uniquely within that table. Although there are a few special
object types on Linux systems, the two most common are
directories
and
files
.

Directories and files

A directory is a container intended to hold objects such as
files and other directories. A directory’s purpose is primarily for
organization. A file, on the other hand, exists within the directory,
and its purpose is to store raw data. At the top of all Linux
filesystem hierarchies is a directory depicted simply by
/
; this is known as the
root
directory. Beneath
/
are named directories and
files in an organized and well-defined tree. To describe these
objects, you simply refer to them by name separated by the
/
character. For example, the object
ls
is an executable program stored in a directory
called
/bin
under the root directory; it is
depicted simply as
/bin/ls
.

Note

Don’t confuse
root
directory with the
username
root
, which is separate
and distinct. There’s also often a directory named
/root
for the
root user. Keeping
/
,
/root
, and the
root
user
straight in a conversation can be a challenge.

Inodes

The identification information for a filesystem object
is known as its
inode
. Inodes carry information
about objects, such as where they are located on disk, their
modification time, security settings, and so forth. Each Linux
ext3
filesystem is created with a finite number
of inodes that is calculated based on the size of the filesystem and
other options that are given to
mke2fs
(the
command used to create an ext2 or ext3 filesystem on a partition).
Multiple objects in the filesystem can share the same inode; this
concept is called
linking
.

File and directory management commands

Once a hierarchy is defined, there is a never-ending need to
manage the objects in the filesystem. Objects are constantly created,
read, modified, copied, moved, and deleted, so wisely managing the
filesystem is one of the most important tasks of a system
administrator. In this section, we discuss the basic command-line
utilities used for file and directory management. There are
GUI tools for this task, but the LPI Level 1 exams only
test on
command-line tools, and although GUI tools are sometimes
more intuitive, a good system administrator should always be always be
able to administer his or her system from the command line.

File-Naming Wildcards (File Globbing)

When working with files on the command line, you’ll often
run into situations in which you need to perform operations on many
files at once. For example, if you are developing a C program, you may
want to
touch
all of your
.c
files in order to be sure to recompile them the next time you issue the
make
utility to build your program. There will also
be times when you need to move or delete all the files in a directory or
at least a selected group of files. At other times, filenames may be
long or difficult to type, and you’ll want to find an abbreviated
alternative to typing the filenames for each command you issue (see
Table 6-5
).

To make these operations simpler, all shells on Linux offer
file-naming wildcards
.

Note

Wildcards are expanded by the shell, not by commands. When a
command is entered with wildcards included, the shell first expands
all the wildcards (and other types of expansion) and passes the full
result on to the command. This process is invisible to you.

Rather than explicitly specifying every file or typing long
filenames, you can use
wildcard characters
in place
of portions of the filenames, and the shell can usually do the work for
you. For example, the shell expands
*.txt
to a list
of all the files that end in
.txt
. File wildcard
constructs like this are called
file globs
, and
their use is awkwardly called
globbing
. Using file
globs to specify multiple files is certainly a convenience, and in many
cases is required to get anything useful accomplished. Wildcards for
shell globbing are listed in
Table 6-5
.

Table 6-5. Common file-naming wildcards

Wildcard

Description

*

Commonly thought to “match anything,”
it actually will match zero or more characters (which includes
“nothing”!). For example,
x*
matches files or directories
x
,
xy
,
xyz
,
x.txt
,
xy.txt
,
xyz.c
,
and so on.

?

Match exactly one character. For
example,
x?
matches files or
directories
xx, xy, xz
, but not
x
and not
xyz
. The
specification
x??
matches
xyz
, but not
x
and
xy
.

[
characters
]

Match any single character from among
characters
listed between the
brackets. For example,
x[yz]
matches
xy
and
xz
.

[!
characters
]

Match any single character other than
characters
listed between the
brackets. For example,
x[!yz]
matches
xa
and
x1
but
does not match
xy
or
xz
.

[
a
-
z
]

Match any single character from among
the range of characters listed between the brackets and
indicated by the dash (the dash character is not matched). For
example,
x[0-9]
matches
x0
and
x1
, but does
not match
xx
. Note that to match both
upper- and lowercase letters (Linux filenames are
case-sensitive), you specify
[a-zA-Z]
. Using
x[a-zA-Z]
matches
xa
and
xA
.

[!
a
-
z
]

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

{
frag1
,
frag2
,
frag3
,
...
}

Create strings
frag1
,
frag2
,
frag3
, etc. For example,
file_{one,two,three}
yields the
strings
file_one
,
file_two
, and
file_three
. This is a special
operator named
brace expansion
that can be
used to match filenames but isn’t specifically a file wildcard
operator and does not examine directories for existing files to
match. Instead, it will expand
any
string
.

For example, it can be used with
echo
to yield strings totally
unrelated to existing filenames:

$
echo string_{a,b,c}
string_a string_b string_c

A few examples of the useful things you can do with wildcards
follow:

  • If you remember part of a filename but not the whole thing,
    use wildcards with the portion you remember to help find the file.
    For example, if you’re working in a directory with a large number of
    files and you know you’re looking for a file named for Linux, you
    may enter a command like this:

    $
    ls -l *inux*
  • When working with groups of related files, wildcards can be
    used to help separate the groups. For example, suppose you have a
    directory full of scripts you’ve written. Some are Perl scripts, for
    which you’ve used an extension of
    .pl
    , and some
    are Python, which have a
    .py
    extension. You may
    wish to separate them into new, separate directories for the two
    languages like this:

    $
    mkdir perl python
    $
    mv *.pl perl
    $
    mv *.py python
  • Wildcards match directory names as well. Suppose you have a
    tree of directories starting with
    contracting
    ,
    where you’ve created a directory for each month (that is,
    contracting/january
    ,
    contracting/february
    , through
    contracting/december
    ). In each of these
    directories are stored invoices, named simply
    invoice_
    custa
    _01.txt
    ,
    invoice_
    custa
    _02.txt
    ,
    invoice_custb_01.txt
    , and so on, where
    custa
    and
    custb
    are customer names of some form. To
    display all of the invoices, wildcards can be used:

    $
    ls con*/*/inv*.txt

    The
    con*
    matches
    contracting
    . The second * matches all
    directories under the
    contracting
    directory
    (
    january
    through
    december
    ). The last * matches all the customers
    and each invoice number for each customer.

See the
bash
manpages or info page for
additional information on how
bash
handles
expansions and on other expansion forms.

Other books

Meeting the Step by Adams, Ash
Strike Out by Cheryl Douglas
Someone Like Summer by M. E. Kerr
Ghost to the Rescue by Carolyn Hart
Emerald of the Elves by Richard S. Tuttle
Palmetto Moon by Kim Boykin