Chapter 5. Unix Basic Commands _ 자주 사용되지는 않는 명령어
자주 사용되지는 않는 명령어
■ cmp CMD / diff CMD
■ sort CMD
■ file CMD
cmp CMD / diff CMD
__________________
compare two files // 텍스트 비교
The cmp utility compares two files. cmp will write no output
if the files are the same. Under default options, if they
differ, it writes to standard output the byte and line
numbers at which the first difference occurred. Bytes and
lines are numbered beginning with 1. If one file is an ini-
tial subsequence of the other, that fact is noted. skip1 and
skip2 are initial byte offsets into file1 and file2 respec-
tively, and may be either octal or decimal; a leading 0
denotes octal.
The diff utility will compare the contents of file1 and
file2 and write to standard output a list of changes neces-
sary to convert file1 into file2. This list should be
minimal. Except in rare circumstances, diff finds a smallest
sufficient set of file differences. No output will be pro-
duced if the files are identical.
(명령어 형식)
# cmp file1 file2 두개의 파일에 대한 간략한 차이점 확인
# diff file1 file2 두개의 파일에 대한 자세한 차이점 확인
# diff -c file1 file2 // -c : 좀더 디테일하게
[EX] cmp 명령어 실습
# cd /test
# cat > file1
1111
2222
3333
<CTRL + D>
# cat file1
# cp file1 file2
# cat > file3
1111
2222
4444
<CTRL + D>
# cat file3
file1 <------> file2 : 같은 파일
file1 <------> file3 : 다른 파일
# cmp file1 file2
# cmp file1 file3
file1 file3 differ: char 11, line 3
# diff file1 file2
# diff file1 file3
# diff -c file1 file3
[참고] cmp 명령어를 사용한 백업 파일과 원본 파일 비교
원본 DISK 백업 테이프
-------------- ----------------
설정파일
/etc/ftpd/ftpaccess /etc/ftpd/ftpacess
A |
--|------------ --------|-------
| |
+---> 원본 파일과 비교 <--------+ 복구
# cd /test
# cp /etc/ftpd/ftpaccess /test
# cp ftpaccess ftpaccess.orig
# vi ftpaccess
......
[수정 전]
chmod no anonymous
delete no anonymous
[수정 후]
chmod yes anonymous
delete yes anonymous
# diff ftpaccess ftpaccess.orig
(출력 결과 분석)
[참고] cmp 명령어를 통한 파일의 무결성 점검(File Consistency Check)
파일 무결성 점검 스크립트 파일의 원리(File Check Principle)
(1). /etc/passwd => /etc/passwd.old
(2). cmp /etc/passwd /etc/passwd.old
cmp /etc/passwd /etc/passwd.old > file.txt
if [ -s file.txt ] ; then
Action1
else
Action2
fi
[참고] 172.16.8.254:/root/shell/check_file.sh
# mkdir /root
# rcp -r 172.16.8.254:/root/shell /root
sort CMD
______________________
sort, merge, or sequence check text files
The sort command sorts lines of all the named files together
and writes the result on the standard output.
Comparisons are based on one or more sort keys extracted
from each line of input. By default, there is one sort key,
the entire input line. Lines are ordered according to the
collating sequence of the current locale.
(명령어 형식) // separate default : 스페이스 , 탭
# sort /etc/passwd
# sort -r /etc/passwd (-r : reverse , 거꾸로 내림차순)
# sort -t : -k 3 -n /etc/passwd (-t : seperate, -k : key, -n : number)
# sort -k 3 filename // -f # : 필드 구분, # 은 구분자
# sort -k 3,5 filename // -k 필드 3,5 로 sort
[참고] 많이 실행되는 sort 명령어 실행 방법
# CMD | sort
# CMD | sort -r
# CMD | sort -k 3
[EX] sort 명령어 실습
# rcp 172.16.8.254:/root/conf_sample/DB.txt /test
# cat DB.txt
Chan1 10 20 30 50
Chan2 20 25 31 20
Chan3 30 20 30 40
Chan4 50 20 30 80
# sort DB.txt
# sort -r DB.txt
# sort -k 3 -n DB.txt
# sort -k 3,5 -n DB.txt
[EX] sort 명령어 활용 예
(윈도우 컴퓨터의 예)
c:\ 9.9G/10G
# cd /var
# df –h // -h : human
# du -sk /var
# du -sk * | sort
# du -sk * | sort -n
# du -sk * | sort -nr | more
- s : sum
- k : kbytes
file CMD
____________________
determine file type
The file utility performs a series of tests on each file
supplied by file and, optionally, on each file listed in
ffile in an attempt to classify it. If the file is not a
regular file, its file type is identified. The file types
directory, FIFO, block special, and character special are
identified as such. If the file is a regular file and the
file is zero-length, it is identified as an empty file.
(명령어 형식)
# file /etc/passwd
[EX] 파일의 종류 확인
# file /etc/passwd
/etc/passwd: ascii text (ASCII 파일)
# file /usr/bin/ls
/usr/bin/ls: ELF 32-bit MSB executable SPARC Version 1, dynamically linked, stripped (Binary 파일)
# file /var/adm/utmpx
/var/adm/utmpx: data (Data 파일)
# file /etc/hosts /etc
/etc/hosts: ascii text
/etc: directory (Directory 파일)
[참고] 특정 디렉토리 하위의 많은 파일들의 형식
# cd /var
# file *
Windows -> 파일.확장자(0)
Unix -> 파일.확장자(X)
[EX] file 명령어의 활용예
Internet File : file.tar.gz -> file.tar (Modification)
# mv file.tar.gz file.tar
# ls -l file.tar
# tar xvf file.tar
# file file.tar
# mv file.tar file.tar.gz
# gzip -d file.tar.gz
# tar xvf file.tar
출처 : http://cafe.daum.net/bscsolaris