Chapter 5. Unix Basic Commands _ 파일관리
파일 관리
■ touch CMD 파일 생성
■ cp CMD 파일 복사
■ mv CMD 파일 이동
■ rm CMD 파일 삭제
touch CMD
________________
touch, settime - change file access and modification times
The touch utility sets the access and modification times of
each file. The file operand is created if it does not
already exist.
빈 파일을 만들때 사용
- if file1 existed
- if file1 not existed
(명령어 형식)
# touch file1
# touch file1 file2
[참고] touch 명령어
만약 ex1.txt 파일이 존재하는 경우 "# touch ex1.txt" 사용하였다면 ex1.txt 파일의
mtime을 현재 시간으로 변경한다.
[참고] 해커(Cracker)들의 touch 명령어 사용
# ls -l file3 > file.txt
# cat file.txt
# vi file3
# touch -t 12061223 file3 (12월 6일 12:23, 파일의 mtime을 원래대로 돌린다.)
cp CMD
________________
copy files
(명령어 형식)
# cp file1 file2
# cp file1 dir1
# cp -r dir1 dir2 (if dir2 exist - dir2/dir1 생성) 기존 dir2가 있다면 dir2/dir1생성
(if dir2 not exist - dir2 생성) 기존 dir2이 없다면 생성
// mkdir –p와 비슷
# cp -i file1 file2 // -i : interactive (대화모드)
OPTIONS
-i Interactive. cp will prompt for confirmation whenever
the copy would overwrite an existing target. A y
answer means that the copy should proceed. Any other
answer prevents cp from overwriting target.
-r Recursive. cp will copy the directory and all its
files, including any subdirectories and their files to
target.
[참고] "cp -i" alias 설정
# vi ~/.kshrc(ksh인 경우)
......
alias cp='cp -i'
......
[EX] cp 명령어 실습
(실습 준비)
# cd /test
# rm -r *
# cp /etc/passwd file1
(cp file1 file2 형식 실습)
# cp file1 file2
(cp file1 dir1 형식 실습)
# mkdir dir1
# cp file1 dir1
# ls dir1
(cp -r dir1 dir2 형식 실습)
# cp -r dir1 dir2
# cp -r dir1 dir2
# ls -R
[EX] 큰 로그 파일 삭제 방법
# cp /dev/null server.log
[EX] cp 명령어에 대한 inode 변화 과정 // 원본 파일이 지워지지 않고 생기기때문
# cd /test
# touch file1
# ls -li file1 (Inode : 109326)
# cp file1 file2
# ls -li file1 file2 (file1 Inode : 109326, file2 Inode : 109341)
mv CMD
_________________
move files (rename)
(명령어 형식)
# mv file1 file2
# mv file1 dir1
# mv dir1 dir2 (if dir2 exist - dir1을 dir2로 move)
(if dir2 not exist - dir1을 dir2/dir1으로 move)
# mv -i file1 file2
[참고] mv => rename
[EX1] mv 명령어 실습
(실습 준비)
# cd /test
# rm -r *
# touch file1
# mkdir dir1
# touch dir1/file2
(mv file1 file2 형식 실습)
# mv file1 file3
(mv file1 dir1 형식 실습)
# mv file3 dir1
(mv dir1 dir2 형식 실습)
# mv dir1 dir2
# cp -r dir2 dir1
# mv dir1 dir2
[EX2] mv 명령어에 대한 inode 변화 과정 // 원본 데이터의 이름만 바뀜
# cd /test
# touch file1
# ls -li file1 (Inode : 109326)
# mv file1 file3
# ls -li file3 (file3 Inode : 109326)
rm CMD
_________________
(명령어 형식)
# rm file1
# rm file1 file2
# rm -r dir1
# rm -i file1
[참고] rm -i & rm -f
# alias rm='rm -i'
# rm -r /test/dir1
# \rm -r /test/dir1
[EX] \rm -i 명령어 실습
(실습 준비)
# cd /test
# rm -r *
# mkdir dir1
# touch dir1/file1
# touch dir1/file2
(\rm -i 옵션 실습)
# alias rm='rm -i'
# rm -r dir1
rm: examine files in directory dir1 (yes/no)? <----- <Ctrl + C> 입력
# \rm -r dir1
[참고] 역슬래쉬(Backslash)의 사용
# echo $HOSTNAME
# echo \$HOSTNAME
# find / \( -perm -4000 -o -perm -2000 \) -type f
# find / -name core -type f -exec rm {} \;
[참고] 파일 생성 및 삭제 과정
● 파일 생성시
1) 슈퍼 블럭에서 하나의 Inode를 할당한다.
2) Inode의 내용들을 초기화한다.
3) Inode-Number와 파일시스템을 해당 상위 디렉토리에 기록한다.
4) Inode-List(Inode Table)에 있는 한 엔트리를 할당한다.
5) 사용자 영역의 파일 지시자(File Descriptor)에 한 엔트리를 할당한다.
6) 파일 지시자를 프로세스에게 반환한다.
● 파일 삭제시
1) 주어진 경로명을 Inode-List(Inode Table)의 포인터로 바꾼다.
2) 주어진 파일이 마운트되었으면 삭제 못하고 반환한다.
3) 주어진 파일이 공유된 텍스트이고 링크 수가 1이면 반환한다.
4) 파일의 링크 수를 하나 감소 시키고, 상위 디렉토리에서 그 파일의 Inode Number를
0으로 설정한다.
clear CMD
_________________
clear the terminal screen
clear clears your screen if this is possible. It looks in
the environment for the terminal type and then in the ter-
minfo database to figure out how to clear the screen.
화면의 내용을 지우고 커서를 화면의 최 상단으로 이동시킴
# clear
파일 내용 관리
■ cat CMD
■ more CMD
■ haed CMD
■ tail CMD
cat CMD
_________________
concatenate and display files
(명령어 형식)
# cat file1
# cat -n file1 (-n : Number Line)
# cat file1 file2 > file3
[EX] cat 명령어 실습
# cat /etc/passwd
# cat -n /etc/passwd
# cat /etc/passwd | grep user01
[EX] file1, file2 두개의 파일을 하나의 file3으로 합치기
Concatenates file1 and file2, and writes the result in file3
# echo 1111 > file1
# echo 2222 > file2
# cat file1 file2
# cat file1 file2 > file3
# cat file3
more CMD
_________________
browse or page through a text file
The more utility is a filter that displays the contents of a
text file on the terminal, one screenful at a time. It nor-
mally pauses after each screenful. /usr/bin/more then prints
--More-- and /usr/xpg4/bin/more then prints file at the bot-
tom of the screen. If more is reading from a file rather
than a pipe, the percentage of characters displayed so far
is also shown.
파일의 내용을 한 화면 단위로 봄.
# more file1
----------------------------------------
기능 설명
----------------------------------------
space key 한화면 앞으로
enter key 한라인 앞으로
/찾을문자열 빠른 찾기
q more 화면에서 빠져나옴
----------------------------------------
[EX] more 명령어 실습
# CMD | more
# cat /etc/inetd.conf | more
# more /etc/inetd.conf
haed CMD
__________________
display first few lines of files
The head utility copies the first number of lines of each
filename to the standard output. If no filename is given,
head copies lines from the standard input. The default value
of number is 10 lines.
# head /etc/passwd
# head -5 /etc/passwd
[EX] head 명령어의 활용 예
# ps -ef // -e : every , -f : full list
# ps -ef | more
# ps -ef | grep inetd
# ps -ef | head -1
UID PID PPID C STIME TTY TIME CMD
# ps -ef | head -1 ; ps -ef | grep inetd
UID PID PPID C STIME TTY TIME CMD
root 185 1 0 10:25:16 ? 0:00 /usr/sbin/inetd -s
# alias pps='ps -ef | head -1 ; ps -ef | grep $1'
# pps inetd
tail CMD
__________________
deliver the last part of a file
The tail utility copies the named file to the standard out-
put beginning at a designated place. If no file is named,
the standard input is used.
Copying begins at a point in the file indicated by the
-cnumber, -nnumber, or +number options (if +number is speci-
fied, begins at distance number from the beginning; if
-number is specified, from the end of the input; if number
is NULL, the value 10 is assumed). number is counted in
units of lines or byte according to the -c or -n options,
or lines, blocks, or bytes, according to the appended option
l, b, or c. When no units are specified, counting is by
lines.
# tail /etc/passwd // default view -10 line
# tail -5 /etc/passwd
# tail -f /var/adm/sulog // -f : file moniter view option
< Q & A >
[질문] 파일이 30 라인으로 되어져 있는데 파일 중간의 10라인만 프린터로 출력해 볼수
있나요?
[답변] 다음과 같은 명령어를 통해 가능합니다.
# head -20 filename | tail -10 | lp
[EX1] tail -1
# useradd -m -d /export/home/user02 -s /bin/ksh user02
# passwd user02
# tail -1 /etc/passwd
or
# cat /etc/passwd | grep user02
[EX2] 로그 모니터링
[TERM1] 관리자 윈도우
# tail -f /var/adm/sulog
[TERM2] 사용자 윈도우
# su - user01
$