Read LPI Linux Certification in a Nutshell Online
Authors: Adam Haeder; Stephen Addison Schneiter; Bruno Gomes Pessanha; James Stanger
Tags: #Reference:Computers
tr
tr [options
] [string1
[string2
]]
Translate characters fromstring1
to the corresponding characters instring2
.
tr
does
not
have file arguments and therefore must use
standard input and output.
Note thatstring1
andstring2
should contain the same number of
characters since the first character instring1
will be replaced with the first
character instring2
and so on.
Eitherstring1
orstring2
can contain several types of
special characters. Some examples follow, although a full list can be
found in thetr
manpage.
a
-
z
All characters froma
toz
.
\\
A backslash (\
)
character.
\
nnn
The ASCII character with the octal valuennn
.
\
x
Various
control characters:
\a bell
\b backspace
\f form feed
\n newline
\r carriage return
\t horizontal tab
\v vertical tab
Use the complement of (or all characters
not
in)string1
.
Delete characters instring1
from the output.
Squeeze out repeated output characters instring1
.
To
change all
lowercase characters in
file1
to
uppercase, use:
$cat file1 | tr a-z A-Z
or:
$cat file1 | tr '[:lower:]' '[:upper:]'
To suppress repeated
whitespace characters from
file1
:
$cat file1 | tr -s '[:blank:]'
To remove all
non-printable characters from
file1
(except the newline character):
$cat file1 | tr -dc '[:print:]\n'
unexpand
unexpand [options
] [files
Convert
spaces to
Tabs. This command performs the opposite action of
expand
. By default, Tab stops are assumed to be
every eight spaces.
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.
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.
number
Specify Tab stops in place of the default 8.
uniq
uniq [options
] [input
[output
]]
Writesinput
(or
stdin
) tooutput
(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
.
Print only nonunique (repeating) lines.
Print only unique (nonrepeating) lines.
Suppose
file
contains the following:
b
b
a
a
c
d
c
Issuing the commanduniq
with
no options:
$uniq file
yields the following output:
b
a
c
d
c
Notice that the line withc
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, usesort
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
wc
wc [options
] [files
]
Print counts of characters, words, and lines forfiles
. When multiple files are listed,
statistics for each file output on a separate line with a cumulative
total output last.
Print the character count only.
Print the line count only.
Print the word count only.
Show all counts and totals for
file1
,
file2
, and
file3
:
$wc file[123]
Count the number of lines in
file1
:
$wc -l file1
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).
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
.
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
.
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.
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
.
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.
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
.
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,” |
| Match exactly one character. For |
| Match any single character from among |
| Match any single character other than |
| Match any single character from among |
| Match any single character from among |
| Create strings For example, it can be used with $ |
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, wherecusta
andcustb
are customer names of some form. To
display all of the invoices, wildcards can be used:
$ls con*/*/inv*.txt
Thecon*
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.