LPI Linux Certification in a Nutshell (16 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
7.17Mb size Format: txt, pdf, ePub
Name

bzip2

Syntax
bzip2 [
options
] [
filenames ...
]
bunzip2 [
options
] [
filenames ...
]
Description

Compress or
uncompress files using the
Burrows-Wheeler block sorting text compression
algorithm
and
Huffman coding.
bzip2
is
generally considered one of the most efficient compression
programs available for Linux systems. Files compressed with
bzip2
usually have the
extension
.bz2
.

Frequently used options
-d

Decompress a file.
bzip2 –d
is
the same as
bunzip2
.

-1
to
-9

Set the block size to 100k, 200k, 300k...900k when
compressing. This essentially means that
-1
compresses faster but leaves
larger compressed files, whereas
-9
compresses more slowly but
results in smaller files.

Example 1

Compress the file
/etc/largefile
using the highest level
of compression. It will be compressed and renamed
/etc/largefile.bz2
:

$
bzip2 -9 /etc/largefile
Example 2

Uncompress
/etc/largefile.bz2
. It will be
uncompressed and renamed
/etc/largefile
:

$
bunzip2 /etc/largefile.bz2

or:

$
bzip2 -d /etc/largefile.bz2
Name

cp

Syntax
cp [
options
]
file1 file2
cp [
options
]
files directory
Description

In the first command form, copy
file1
to
file2
. If
file2
exists and you have appropriate
privileges, it will be overwritten without warning (unless you use
the
-i
option). Both
file1
and
file2
can be any valid filename, either
fully qualified or in the local directory. In the second command
form, copy
files
to
directory
. Note that the presence of
multiple files implies that you wish to copy the files to a
directory. If
directory
doesn’t exist,
an error message will be printed. This command form can get you in
trouble if you attempt to copy a single file into a directory that
doesn’t exist, as the command will be interpreted as the first
form and you’ll end up with
file2
instead of
directory
.

Frequently used options
-f

Force an overwrite of existing files in the
destination.

-i

Prompt
interactively
before
overwriting destination files. It is common practice (and
advised) to alias the
cp
command to
cp -i
to prevent accidental overwrites.
You may find that this is already done for you for the
root
user on your Linux system.

-p

Preserve
all information,
including owner, group, permissions, and timestamps. Without
this option, the copied file or files will have the present
date and time, default permissions, owner, and group.

-r
,
-R

Recursively
copy
directories. You may use either upper- or lowercase for this
option. If
file1
is actually a
directory instead of a file and the recursive option is
specified,
file2
will be a copy
of the entire hierarchy under directory
file1
.

-v

Display the name of each file verbosely before
copying.

Example 1

Copy the messages file to the local directory (specified by
.
):

$
cp /var/log/messages .
Example 2

Make an identical copy, including preservation of file
attributes, of directory
src
in new
directory
src2
:

$
cp -Rp src src2

Copy
file1, file2, file5, file6
,
and
file7
from the local directory into
your home directory (in
bash
):

$
cp file1 file2 file[567] ~

On the Exam

Be sure to know the difference between a
file destination and a directory destination and
how to force an
overwrite of existing objects.

Name

cpio

Syntax
cpio –o [
options
] < [
filenames ...
] > [archive]
cpio –i < [archive]
cpio –p [destination-directory] < [filenames...]
Description

cpio
is used to
create and extract archives, or copy files from one
place to another. No
compression
is done natively on
these archives; you must employ
gzip
or
bzip2
if you desire
compression
.

Frequently used options
-o

Copy-out mode. This mode is used to create an
archive.

-i

Copy-in mode. This mode is used to copy files out of
an archive.

-p

Copy-pass mode. Don’t create an archive; just copy
files from one directory tree to another.

Example 1

Create an archive that contains all the files in the current
working directory:

$
ls | cpio –ov > /tmp/archive.cpio

Notice that instead of passing files to archive to
cpio
on the command line, we had the
ls
command create a list of files for us,
which we then send to the
cpio
command via
standard input using the
|
(vertical bar) character.

Example 2

Extract all the files from the archive we just
created:

$
cpio –iv < /tmp/archive.cpio
Name

dd

Syntax
dd [
options
]
Description

dd
converts and copies files.
It is one of the few commands in the Linux world that can operate
directly on
block devices, rather than requiring access through
the filesystem layer. This is especially useful when performing
backups of block devices, such as hard drive partitions, CD-ROMs,
or floppy disks.

Frequently used options
-if=
file

Read from
file
instead of
standard input.

-of=
file

Output to
file
instead of
standard output.

-ibs=
n

Read
n
bytes at a
time.

-obs=
n

Write
n
bytes at a
time.

-conv=
list

Perform the conversions defined in
list
.

Example 1

Create an image of the compact disc currently in the default
CD drive (
/dev/cdrom
):

$
dd if=/dev/cdrom of=/tmp/cd.img
Example 2

Copy
/tmp/file
to
/tmp/file2
, converting all
characters to lowercase:

$
dd if=/tmp/file of=/tmp/file2 conv=lcase
Name

file

Syntax
file [
options
] [
file
]
Description

file
is designed to determine
the kind of file being queried. Because Linux (and other Unix-like
systems) don’t require filename extensions to determine the type
of a file, the
file
command is useful when
you’re unsure what kind of file you’re dealing with.
file
accomplishes this by performing three
sets of tests on the file in question: filesystem tests, magic
tests, and language tests.
Filesystem tests involved examining the output of
the
“stat” system call.
Magic tests are used to check for files with data in
particular fixed formats. If neither of these tests results in a
conclusive answer, a
language test is performed to determine whether the
file is some sort of text file.

Frequently used options
-f
namefile

Read the names of the files to be examined from
namefile
(one per line) before
the argument list.

-z

Try to look inside compressed files.

Example 1

Determine the file type of the currently running
kernel:

$
file /boot/vmlinuz-2.6.27.29-170.2.78.fc10.i686
/boot/vmlinuz-2.6.27.29-170.2.78.fc10.i686: Linux kernel x86 boot executable \
RO-rootFS, root_
0x902, swap_dev 0x2, Normal VGA
Example 2

Determine the file type of
/etc/passwd
:

$
file /etc/passwd
/etc/passwd: ASCII text
Name

find

Syntax
find [
options
] [
path...
] [
expression
]
Description

find
searches
recursively through directory trees for files or directories that
match certain
characteristics
.
find
can then either print the file or
directory that matches or perform other operations on the
matches.

Frequently used options
-mount

Do not recursively descend through directories on
mounted filesystems. This prevents
find
from doing a potentially very long search over an
NFS-mounted share, for example.

-maxdepth
X

Descend at most
X
levels of
directories below the command-line arguments.
–maxdepth 0
eliminates all
recursion into subdirectories.

Example 1

Find all files in
/tmp
that end in
.c
and print them
to standard out:

$
find /tmp –name "*.c"

The expression
"*.c"
means “all files that end in .c”. This is an example of
file globbing
and is explained
in detail later in this Objective.

Example 2

Find files (and only files) in
/tmp
older than seven days. Do not
recurse into subdirectories of
/tmp
:

$
find /tmp -maxdepth 1 -type f -daystart -ctime +7
Example 3

Find files in
/usr
that
have the
setuid
permission bit
set (mode 4000):

$
find /usr -perm -4000
Name

gzip and gunzip

Syntax
gzip [
options
] [
filenames ...
]
gunzip [
options
] [
filenames ...
]
Description

Compress or
uncompress files using Lempel-Ziv coding.
gzip
is one of the most common compression
formats found on Linux systems, although it is starting to be
replaced by the more efficient
bzip2
. Files
compressed with
gzip
usually
have the extension
.gz
.
Command-line options for
gzip
are very
similar to those for
bzip2
.

Frequently used options
-d

Decompress a file.
gzip -d
is the
same as
gunzip
.

-r

Travel the directory structure recursively. If any of
the filenames specified on the command line are directories,
gzip
will descend into the directory
and compress all the files it finds there (or decompress
them in the case of
gunzip
).

Example 1

Compress the file
/etc/largefile
. It will be compressed
and renamed
/etc/largefile.gz
:

$
gzip /etc/largefile
Example 2

Uncompress
/etc/largefile.gz
. It will be
uncompressed and renamed
/etc/largefile
:

$
gunzip /etc/largefile.gz

or:

$
gzip -d /etc/largefile.gz

Other books

Get Well Soon by Julie Halpern
Shakespeare by Peter Ackroyd
My Only Wish by Anna Robbins
The Rule of Won by Stefan Petrucha
Women Aviators by Karen Bush Gibson
The Last Princess by Stacey Espino
A Florentine Death by Michele Giuttari
Fuck buddies by Klaus, Shirin