Chapter 5. Unix Basic Commands _ 아카이브/압축 관련 명령어
압축(Compress)
■ compress / uncompress CMD
■ gzip / gunzip CMD
아카이브(Archive)
■ tar CMD
■ cpio CMD
압축 + 아카이브
■ zip CMD
■ jar CMD
compress/uncompress CMD
___________________________
compress, uncompress, zcat - compress,
uncompress files or display expanded files
[compress]
The
compress utility will attempt to reduce the size of the
named
files by using adaptive Lempel-Ziv coding. Except when
the output
is to the standard output, each
file will be
replaced by one
with the extension .Z, while
keeping the
same
ownership modes, change times and
modification times.
If appending
the .Z to the
file pathname would make the
pathname
exceed 1023 bytes, the command will fail.
If no
files are
specified, the standard input will be compressed
to the
standard output.
[uncompress]
The
uncompress utility will restore files to their
original
state after
they have been compressed using the compress
utility.
If no files are specified, the standard input
will
be
uncompressed to the standard output.
[zcat]
The zcat
utility will write
to standard output
the
uncompressed form of files that have been compressed using
compress.
It is the equivalent of uncompress -c. Input files
are not
affected.
---- file1 -----
aaaaa a(addr1,addr2,addr3,....)
bbbbb
====Compress===>
b(addr1,addr2,addr3,....)
ccccc .......
----------------
[그림] 압축의 원리
(압축에 대한 예1)
- 똑같은 페이지 수를 가지고 있는 두개의 파일을 압축하더라도 압축 효율은 서로 틀리다.
이유는 반복되는 글자 수가 서로 틀리기 때문이다.
(압축에 대한 예2)
- 압축된 파일을 다시 압축하거나 바이너리 파일을 다시 압축하는 경우는 압축효율이 떨어
질수 있다. 이유는 반복되는 글자가 많지 않고 글자 종류의 수가 증가 하기 때문이다.
Compress File
--- Compress ---> size(?)
Binary
File --- Compress ---> size(?)
■ (파일 압축)
# compress file
file.Z
■ (파일 압축 확인)
# zcat file.Z
# uncompress -c file.Z
■ (파일 압축 해제)
# uncompress file.Z
file
[EX] compress 명령어 실습
# cd /test
# cp /etc/inet/inetd.conf file1
# compress file1
file1.Z
# ls -l (File Size)
# zcat file1.Z (0)
# cat file1.Z
(X)
# uncompress file1.Z
# ls -l (File Size)
gzip/gunzip CMD
____________________
gzip, gunzip, gzcat - compress or expand files
[gzip]
Gzip
reduces the size of the named files using
Lempel-Ziv
coding (LZ77). Whenever possible, each file is replaced by
one with
the extension .gz, while keeping the same ownership
modes, access and modification times. (The default exten-
sion is
-gz for VMS, z for MSDOS, OS/2 FAT, Windows
NT FAT
and
Atari.) If no files are specified, or if
a file name is
"-", the standard input is compressed to the standard
out-
put. Gzip will only attempt to compress regular
files. In
particular, it will ignore symbolic links.
[gunzip]
gunzip
takes a list of
files on its
command line and
replaces
each file whose name ends with .gz, -gz, .z, -z, _z
or .Z and
which begins with the correct magic number with an
uncompressed file without
the original extension. gunzip
also
recognizes the special extensions .tgz
and .taz as
shorthands for .tar.gz
and .tar.Z respectively. When
compressing, gzip uses the
.tgz extension if
necessary
instead of
truncating a file with a .tar extension.
[gzcat]
gzcat is
identical to gunzip -c. (On some systems, zcat
may
be installed
as gzcat to
preserve the original link to
compress.)
gzcat uncompresses either a list of files on
the
command line or
its standard input
and writes the
uncompressed data on standard output.
gzcat will uncompress
files that
have the correct magic number whether they have a
.gz suffix
or not.
■ (파일 압축)
# gzip file
file.gz
■ (파일 압축 확인)
# gzcat file.gz
or
# gunzip -c file.gz
■ (파일 압축 해제)
# gzip -d file.gz (-d : decompress)
file
or
# gunzip file.gz
file
[EX] gzip 명령어 실습
# cp /etc/inet/inetd.conf file2
# gzip file2
file2.gz
# ls -l (파일의 크기 점검)
# gzcat file2.gz (0)
# cat file2.gz
(X)
# gzip -d file2.gz
file1
# ls -l (파일의 크기 점검)
tar CMD
____________________
create tape archives and add or extract files
(명령어 형식)
# tar cvf file.tar file1 file2 file3 (c: create)
# tar tvf file.tar (t: contents)
# tar xvf file.tar (x: extract)
[참고] 테이프 백업 방법(Tape Device Backup)
# tar cvf /dev/rmt/0n /export/home
# tar cvf /dev/rmt/0n file1 file2 file3
[참고] 디렉토리 마이그레이션(Migration)
Directory Copy using tar CMD.
작업 내용: /test1 => /test2
# cd /test1
# tar cvf - . | (cd /test2 ; tar xvf -)
[EX] tar 명령어 실습
(실습 준비)
# cd /test
# rm -r *
# cp /etc/passwd file1
# cp file1 file2
# cp file1 file3
# ls -l file*
(tar 명령어를 사용한 실습)
# tar cvf file.tar file1 file2 file3
# ls -l file*
# rm file1 file2 file3
# tar tvf file.tar
# tar xvf file.tar
# ls -l file*
(tar 명령어를 사용한 디렉토리 마이그레이션)
# mkdir -p /test1
# mkdir -p /test2
# cp /etc/default/* /test1
# ls /test1
# cd /test1
# tar cvf - . | (cd /test2 ; tar xvf -)
# ls /test1
# ls /test2
cpio CMD
____________________
copy file archives in and out
The cpio
command copies files
into and out
of a cpio
archive.
The cpio archive may span multiple volumes. The -i,
-o, and -p
options select the action to be performed.
The
following list describes each of
the actions. These actions
are
mutually exclusive.
Copy In Mode
cpio -i
(copy in) extracts files from the standard
input,
which is
assumed to be the product of a previous cpio -o
command.
Only files with names that match one
of the pat-
terns are
selected. See sh(1) and OPERANDS for more informa-
tion about
pattern. Extracted files are conditionally copied
into the
current directory tree,
based on the options
described
below. The permissions of the files will be
those
of the
previous cpio -o command. The owner and group will be
the same
as the current user, unless the current user is the
super-user. If this is the case, owner and group will be the
same as
those resulting from the previous cpio
-o command.
Notice that if cpio -i tries to create a file that
already
exists and
the existing file is the same
age or younger
(newer), cpio will output a
warning message and not replace
the file.
The -u option can
be used to
unconditionally
overwrite
the existing file.
Copy Out Mode
cpio -o
(copy out) reads a list of file path names from
the
standard input and copies those files to the standard out-
put,
together with path name and status information
in the
form of
a cpio archive. Output is padded to an 8192-byte
boundary
by default or to the
user-specified block size
(with the
-B or -C
options) or to some device-dependent
block size
where necessary (as with the CTC tape).
Pass Mode
cpio -p
(pass) reads a list of file path
names from the
standard
input and conditionally copies those files into the
destination directory tree, based on the
options described
below.
-i (copy in) Reads an archive from the
standard input and
conditionally extracts the files contained in it and
places them into the current directory tree.
-o (copy out) Reads a list of file path names
from the
standard input and copies those
files to the standard
output in the form of a cpio archive.
-p (pass) Reads a list of file path names from
the stan-
dard input and conditionally copies those files into
the
destination directory tree.
-c Reads or writes header information in
ASCII character
form
for portability. There are no UID or GID restric-
tions associated with this
header format. Use
this
option between SVR4-based
machines, or the -H odc
option between unknown machines. The -c option implies
the use of
expanded device numbers, which
are only
supported on SVR4-based systems.
When transferring
files between SunOS
4 or Interactive UNIX and the
Solaris 2.6 Operating environment or
compatible ver-
sions,
use -H odc.
-v Verbose. Prints a list of file and
extended attribute
names. When used with the -t option, the table of con-
tents looks like the output of an ls -l
command (see
ls(1)).
-b Reverses the order of the bytes within each
word. Use
only
with the -i option.
-B Blocks input/output 5120 bytes
to the record.
The
default buffer size is 8192 bytes when this and the -C
options are not used. -B does
not apply to
the -p
-d Creates directories as needed.
-m Retains previous file modification
time. This option
is
ineffective on directories that are being copied.
-I file
Reads the contents of
file as an
input archive,
instead of the standard input. If
file is a character
special device, and the current medium has been
com-
pletely read, replace the medium and press <RETURN> to
continue
to the next medium. This option is used
only
with
the -i option.
-O file
Directs the output of cpio to
file, instead of the
standard output. If file is a character special device
and
the current medium is full, replace the medium and
type
a carriage return to continue to the next medium.
Use
only with the -o option.
cpio는 표준 입력으로부터 파일의 이름을 입력받고, 표준출력으로
목록명을 얻어서
하나 또는 복수개의 파일을 압축하는데 사용된다. cpio는 3개의 다름 모드가 존재
한다.
● Copy In Mode : cpio -i, 표준 입력으로 들어온 파일들을 extract 한다.
● Copy Out Mode : cpio -o, 표준 입력으로 부터 파일을 얻어서 이들 파일을
가지고
그들의 pathname과 함께, 새로운 파일을 생성한다.
● Pass Mode : cpio -in, copy-out 모드와 같다. 다만 새로운 파일이 생기는 것이
아니라 디렉토리 구조를 그대로 copy 한다는 것만 다르다.
(명령어 형식 예)
# find . -print | cpio -o[aBcv] > file.list
# cpio -ivt < file.list
# cpio -i[cdlmv] < file.list
# find . -print | cpio -p[adlmuv] directory
[EX1] 현재 디렉토리를 tape에 Backup
# find . -print | cpio -ocvB > /dev/rmt/0
[EX2] Tape의 목차를 출력
# cpio -iBtv < /dev/rmt/0
[EX3] Backup 받은 tape로부터 restore
# cpio -iBvd < /dev/rmt/0
[EX4] 특정 파일을 find 명령어로 출력하여 archive file로 restore
# find . -name 'file*' -print | cpio -ocvB >
file.list
[EX5] 디렉토리 /export/home/user01 디렉토리를 /export/home/user02로 backup
# cd /export/home/user01
# find . -print | cpio -pdmv /export/home/user02
[EX6] 현재 디렉토리 파일들을 새로운 파일(newfile)로
아키이빙
# ls | cpio -ocv > /test/newfile
[EX7] 디렉토리 안에서 파일 풀기
# cd /test
# cat newfile | cpio -icd "memo/a1"
"memo/b*"
zip CMD
____________________
zip, zipcloak, zipnote, zipsplit - package and
compress (archive) files
zip is a
compression and file packaging
utility for Unix,
VMS, MSDOS,
OS/2, Windows NT, Minix, Atari
and Macintosh,
Amiga and
Acorn RISC OS.
It is
analogous to a combination of the UNIX commands tar(1)
and compress(1)
and is compatible with PKZIP
(Phil Katz's
ZIP for
MSDOS systems).
# man zip
.....
-r Travel the directory structure
recursively; for exam-
ple:
zip -r foo foo
In
this case, all the files and directories in foo
are
saved in a zip archive named foo.zip, including files
with
names starting with ".", since the recursion does
not use the shell's file-name substitution mechanism.
If
you wish to include only a specific
subset of the
files in directory foo and its
subdirectories, use the
-i
option to specify
the pattern of
files to be
included. You should
not use -r with the name ".*",
since
that matches ".." which will
attempt to zip up
the
parent directory (probably not what was intended).
.....
# man unzip
.....
-c extract files to stdout/screen
(``CRT''). This option
is similar to
the -p option except that the
name of
each
file is printed as it is extracted, the -a
option
is allowed, and
ASCII-EBCDIC conversion is automati-
cally
performed if appropriate. This option
is not
listed in the unzip usage screen.
.....
■ (파일 압축)
# zip file.zip file1 file2 file3
■ (파일 압축 확인)
# unzip -c file.zip
■ (파일 압축 해제)
# unzip file.zip
[참고] 명령어 호환
Unix zip 명령어<->
Unix jar 명령어
Unix zip 명령어<->
Window zip 명령어
jar CMD
____________________
archive tool
for Java archives
# jar cvf file.jar file1 file2 file3
# jar tvf file.jar
# jar xvf file.jar
dd CMD
---------------------
convert and copy a file
The dd
utility copies the specified input file to the speci-
fied output
with possible conversions. The
standard input
and output
are used by default. The input and
output block
sizes may
be specified to take advantage of raw physical
I/O. Sizes
are specified in bytes; a number may end with
k,
b, or
w to specify
multiplication by 1024, 512, or 2,
respectively. Numbers may also be separated by x to indicate
multiplication.
The dd
utility reads the input one block at
a time, using
the specified input block size. dd then processes
the block
of data
actually returned, which could be smaller
than the
requested block size. dd applies any conversions that have
been
specified and writes the resulting data to
the output
in blocks
of the specified output block size.
# man dd
.....
if=file
Specifies the input
path. Standard input
is the
default.
of=file
Specifies the output path. Standard
output is the
default. If the
seek=expr conversion is not
also
specified, the output file will
be truncated before
the copy begins, unless conv=notrunc
is specified. If
seek=expr is specified, but conv=notrunc is not,
the
effect of the copy will be to preserve the blocks in
the
output file over which dd seeks, but no other por-
tion
of
the output file will be preserved. (If the
size
of the seek plus the size of the
input file is
less than the
previous size of the output file, the
output file is shortened by the copy.)
bs=n Sets both input and output block sizes
to n bytes,
superseding ibs= and obs=. If no conversion other than
sync, noerror, and notrunc is
specified, each input
block is copied
to the output
as a single block
without
aggregating short blocks.
skip=n
Skips n input blocks (using the specified input block
size) before starting to copy. On
seekable files, the
implementation reads the blocks or seeks past them. On
non-seekable files,
the blocks are read and the data
is
discarded.
seek=n
Skips n blocks (using the specified output block size)
from beginning of output file
before copying. On non-
seekable files, existing blocks
are read and
space
from the current end-of-file to
the specified offset,
if
any, is filled with null bytes. On seekable
files,
the implementation seeks
to the specified offset or
reads the blocks as described for non-seekable files.
count=n
Copies only n input blocks.
.....
(명령어 형식)
# dd if=filename of=filename bs=n seek=n skip=n
count=n
(if : Input File, of : Output File, bs : Block Size)
[EX1] 큰 파일 생성(Big File creation)
# dd if=/dev/zero of=/swap/swapfile bs=1024
count=102400
[EX2] VTOC/Super Block 삭제
# dd if=/dev/zero of=/dev/rdsk/c0t0d0s4 bs=512
count=1
# dd if=/dev/zero of=/dev/rdsk/c0t0d0s4 bs=512
count=32
# dd if=/dev/zero of=/dev/rdsk/c0t0d0s1 bs=512
skeep=11 count=1
[EX3] 디스크 마이그레이션(Disk Migration)
# dd if=/dev/dsk/c0t0d0s2 of=/dev/dsk/c0t1d0s2
# dd if=/dev/rdsk/c0t0d0s2 of=/dev/rdsk/c0t1d0s2
bs=4096
# dd if=/dev/rdsk/c0t0d0s2 of=/dev/rdsk/c0t1d0s2
bs=128k
[EX4] 테잎 장치에 백업
# dd if=/dev/rdsk/c0t0d0s7 of=/dev/rmt/0
# dd if=/dev/rmt/0 of=/dev/rmt/1
[참고문서] /root/docs_html/Reference/DiskMigration.txt
출처 : http://cafe.daum.net/bscsolaris