Read LPI Linux Certification in a Nutshell Online
Authors: Adam Haeder; Stephen Addison Schneiter; Bruno Gomes Pessanha; James Stanger
Tags: #Reference:Computers
bzip2
bzip2 [options
] [filenames ...
]
bunzip2 [options
] [filenames ...
]
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
.
Decompress a file.
bzip2 –d
is
the same as
bunzip2
.
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.
Compress the file
/etc/largefile
using the highest level
of compression. It will be compressed and renamed
/etc/largefile.bz2
:
$bzip2 -9 /etc/largefile
Uncompress
/etc/largefile.bz2
. It will be
uncompressed and renamed
/etc/largefile
:
$bunzip2 /etc/largefile.bz2
or:
$bzip2 -d /etc/largefile.bz2
cp
cp [options
]file1 file2
cp [options
]files directory
In the first command form, copyfile1
tofile2
. Iffile2
exists and you have appropriate
privileges, it will be overwritten without warning (unless you use
the
-i
option). Bothfile1
andfile2
can be any valid filename, either
fully qualified or in the local directory. In the second command
form, copyfiles
todirectory
. Note that the presence of
multiple files implies that you wish to copy the files to a
directory. Ifdirectory
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 withfile2
instead ofdirectory
.
Force an overwrite of existing files in the
destination.
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.
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.
Recursively
copy
directories. You may use either upper- or lowercase for this
option. Iffile1
is actually a
directory instead of a file and the recursive option is
specified,file2
will be a copy
of the entire hierarchy under directoryfile1
.
Display the name of each file verbosely before
copying.
Copy the messages file to the local directory (specified by.
):
$cp /var/log/messages .
Make an identical copy, including preservation of file
attributes, of directorysrc
in new
directorysrc2
:
$cp -Rp src src2
Copyfile1, file2, file5, file6
,
andfile7
from the local directory into
your home directory (inbash
):
$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.
cpio
cpio –o [options
] < [filenames ...
] > [archive]
cpio –i < [archive]
cpio –p [destination-directory] < [filenames...]
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
.
Copy-out mode. This mode is used to create an
archive.
Copy-in mode. This mode is used to copy files out of
an archive.
Copy-pass mode. Don’t create an archive; just copy
files from one directory tree to another.
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.
Extract all the files from the archive we just
created:
$cpio –iv < /tmp/archive.cpio
dd
dd [options
]
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.
file
Read fromfile
instead of
standard input.
file
Output tofile
instead of
standard output.
n
Readn
bytes at a
time.
n
Writen
bytes at a
time.
list
Perform the conversions defined inlist
.
Create an image of the compact disc currently in the default
CD drive (
/dev/cdrom
):
$dd if=/dev/cdrom of=/tmp/cd.img
Copy
/tmp/file
to
/tmp/file2
, converting all
characters to lowercase:
$dd if=/tmp/file of=/tmp/file2 conv=lcase
file
file [options
] [file
]
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.
namefile
Read the names of the files to be examined fromnamefile
(one per line) before
the argument list.
Try to look inside compressed files.
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
Determine the file type of
/etc/passwd
:
$file /etc/passwd
/etc/passwd: ASCII text
find
find [options
] [path...
] [expression
]
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.
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.
X
Descend at mostX
levels of
directories below the command-line arguments.–maxdepth 0
eliminates all
recursion into subdirectories.
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.
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
Find files in
/usr
that
have thesetuid
permission bit
set (mode 4000):
$find /usr -perm -4000
gzip and gunzip
gzip [options
] [filenames ...
]
gunzip [options
] [filenames ...
]
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
.
Decompress a file.
gzip -d
is the
same as
gunzip
.
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
).
Compress the file
/etc/largefile
. It will be compressed
and renamed
/etc/largefile.gz
:
$gzip /etc/largefile
Uncompress
/etc/largefile.gz
. It will be
uncompressed and renamed
/etc/largefile
:
$gunzip /etc/largefile.gz
or:
$gzip -d /etc/largefile.gz