블로그 이미지
22Hz 22Hz

카테고리

분류 전체보기 (109)
모의해킹 침해대응 전문가 과정 (99)
리눅스 설정 (10)
Total
Today
Yesterday

달력

« » 2024.5
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

공지사항

태그목록

최근에 올라온 글

5일차

==============================안내==============================

파이썬(해커의 언어) + 쉘 스크립트 해두면 좋다 해라

==============================복습==============================

chmod CMD
 심볼릭 모드 # chmod u+x file1
 옥탈 모드 # chmod 744 file1

파일 & 디렉토리 퍼미션 의미 r, w, x

umask의 정의 - default permission의 정의를 변경할 때 쓰는 CMD
특수 퍼미션 SetUID, SetGID, Sticky Bit 의미/설정/관리

프로그램이 실행되는 동안, 게시판처럼 (ex tmp)
 # chmod 4755 file1
 # chmod 2755 file1
 # chmod 1777 file1

사용자와 통신할 때 사용하는 명령어
 talk CMD
 [참고] 서비스 on/off 관리 툴 종료
 (GUI) # system-config-services (# serviceconf)
 (TUI) # ntsysv (# setup)
 (CLI) # chkconfig ntalk on/off

==============================강의==============================

사용자와 통신할 때 사용하는 명령어
 wall CMD

관리자 알아두면 좋은 명령어들
 diff/cmp CMD
  # cmp -s file1 file2 // -s: silent mode
  # diff file1 file2
  # diff -c file1 file2
  # diff --recursive dir1 dir2
 sort CMD
  # cmd | sort -k 3
  # cmd | sort -k 3 -r

  # df -k
  # du -sk /var
  # cd /var ; du -sk * | sort -nr | more
 file CMD

검색 관련 명령어
 grep CMD
  # cmd OPTIONS PATTERN file1
  OPTIONS: -l, -i. -v, -w, --color
  PATTERN: *  .  ^  $  [abc]

  # cmd | grep root
  # cat /etc/passwd | grep root
  # rpm -qa | grep talk
  # ps -ef | grep xinetd
  # chkconfig --list | grep xinetd

  [참고] egrep/fgrep CMD
  # cat /var/log/messages | egrep -i '(warn|err|crit|alert|emerg)'
 find CMD
  # find / -name core -type f
  # find / -user user1 -group class1
  # find / -mtime [-7|7|+7]
  # find / -size [-300c|300c|+300c]
  # find / -perm [-755|755]
  # find / -name core -type f -exec rm -f {} \;

  # find /log -name "*.log" -type f -mtime +30 -exec rm -f {} \;
  # find /log -type f -mtime -2 -size +1G
  # find / -perm -4000 -o -perm -2000
  # find /source -type f -exec grep -l 'Server Error' {} \;

압축과 아카이빙 관련 명령어
 compress CMD
  gzip/gunzip CMD
   # gzip file1
   # gunzip -c file1.gz
   # gunzip file1.gz
  bzip2/bunzip2 CMD
   # bzip2 file1
   # bunzip2 -c file1.bz2
   # bunzip2 file1.bz2
 archive CMD
  tar CMD
   # tar cvf file.tar file1 file2 file3
   # tar tvf file.tar
   # tar xcf file.tar
 compress + archive CMD
  tar CMD
   # tar cvzf file.tar.gz file1 file2 file3
   # tar tvzf file.tar.gz
   # tar xczf file.tar.gz

   # tar cvjf file.tar.bz2 file1 file2 file3
   # tar tvjf file.tar.bz2
   # tar xcjf file.tar.bz2
  jar CMD
   # jar cvf file.jar file1 file2 file3
   # jar tvf file.jar
   # jar xcf file.jar
  zip CMD
   # zip [-r] file.zip file1 file2 file3
   # unzip -l file.zip
   # unzip file.zip

쉘의 특성
 리다이렉션

==============================실습==============================

오른쪽 위에서 업데이트 말풍선 끄는 방법(업데이트 서비스 끄는 방법)
[root@linux220 ~]# chkconfig --list | grep update
yum-updatesd    0:해제  1:해제  2:활성  3:활성  4:활성  5:활성  6:해제
[root@linux220 ~]# who -r
         run-level 5  2016-04-18 16:01                   last=S
[root@linux220 ~]# chkconfig yum-updatesd off
[root@linux220 ~]# chkconfig --list | grep update
yum-updatesd    0:해제  1:해제  2:해제  3:해제  4:해제  5:해제  6:해제
[root@linux220 ~]# service yum-updatesd stop
yum-updatesd를 정지 중:                                    [  OK  ]


wall CMD
# wall "test messages"

[터미널1] user01
[root@linux220 ~]# telnet localhost
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.

    linux220.example.com (Linux release 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:35 EDT 2010) (2)

login: user01
Password:
Last login: Fri Apr 15 19:47:21 from linux220
[user01@linux220 ~]$ id
uid=501(user01) gid=501(user01) groups=501(user01)
[user01@linux220 ~]$ pwd
/home/user01
[user01@linux220 ~]$
Broadcast message from root (pts/3) (Mon Apr 18 16:15:41 2016):

hello hell oh

Broadcast message from root (pts/3) (Mon Apr 18 16:15:41 2016):

hello hell oh

Broadcast message from root (pts/3) (Mon Apr 18 16:16:23 2016):

hell oh!

Broadcast message from root (pts/3) (Mon Apr 18 16:16:23 2016):

hell oh!

Broadcast message from root (Mon Apr 18 16:16:50 2016):

127.0.0.1       localhost       localhost.localdomain
::1                     localhost6      localhost.localdomain6
172.16.6.220    linux220.example.com    linux220

Broadcast message from root (Mon Apr 18 16:16:50 2016):

127.0.0.1       localhost       localhost.localdomain
::1                     localhost6      localhost.localdomain6
172.16.6.220    linux220.example.com    linux220


[터미널2] root
[root@linux220 ~]# wall "hello hell oh!"
bash: !": event not found //이건 왜 안되는걸까??
[root@linux220 ~]# wall "hello hell oh"

Broadcast message from root (pts/3) (Mon Apr 18 16:15:41 2016):

hello hell oh
[root@linux220 ~]# wall
hell oh! //< Ctrl + D >

Broadcast message from root (pts/3) (Mon Apr 18 16:16:23 2016):

hell oh!
[root@linux220 ~]# wall < /etc/hosts

Broadcast message from root (Mon Apr 18 16:16:50 2016):

127.0.0.1       localhost       localhost.localdomain
::1                     localhost6      localhost.localdomain6
172.16.6.220    linux220.example.com    linux220



[실무 예] 긴급한 작업 공지시

긴급한 작업(EX: fsck CMD, dump CMD)
# wall < /etc/MESS/work.txt
..... 10분 .....
# wall < /etc/MESS/work.txt
..... 20분 ......
#





유용한 명령어
비교(cmp, diff), 정렬(sort), 확인(file)

cmp CMD

[root@linux220 ~]# cd /test
[root@linux220 /test]# cat > cmpfile1
1111
2222
3333 <Ctrl + D>
[root@linux220 /test]# cat > cmpfile2
1111
2222
3333 <Ctrl + D>
[root@linux220 /test]# cat > cmpfile3
1111
2222
4444 <Ctrl + D>
[root@linux220 /test]# cmp cmpfile1 cmpfile2
[root@linux220 /test]# cmp cmpfile1 cmpfile3
cmpfile1 cmpfile3 differ: byte 11, line 3


diff CMD

[root@linux220 /test]# diff cmpfile1 cmpfile2
[root@linux220 /test]# diff cmpfile1 cmpfile3
3c3
< 3333
---
> 4444
[root@linux220 /test]# diff -c cmpfile1 cmpfile3
*** cmpfile1    2016-04-18 16:21:53.000000000 +0900
--- cmpfile3    2016-04-18 16:22:10.000000000 +0900
***************
*** 1,3 ****
  1111
  2222
! 3333
--- 1,3 ----
  1111
  2222
! 4444


디렉토리 비교

[root@linux220 /test]# mkdir dir1
[root@linux220 /test]# vi dir1/file1
-------------------------------------------------------------------
1111
-------------------------------------------------------------------
[root@linux220 /test]# mkdir dir2
[root@linux220 /test]# vi dir2/file1
-------------------------------------------------------------------
2222
-------------------------------------------------------------------
[root@linux220 /test]# diff --recursive dir1 dir2
diff --recursive dir1/file1 dir2/file1
1c1
< 1111
---
> 2222
[root@linux220 /test]# vi dir2/file1
-------------------------------------------------------------------
1111 //2222 에서 1111 로 수정
-------------------------------------------------------------------
[root@linux220 /test]# touch dir2/file2
[root@linux220 /test]# diff --recursive dir1 dir2
Only in dir2: file2




[실무 예] 파일 비교

(원 본) -------------------> (백업 본)
/source/* (server.xml) /source/* (server.xml)
# diff server.xml servier.xml.old


[root@linux220 /test]# cp /etc/httpd/conf/httpd.conf /test
[root@linux220 /test]# cp httpd.conf httpd.conf.old
[root@linux220 /test]# vi httpd.conf
-------------------------------------------------------------------
/ServerName -> n
[수정전]
#ServerName www.example.com:80
[수정후]
ServerName 172.16.9.2XX:80
-------------------------------------------------------------------
[root@linux220 /test]# diff httpd.conf httpd.conf.old
265c265
< ServerName 172.16.6.220:80
---
> #ServerName www.example.com:80


[실무 예] 디렉토리 마이그레이션 작업

/was1/* ---- Migration ----> /was2/*
# ls -alR /was1 | wc -l
# ls -alR /was2 | wc -l
# diff --recursive /was1 /was2



sort CMD 정렬

[명령어 형식]
# sort /etc/passwd
# sort –r /etc/passwd   /* -r : reverse */
# sort -k 3 filename   /* -k : key */
# sort -t : -k 3 –n /etc/passwd  /* -t : seperate, -n : numeric */

정열 기준(default)
 오름차순 정열   <---> (-r) 내림차순 정열
 1필드->2필드->3필드-> ... 정열 <---> (-k) 필드 지정 정열
 문자열 정열   <---> (-n) 숫자열 정열


[root@linux220 /test]# vi sortfile1
[root@linux220 /test]# cat sortfile1
linux100 10 20 31 50
linux200 20 25 31 20
linux300 30 20 30 40
linux400 50 20 30 80

[root@linux220 /test]# sort sortfile1

linux100 10 20 31 50
linux200 20 25 31 20
linux300 30 20 30 40
linux400 50 20 30 80
[root@linux220 /test]# sort -r sortfile1
linux400 50 20 30 80
linux300 30 20 30 40
linux200 20 25 31 20
linux100 10 20 31 50

[root@linux220 /test]# sort -k 3 -n sortfile1 //숫자일때 -n 옵션

linux100 10 20 31 50
linux300 30 20 30 40
linux400 50 20 30 80
linux200 20 25 31 20


[root@linux220 /test]# cat /etc/passwd | head
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/etc/news:
[root@linux220 /test]# sort -t : -k 1 /etc/passwd | head
adm:x:3:4:adm:/var/adm:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
avahi-autoipd:x:100:102:avahi-autoipd:/var/lib/avahi-autoipd:/sbin/nologin
avahi:x:70:70:Avahi daemon:/:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
distcache:x:94:94:Distcache:/:/sbin/nologin
dovecot:x:97:97:dovecot:/usr/libexec/dovecot:/sbin/nologin
fedora:x:500:500:fedora:/home/fedora:/bin/bash
[root@linux220 /test]# sort -t : -k 3 -n /etc/passwd | head
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/etc/news:


[EX2] sort 명령어 활용
# CMD | sort
# CMD | sort –r
# CMD | sort –nr
# ps -ef | head | sort
# ps -ef | head | sort –r
# ps -ef | head | sort -k 3
# ps -ef | head | sort -k 3 –r
# df –h | sort -k 4
# df -h | sort -k 4 -r


[root@linux220 /test]# ps -ef | head | sort -k 2
root         1     0  0 15:59 ?        00:00:00 init [5]        
root         2     1  0 15:59 ?        00:00:00 [migration/0]
root         3     1  0 15:59 ?        00:00:00 [ksoftirqd/0]
root         4     1  0 15:59 ?        00:00:00 [watchdog/0]
root         5     1  0 15:59 ?        00:00:00 [migration/1]
root         6     1  0 15:59 ?        00:00:00 [ksoftirqd/1]
root         7     1  0 15:59 ?        00:00:00 [watchdog/1]
root         8     1  0 15:59 ?        00:00:00 [migration/2]
root         9     1  0 15:59 ?        00:00:00 [ksoftirqd/2]
UID        PID  PPID  C STIME TTY          TIME CMD
You have new mail in /var/spool/mail/root
[root@linux220 /test]# ps -ef | head | sort -k 2 -r
UID        PID  PPID  C STIME TTY          TIME CMD
root         9     1  0 15:59 ?        00:00:00 [ksoftirqd/2]
root         8     1  0 15:59 ?        00:00:00 [migration/2]
root         7     1  0 15:59 ?        00:00:00 [watchdog/1]
root         6     1  0 15:59 ?        00:00:00 [ksoftirqd/1]
root         5     1  0 15:59 ?        00:00:00 [migration/1]
root         4     1  0 15:59 ?        00:00:00 [watchdog/0]
root         3     1  0 15:59 ?        00:00:00 [ksoftirqd/0]
root         2     1  0 15:59 ?        00:00:00 [migration/0]
root         1     0  0 15:59 ?        00:00:00 init [5]



[root@linux220 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              36G  3.2G   31G  10% /
/dev/sda8             487M   11M  451M   3% /data1
/dev/sda7             487M   11M  451M   3% /data2
/dev/sda6             487M   11M  451M   3% /data3
/dev/sda5             487M   11M  451M   3% /data4
/dev/sda3             487M   11M  451M   3% /home
tmpfs                 506M     0  506M   0% /dev/shm
/dev/hdc              3.9G  3.9G     0 100% /media/CentOS_5.5_Final
[root@linux220 ~]# du -sk /var //disk usage -s: sum, -k: Kbytes
72720   /var
[root@linux220 ~]# du -sk /etc
132976  /etc


[root@linux220 /var]# du -sk *
12      account
14012   cache
8       crash
28      db
32      empty
16      ftp
8       games
20      gdm
46808   lib
8       local
32      lock
2124    log
4       mail
140     named
8       nis
8       opt
8       preserve
8       racoon
388     run
244     spool
12      tmp
8       tux
8752    www
24      yp
[root@linux220 /var]# du -sk * | sort  //문자열 형태의 정렬방식
12      account
12      tmp
140     named
14012   cache
16      ftp
20      gdm
2124    log
24      yp
244     spool
28      db
32      empty
32      lock
388     run
4       mail
46808   lib
8       crash
8       games
8       local
8       nis
8       opt
8       preserve
8       racoon
8       tux
8752    www
[root@linux220 /var]# du -sk * | sort -n //숫자열 형태의 정렬방식
4       mail
8       crash
8       games
8       local
8       nis
8       opt
8       preserve
8       racoon
8       tux
12      account
12      tmp
16      ftp
20      gdm
24      yp
28      db
32      empty
32      lock
140     named
244     spool
388     run
2124    log
8752    www
14012   cache
46808   lib
[root@linux220 /var]# du -sk * | sort -nr (# du -sk * | sort -nr | more)
46808   lib
14012   cache
8752    www
2124    log
388     run
244     spool
140     named
32      lock
32      empty
28      db
24      yp
20      gdm
16      ftp
12      tmp
12      account
8       tux
8       racoon
8       preserve
8       opt
8       nis
8       local
8       games
8       crash
4       mail




file CMD

[참고] Windos / Unix 계열의 파일 확장자에 대해서
Windows -> 파일에 확장자(예: file.txt, hwp.exe, ....)
Unix,Linux -> 파일의 확장자가 거의 의미가 없다.

이 파일이 어떤 종류의 파일인지 확인하는 명령어가 필요하다!! -> file CMD

[EX1] 파일의 종류 확인
# file /etc/passwd /* ASCII파일 (# cat /etc/passwd) */

[root@linux220 ~]# file /etc/passwd
/etc/passwd: ASCII text

[root@linux220 ~]# file /bin/ls
/bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, stripped

[root@linux220 ~]# file /var/run/utmp
/var/run/utmp: data

[root@linux220 ~]# file /etc/rc.d/init.d/sendmail
/etc/rc.d/init.d/sendmail: Bourne-Again shell script text executable

[root@linux220 ~]# file /etc/hosts /etc
/etc/hosts: ASCII text
/etc:       directory

[root@linux220 ~]# cd /etc
[root@linux220 /etc]# file *
DIR_COLORS:            ASCII English text
DIR_COLORS.xterm:      ASCII English text
Muttrc:                ASCII English text
Muttrc.local:          empty
NetworkManager:        directory
X11:                   directory
a2ps-site.cfg:         ASCII English text
a2ps.cfg:              ASCII English text
acpi:                  directory
adjtime:               ASCII text
alchemist:             directory
aliases:               ASCII English text
aliases.db:            Berkeley DB (Hash, version 8, native byte-order)
alsa:                  directory
alternatives:          directory
anacrontab:            ASCII text
asound.state:          ASCII text, with very long lines
at.deny:               very short file (no magic)
...



검색 관련 명령어

grep CMD: pattern(g/re/p, Globally/Regular Expression/Print)

[명령어 형식]
# grep OPTIONS PATTERN file1

(기본 사용법)
# grep root /etc/passwd (# cat /etc/passwd | grep root)

# CMD | grep root
# cat /etc/passwd | grep root
# rpm -qa | grep talk
# ps -ef | grep xinetd
# chkconfig --list | grep ssh


[root@linux220 /etc]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@linux220 /etc]# cat /etc/passwd | grep root //선호하는 형식
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

[root@linux220 /etc]# rpm -qa | grep talk //rpm: 레드햇 패키지 매니저
talk-server-0.17-31.el5
talk-0.17-29.2.2
[root@linux220 /etc]# ps -ef | grep xinetd
root      3918     1  0 16:02 ?        00:00:00 xinetd -stayalive -pidfile /var/run/xinetd.pid
root     10092  4625  0 17:28 pts/1    00:00:00 grep xinetd
[root@linux220 /etc]# chkconfig --list | grep ssh
sshd            0:해제  1:해제  2:활성  3:활성  4:활성  5:활성  6:해제


(옵션 사용법) "# grep OPTIONS PATTERN file1"
# grep -l root /etc/hosts /etc/passwd /etc/group /* 여러 파일 중 검색 문자열 존재 파일 출력 */
# grep -n root /etc/group    /* -n: number line, 파일에서 root 문자열을 검색하고 라인 번호도 같이 출력 */
# grep -v root /etc/passwd    /* -v: inverse, 파일에서 검색 문자열을 제외하고 나머지 출력 */
# grep -i root /etc/passwd    /* -i: ignore case, 파일에서 검색 문자열의 대소문자를 구분하지 않음 */
# grep -w root file1     /* -w: word , 단어의 일부분이 아닌 단어의 전체가 일치하는 경우 출력 */
# grep --color root /etc/passwd

[root@linux220 /etc]# grep -l root /etc/hosts /etc/passwd /etc/group
/etc/passwd
/etc/group
[root@linux220 /etc]# grep -n root /etc/group
1:root:x:0:root
2:bin:x:1:root,bin,daemon
3:daemon:x:2:root,bin,daemon
4:sys:x:3:root,bin,adm
5:adm:x:4:root,adm,daemon
7:disk:x:6:root
11:wheel:x:10:root
[root@linux220 /etc]# grep -v root /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/etc/news:
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
distcache:x:94:94:Distcache:/:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin
webalizer:x:67:67:Webalizer:/var/www/usage:/sbin/nologin
dovecot:x:97:97:dovecot:/usr/libexec/dovecot:/sbin/nologin
squid:x:23:23::/var/spool/squid:/sbin/nologin
pcap:x:77:77::/var/arpwatch:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
avahi:x:70:70:Avahi daemon:/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
avahi-autoipd:x:100:102:avahi-autoipd:/var/lib/avahi-autoipd:/sbin/nologin
xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin
gdm:x:42:42::/var/gdm:/sbin/nologin
sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin
fedora:x:500:500:fedora:/home/fedora:/bin/bash
user01:x:501:501::/home/user01:/bin/bash
[root@linux220 /etc]# grep -i root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@linux220 /etc]# grep --color root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin


[root@linux220 /etc]# grep -nv nologin /etc/passwd //내가 따로해본것
1:root:x:0:0:root:/root:/bin/bash
6:sync:x:5:0:sync:/sbin:/bin/sync
7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8:halt:x:7:0:halt:/sbin:/sbin/halt
10:news:x:9:13:news:/etc/news:
41:fedora:x:500:500:fedora:/home/fedora:/bin/bash
42:user01:x:501:501::/home/user01:/bin/bash


(패턴 사용법) "# grep OPTIONS PATTERN file1"
* # grep 'ro*t' /etc/passwd //없거나 모든단어
. # grep 'no...y' /etc/passwd //반드시 존재해야하는 모든단어
^root # grep '^root' /etc/passwd //라인의 처음에 root로 시작하는
root$ # grep 'root$' /etc/group //라인의 끝에 root로 끝나는
[abc] # grep 'user0[123]' /etc/passwd //user01 or user02 or user03


[root@linux220 /etc]# grep 'ro*t' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
[root@linux220 /etc]# grep 'no...y' /etc/passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
[root@linux220 /etc]# grep '^root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@linux220 /etc]# grep 'root$' /etc/group
root:x:0:root
disk:x:6:root
wheel:x:10:root
[root@linux220 /etc]# grep 'user0[123]' /etc/passwd
user01:x:501:501::/home/user01:/bin/bash
[root@linux220 /etc]#



grep 명령어 옵션 실습
[root@linux220 ~]# useradd FEDORA
[root@linux220 ~]# passwd FEDORA
Changing password for user FEDORA.
New UNIX password:
BAD PASSWORD: it is based on a dictionary word
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[root@linux220 ~]# grep fedora /etc/passwd
fedora:x:500:500:fedora:/home/fedora:/bin/bash
[root@linux220 ~]# grep -i fedora /etc/passwd //-i: ignore case
fedora:x:500:500:fedora:/home/fedora:/bin/bash
FEDORA:x:502:502::/home/FEDORA:/bin/bash


(실무 예) 환경 파일에 등록
# alias grep='grep -i' => $HOME/.bashrc
# alias grep='grep -i --color' => $HOME/.bashrc < 검색된 단어에 색상을 입혀 가독성을 높여준다 >

# vi ~/.bashrc
alias grep='grep -i --color'
# . ~/.bashrc
# alias grep

그림1



[root@linux220 ~]# grep -l root /etc/group /etc/passwd
/etc/group
/etc/passwd

[root@linux220 ~]# vi file.txt
[root@linux220 ~]# grep -w root file.txt
root hello
[root@linux220 ~]# cat file.txt
root hello
roothello
testroottest
helloroot

[root@linux220 ~]# grep -l "network-function" /etc/rc.d/rc?.d/* //와일드카드문자 ?
/etc/rc.d/rc0.d/K90network
/etc/rc.d/rc1.d/K90network
/etc/rc.d/rc2.d/S10network
/etc/rc.d/rc3.d/S10network
/etc/rc.d/rc4.d/S10network
/etc/rc.d/rc5.d/S10network
/etc/rc.d/rc6.d/K90network
[root@linux220 ~]# cat -n /etc/rc.d/rc5.d/S10network | grep network-function
    42  . ./network-functions

[root@linux220 ~]# cat /var/log/messages
Apr 18 17:07:07 linux220 syslogd 1.4.1: restart.
Apr 18 17:07:58 linux220 avahi-daemon[4117]: Invalid query packet.
Apr 18 17:09:59 linux220 init: Trying to re-exec init
Apr 18 17:35:33 linux220 avahi-daemon[4117]: Invalid query packet.
Apr 18 17:36:34 linux220 avahi-daemon[4117]: Invalid query packet.
Apr 18 17:37:14 linux220 last message repeated 9 times
Apr 18 17:43:28 linux220 kernel: atkbd.c: Unknown key pressed (translated set 2, code 0x72 on isa0060/serio0).
Apr 18 17:43:28 linux220 kernel: atkbd.c: Use 'setkeycodes 72 <keycode>' to make it known.
Apr 18 17:43:28 linux220 kernel: atkbd.c: Unknown key released (translated set 2, code 0x72 on isa0060/serio0).
Apr 18 17:43:28 linux220 kernel: atkbd.c: Use 'setkeycodes 72 <keycode>' to make it known.
Apr 18 17:46:04 linux220 avahi-daemon[4117]: Invalid query packet.
Apr 18 17:46:44 linux220 last message repeated 9 times
[root@linux220 ~]# cat /var/log/messages | grep 'Apr 18 17:3'
Apr 18 17:35:33 linux220 avahi-daemon[4117]: Invalid query packet.
Apr 18 17:36:34 linux220 avahi-daemon[4117]: Invalid query packet.
Apr 18 17:37:14 linux220 last message repeated 9 times

[root@linux220 ~]# ps
  PID TTY          TIME CMD
 4625 pts/1    00:00:00 bash
10193 pts/1    00:00:00 ps
[root@linux220 ~]# ps -ef | grep 4625
root      4625  4620  0 16:10 pts/1    00:00:00 bash
root     10194  4625  0 17:53 pts/1    00:00:00 ps -ef
root     10195  4625  0 17:53 pts/1    00:00:00 grep -i --color 4625
[root@linux220 ~]# ps -ef | grep bash
root      4348  4312  0 16:02 ?        00:00:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients"
root      4625  4620  0 16:10 pts/1    00:00:00 bash
root     10197  4625  0 17:53 pts/1    00:00:00 grep -i --color bash


파일내의 특정 패턴 여러개 검색하기

egrep(Extended grep) CMD
# cat /var/log/messages | egrep -i '(warn|err|crit|alert|emerg)'

fgrep(Fixed grep) CMD
# fgrep '^root' file1

[root@linux220 ~]# egrep "fedora|user01" /etc/passwd
fedora:x:500:500:fedora:/home/fedora:/bin/bash
user01:x:501:501::/home/user01:/bin/bash

(실무 예) 로그 파일에서 에러 메세지 검색
# cat /var/log/messages | egrep –i '(warn|err|crit|alert|emerg)'

(실무) 특정한 단어를 제외하고 검색
# ps –ef | grep xinetd | grep –v grep

[root@linux220 ~]# ps -ef | grep xinetd
root      3918     1  0 16:01 ?        00:00:00 xinetd -stayalive -pidfile /var/run/xinetd.pid
root     10289  4625  0 18:24 pts/1    00:00:00 grep -i --color xinetd
[root@linux220 ~]# ps -ef | grep xinetd | grep -v grep
root      3918     1  0 16:02 ?        00:00:00 xinetd -stayalive -pidfile /var/run/xinetd.pid




find CMD
------------------------------------------------------------------------------
(형식1) # find / -name core -type [f|d] (# find / -name "*oracle*" -type f)
(형식2) # find / -user user01 -group class1
(형식3) # find / -mtime [-7|7|+7]
(형식4) # find / -perm [-755|755]
(형식5) # find / -size [-300M|300M|+300M]
(형식6) # find / -name core -type f -exec rm {} \;
------------------------------------------------------------------------------

[EX1] 파일 이름 검색(예: # find / -name core –type f)

[root@linux220 ~]# cd /test
[root@linux220 /test]# mkdir dir1 dir2
[root@linux220 /test]# touch dir1/file1 dir2/file3 file1 file2
[root@linux220 /test]# tree
.
|-- dir1
|   `-- file1
|-- dir2
|   `-- file3
|-- file1
`-- file2

2 directories, 4 files

[root@linux220 /test]# find . -name file1 //. 현재 폴더 아래에 있는
./dir1/file1
./file1

[root@linux220 /test]# find . -name file1 -type f
./dir1/file1
./file1




[root@linux220 /test]# find /home -user fedora -group fedora
/home/fedora
/home/fedora/dirtest
/home/fedora/dirtest/test2.txt
/home/fedora/.bash_history
/home/fedora/.bash_profile
/home/fedora/.bashrc
/home/fedora/.bash_logout
/home/fedora/.mozilla
/home/fedora/.mozilla/plugins
/home/fedora/.mozilla/extensions
/home/fedora/.viminfo
[root@linux220 /test]# find /home -user fedora -group fedora -ls
102001    2 drwxr-xrwx   4 fedora   fedora       1024  4월 15 19:50 /home/fedora
102009    1 drwxr-xrwx   2 fedora   fedora       1024  4월 15 17:37 /home/fedora/dirtest
102010    0 -rw-rw-r--   1 fedora   fedora          0  4월 15 17:27 /home/fedora/dirtest/test2.txt
102008    2 -rw-------   1 fedora   fedora       1131  4월 15 19:54 /home/fedora/.bash_history
102002    2 -rw-r--r--   1 fedora   fedora        176  1월 22  2009 /home/fedora/.bash_profile
102007    2 -rw-r--r--   1 fedora   fedora        124  1월 22  2009 /home/fedora/.bashrc
102006    2 -rw-r--r--   1 fedora   fedora         33  1월 22  2009 /home/fedora/.bash_logout
102003    2 drwxr-xr-x   4 fedora   fedora       1024  4월 12 03:38 /home/fedora/.mozilla
102004    2 drwxr-xr-x   2 fedora   fedora       1024  2월 18  2010 /home/fedora/.mozilla/plugins
102005    2 drwxr-xr-x   2 fedora   fedora       1024  2월 18  2010 /home/fedora/.mozilla/extensions
102012    1 -rw-------   1 fedora   fedora        779  4월 15 19:50 /home/fedora/.viminfo



# find /usr/share -name "*.log" -type f

/usr/share/texmf-var/web2c/jadetex.log
/usr/share/texmf-var/web2c/updmap.log
/usr/share/texmf-var/web2c/pdflatex.log
..... (중략) .....

[root@linux220 ~]# find /usr/share -name "*.log" -type f
/usr/share/doc/tux-3.2.18/sample.log




[root@linux220 /test/dir2]# touch file1 file2 file3 file4 file5 file6
[root@linux220 /test/dir2]# ls
file1  file2  file3  file4  file5  file6
[root@linux220 /test/dir2]# ls -l file*
-rw-r--r-- 1 root root 0  4월 18 18:39 file1
-rw-r--r-- 1 root root 0  4월 18 18:39 file2
-rw-r--r-- 1 root root 0  4월 18 18:39 file3
-rw-r--r-- 1 root root 0  4월 18 18:39 file4
-rw-r--r-- 1 root root 0  4월 18 18:39 file5
-rw-r--r-- 1 root root 0  4월 18 18:39 file6
[root@linux220 /test/dir2]# date
2016. 04. 18. (월) 18:40:14 KST
[root@linux220 /test/dir2]# touch -t 04171839 file2
[root@linux220 /test/dir2]# touch -t 04161839 file3
[root@linux220 /test/dir2]# touch -t 04151839 file4
[root@linux220 /test/dir2]# touch -t 04141839 file5
[root@linux220 /test/dir2]# touch -t 04131839 file6
[root@linux220 /test/dir2]# ls -l file*
-rw-r--r-- 1 root root 0  4월 18 18:39 file1
-rw-r--r-- 1 root root 0  4월 17 18:39 file2
-rw-r--r-- 1 root root 0  4월 16 18:39 file3
-rw-r--r-- 1 root root 0  4월 15 18:39 file4
-rw-r--r-- 1 root root 0  4월 14 18:39 file5
-rw-r--r-- 1 root root 0  4월 13 18:39 file6

[root@linux220 /test/dir2]# find . -mtime 3 -type f //3일전
./file4
[root@linux220 /test/dir2]# find . -mtime +3 -type f //4일전~
./file6
./file5
[root@linux220 /test/dir2]# find . -mtime -3 -type f //~2일전
./file3
./file2
./file1



[root@linux220 /test/dir2]# cd /test
[root@linux220 /test]# mkdir dir3
[root@linux220 /test]# cd dir3
[root@linux220 /test/dir3]# touch file{1,2,3,4,5,6,7,8}
[root@linux220 /test/dir3]# ll
합계 0
-rw-r--r-- 1 root root 0  4월 18 18:43 file1
-rw-r--r-- 1 root root 0  4월 18 18:43 file2
-rw-r--r-- 1 root root 0  4월 18 18:43 file3
-rw-r--r-- 1 root root 0  4월 18 18:43 file4
-rw-r--r-- 1 root root 0  4월 18 18:43 file5
-rw-r--r-- 1 root root 0  4월 18 18:43 file6
-rw-r--r-- 1 root root 0  4월 18 18:43 file7
-rw-r--r-- 1 root root 0  4월 18 18:43 file8
[root@linux220 /test/dir3]# chmod 000 file1
[root@linux220 /test/dir3]# chmod 100 file2
[root@linux220 /test/dir3]# chmod 200 file3
[root@linux220 /test/dir3]# chmod 300 file4
[root@linux220 /test/dir3]# chmod 400 file5
[root@linux220 /test/dir3]# chmod 500 file6
[root@linux220 /test/dir3]# chmod 600 file7
[root@linux220 /test/dir3]# chmod 700 file8
[root@linux220 /test/dir3]# ll
합계 0
---------- 1 root root 0  4월 18 18:43 file1
---x------ 1 root root 0  4월 18 18:43 file2
--w------- 1 root root 0  4월 18 18:43 file3
--wx------ 1 root root 0  4월 18 18:43 file4
-r-------- 1 root root 0  4월 18 18:43 file5
-r-x------ 1 root root 0  4월 18 18:43 file6
-rw------- 1 root root 0  4월 18 18:43 file7
-rwx------ 1 root root 0  4월 18 18:43 file8
[root@linux220 /test/dir3]# find . -perm 600 -type f -ls //최소한 600 퍼미션인 파일들 검색
2387491    0 -rw-------   1 root     root            0  4월 18 18:43 ./file7
[root@linux220 /test/dir3]# find . -perm -400 -type f -ls
2387492    0 -rwx------   1 root     root            0  4월 18 18:43 ./file8
2387491    0 -rw-------   1 root     root            0  4월 18 18:43 ./file7
2387490    0 -r-x------   1 root     root            0  4월 18 18:43 ./file6
2387489    0 -r--------   1 root     root            0  4월 18 18:43 ./file5

[root@linux220 /test/dir3]# cp /etc/passwd file10
[root@linux220 /test/dir3]# cp /etc/services file11
[root@linux220 /test/dir3]# ll
합계 364K
---------- 1 root root    0  4월 18 18:43 file1
-rw-r--r-- 1 root root 2.0K  4월 18 18:46 file10
-rw-r--r-- 1 root root 354K  4월 18 18:47 file11
---x------ 1 root root    0  4월 18 18:43 file2
--w------- 1 root root    0  4월 18 18:43 file3
--wx------ 1 root root    0  4월 18 18:43 file4
-r-------- 1 root root    0  4월 18 18:43 file5
-r-x------ 1 root root    0  4월 18 18:43 file6
-rw------- 1 root root    0  4월 18 18:43 file7
-rwx------ 1 root root    0  4월 18 18:43 file8
[root@linux220 /test/dir3]# unalias ls
[root@linux220 /test/dir3]# ll (# ls -l)
합계 364
---------- 1 root root      0  4월 18 18:43 file1
-rw-r--r-- 1 root root   1986  4월 18 18:46 file10
-rw-r--r-- 1 root root 362031  4월 18 18:47 file11
---x------ 1 root root      0  4월 18 18:43 file2
--w------- 1 root root      0  4월 18 18:43 file3
--wx------ 1 root root      0  4월 18 18:43 file4
-r-------- 1 root root      0  4월 18 18:43 file5
-r-x------ 1 root root      0  4월 18 18:43 file6
-rw------- 1 root root      0  4월 18 18:43 file7
-rwx------ 1 root root      0  4월 18 18:43 file8

[root@linux220 /test/dir3]# find . -size 1986c -type f  //size=1986
./file10
[root@linux220 /test/dir3]# find . -size 362031c -type f //size=362031
./file11
[root@linux220 /test/dir3]# find . -size -1986c -type f  //size<1986
./file3
./file8
./file2
./file7
./file4
./file1
./file6
./file5
[root@linux220 /test/dir3]# find . -size +1986c -type f  //size>1986
./file11



[root@linux220 /test]# cd /test
[root@linux220 /test]# find . -name file1 -type f
./dir3/file1
./dir1/file1
./file1
./dir2/file1
[root@linux220 /test]# find . -name file1 -type f -ls
2387485    0 ----------   1 root     root            0  4월 18 18:43 ./dir3/file1
2387475    0 -rw-r--r--   1 root     root            0  4월 18 18:34 ./dir1/file1
2387477    0 -rw-r--r--   1 root     root            0  4월 18 18:34 ./file1
2387479    0 -rw-r--r--   1 root     root            0  4월 18 18:39 ./dir2/file1
[root@linux220 /test]# find . -name file1 -type f -exec chown user01 {} \;
[root@linux220 /test]# find . -name file1 -type f -ls
2387485    0 ----------   1 user01   root            0  4월 18 18:43 ./dir3/file1
2387475    0 -rw-r--r--   1 user01   root            0  4월 18 18:34 ./dir1/file1
2387477    0 -rw-r--r--   1 user01   root            0  4월 18 18:34 ./file1
2387479    0 -rw-r--r--   1 user01   root            0  4월 18 18:39 ./dir2/file1
[root@linux220 /test]# find . -name file1 -type f -exec chmod 640 {} \;
[root@linux220 /test]# find . -name file1 -type f -ls
2387485    0 -rw-r-----   1 user01   root            0  4월 18 18:43 ./dir3/file1
2387475    0 -rw-r-----   1 user01   root            0  4월 18 18:34 ./dir1/file1
2387477    0 -rw-r-----   1 user01   root            0  4월 18 18:34 ./file1
2387479    0 -rw-r-----   1 user01   root            0  4월 18 18:39 ./dir2/file1
[root@linux220 /test]# find . -name file1 -type f -exec rm -f {} \;
[root@linux220 /test]# find . -name file1 -type f



(실무 예) 오래된 로그 기록 삭제
한달(시스템 생성일 ~ 30일)이 지난 로그파일은 그 의미를 상실하게 된다. 따라서 일정 시간이 지난 로그파일의 경
우 find라는 명령어를 이용하여 파일을 주기적으로 삭제해 주도록 한다.

# find /Log_dir1 -name "*.log" -type f -mtime +30 -exec rm –f {} \;
# find /Log_dir2 -name "*.log" -type f -mtime +60 -exec rm –f {} \;


(실무 예) 파일시스템이 갑자기 풀(Full) 나는 경우

# find /var -mtime -2 -size +1G -type f
# find /var -mtime -2 -size +512M -type f
# find /var -mtime -2 -size +256M -type f
# find /var -mtime -2 -size +128M -type f
/var/server/log/file.log

 [참고] lsof(list open file)
 # lsof | grep /var/server/log/file.log


(실무 예) 에러메세지가 들어 있는 startup script 검색

# /was/bin/startup.sh
..... Server Error .....
# find /was –type f –exec grep –l 'Server Error' {} \;
/was/conf/server.xml
# vi /was/conf/server.xml
/Server Error
......
if 조건 ; then
else
 echo "Server Error"
fi


(실무 예) 부팅시에 에러메세지 제어
부팅 중간에 .... Server Error .....

# find /etc/rc?.d/* -type f –exec grep –l 'Server Error' {} \;
/etc/rc5.d/init.d/S90network
# vi /etc/rc5.d/init.d/S90netwok
/Server Error


(실무 예) 에러 메세지를 검색하는 방법
http://www.google.co.kr
-> site:.redhat.com "Server Error"
-> 가상화 .pdf
-> 가상화 .ppt
-> "Server Error1" AND "Server Error2" (AND/OR)
-> "Server Error1"



[root@linux220 ~]# find . -type f -exec grep -l 'user01' {} \;
./.bash_history
./mbox
./user.list



# find / -mtime -10 -a mtime +2 //3일전부터 9일전까지 -a:and -o:or

[root@linux220 ~]# find /etc -type f -exec grep -l "PASS*" {} \;
/etc/sudoers
/etc/profile.d/gnome-ssh-askpass.sh
/etc/profile.d/gnome-ssh-askpass.csh
/etc/squid/squid.conf.default
/etc/squid/squid.conf
/etc/Muttrc
/etc/services
/etc/sysconfig/hsqldb
/etc/sysconfig/authconfig
/etc/udev/rules.d/60-libsane.rules
/etc/vmware-tools/installer.sh
/etc/dbus-1/system.d/wpa_supplicant.conf
/etc/login.defs
/etc/rc.d/init.d/hsqldb
/etc/rc.d/init.d/functions
/etc/libuser.conf
/etc/termcap
/etc/gdm/XKeepsCrashing




gzip / gunzip

[명령어의 형식]
(압축) # gzip file1 (file1.gz)
(확인) # gunzip -c file1.gz (# zcat file1.gz)
(해제) # gunzip file1.gz (# gzip -d file1.gz)

[root@linux220 ~]# cd /test
[root@linux220 /test]# rm -rf /test/*
[root@linux220 /test]# cp /etc/services file1
[root@linux220 /test]# cp file1 file2
[root@linux220 /test]# cp file1 file3
[root@linux220 /test]# cp file1 file4
[root@linux220 /test]# ls
file1  file2  file3  file4
[root@linux220 /test]# gzip file1
[root@linux220 /test]# ls -l
합계 1184
-rw-r--r-- 1 root root  98859  4월 18 20:24 file1.gz
-rw-r--r-- 1 root root 362031  4월 18 20:24 file2
-rw-r--r-- 1 root root 362031  4월 18 20:24 file3
-rw-r--r-- 1 root root 362031  4월 18 20:25 file4

[root@linux220 /test]# gzip -9v file2
file2:   72.6% -- replaced with file2.gz
[root@linux220 /test]# ll
합계 928
-rw-r--r-- 1 root root  98859  4월 18 20:24 file1.gz
-rw-r--r-- 1 root root  99285  4월 18 20:24 file2.gz
-rw-r--r-- 1 root root 362031  4월 18 20:24 file3
-rw-r--r-- 1 root root 362031  4월 18 20:25 file4

[root@linux220 /test]# gzip -1v file3
file3:   69.4% -- replaced with file3.gz
[root@linux220 /test]# ll
합계 684
-rw-r--r-- 1 root root  98859  4월 18 20:24 file1.gz
-rw-r--r-- 1 root root  99285  4월 18 20:24 file2.gz
-rw-r--r-- 1 root root 110855  4월 18 20:24 file3.gz
-rw-r--r-- 1 root root 362031  4월 18 20:25 file4



[root@linux220 /test]# cp -r /var/lib dir
ls
[root@linux220 /test]# ls
dir  file1.gz  file2.gz  file3.gz  file4
[root@linux220 /test]# du -sh dir
44M     dir
[root@linux220 /test]# gzip -r dir
gzip: dir/hsqldb/lib/servlet.jar is not a directory or a regular file - ignored
gzip: dir/hsqldb/lib/hsqldb.jar is not a directory or a regular file - ignored
[root@linux220 /test]# du -sh dir
16M     dir

[root@linux220 /test]# gunzip -r dir
gunzip: dir/hsqldb/lib/servlet.jar is not a directory or a regular file - ignored
gunzip: dir/hsqldb/lib/hsqldb.jar is not a directory or a regular file - ignored
[root@linux220 /test]# du -sh dir
48M     dir



여러개의 파일을 하나로 압축할 수 있을까?

[root@linux220 /test]# cp /etc/services file1
[root@linux220 /test]# cp file1 file2
[root@linux220 /test]# cp file1 file3
[root@linux220 /test]# ls
file1  file2  file3
[root@linux220 /test]# gzip file*
[root@linux220 /test]# ls
file1.gz  file2.gz  file3.gz
[root@linux220 /test]# gunzip file*
[root@linux220 /test]# ls
file1  file2  file3



tar - Tape ARchive

tar(tape archive) 명령어는 여러개의 파일이나 디렉토리를 한 개의 묶음파일로 만들 때 사용한다.
또는 Tape 디바이스에 백업할 때 사용한다. tar 명령어는 c, x, v, f 옵션들을 사용할 수 있다.
한 개의 아카이빙 할 때는 c 옵션을 사용하고 f 옵션으로 만들어질 파일의 이름을 지정하면 된다.
아카이빙 파일의 내용을 볼때는 t 옵션을 사용하고 f 옵션으로 아카이빙 파일을 지정한다.
아카이빙 파일을 해제 할 때는 x 옵션을 사용하고 f 옵션으로 아카이빙파일을 지정하면 된다.

[명령어 형식] tar명령어 사용시 옵션에 -기호 생략가능
# tar cvf <묶음파일명> <파일명1> <파일명2> ... /* c: create, v: verbose, f: file or tape */
# tar tvf <묶음파일명>    /* t: content */
# tar xvf <묶음파일명>    /* x: extract */

-c 여러 개의 파일을 하나의 파일로 묶을 때
-v 생성과정을 보여 줌
-x 파일의 압축을 해제하고 풀어 낼 때
-r 파일 및 디렉토리 추가
-u tar 파일과 새로운 디렉토리 내의 파일과 비교하여 최근 파일을 아카이브에 추가
-t tar 파일의 내용을 확인할 때
-z gzip과 관련하여 압축이나 해제를 한꺼번에 할 때
-j, -l bzip2를 이용해서 압축한다.

# tar cvf file.tar file1 file2 file3
# tar tvf file.tar
# tar xvf file.tar

# tar cvzf file.tar.gz file1 file2 file3 (# tar cvzf file.tgz file1 file2 file3)
# tar tvzf file.tar.gz
# tar xvzf file.tar.gz    (# tar xvf file.tar.gz)

# tar cvjf file.tar.bz2 file1 file2 file3 (# tar cvjf file.tbz file1 file2 file3)
# tar tvjf file.tar.bz2
# tar xvjf file.tar.bz2    (# tar xvf file.tar.bz2)




[root@linux220 /test]# ll
합계 1080
-rw-r--r-- 1 root root 362031  4월 18 20:34 file1
-rw-r--r-- 1 root root 362031  4월 18 20:34 file2
-rw-r--r-- 1 root root 362031  4월 18 20:34 file3
[root@linux220 /test]# tar cvf file.tar file1 file2 file3
file1
file2
file3
[root@linux220 /test]# ls -l file*
-rw-r--r-- 1 root root 1095680  4월 18 20:59 file.tar
-rw-r--r-- 1 root root  362031  4월 18 20:34 file1
-rw-r--r-- 1 root root  362031  4월 18 20:34 file2
-rw-r--r-- 1 root root  362031  4월 18 20:34 file3
[root@linux220 /test]# rm -f file1 file2 file3
[root@linux220 /test]# ls
file.tar
[root@linux220 /test]# tar tvf file.tar
-rw-r--r-- root/root    362031 2016-04-18 20:34:41 file1
-rw-r--r-- root/root    362031 2016-04-18 20:34:43 file2
-rw-r--r-- root/root    362031 2016-04-18 20:34:45 file3
[root@linux220 /test]# tar xvf file.tar
file1
file2
file3
[root@linux220 /test]# ls file*
file.tar  file1  file2  file3



[root@linux220 /test]# ls
file1  file2  file3
[root@linux220 /test]# tar cvf file.tar file1 file2 file3
file1
file2
file3
[root@linux220 /test]# gzip file.tar
[root@linux220 /test]# ls
file.tar.gz  file1  file2  file3
[root@linux220 /test]# gunzip file.tar.gz (# gzip -d file.tar.gz)
[root@linux220 /test]# ls
file.tar  file1  file2  file3

[root@linux220 /test]# rm -f file?
[root@linux220 /test]# ls
file.tar
[root@linux220 /test]# tar xvf file.tar
file1
file2
file3
[root@linux220 /test]# ls
file.tar  file1  file2  file3

[root@linux220 /test]# rm -f file.tar
[root@linux220 /test]# ls
file1  file2  file3
[root@linux220 /test]# tar cvzf file.tar.gz file1 file2 file3
file1
file2
file3
[root@linux220 /test]# file file.tar.gz
file.tar.gz: gzip compressed data, from Unix, last modified: Mon Apr 18 21:07:36 2016
[root@linux220 /test]# rm -f file?
[root@linux220 /test]# ls
file.tar.gz

[root@linux220 /test]# tar xvzf file.tar.gz
file1
file2
file3
[root@linux220 /test]# rm -f file.tar.gz
[root@linux220 /test]# ls
file1  file2  file3
[root@linux220 /test]# tar cvjf filr.tar.bz2 file1 file2 file3
file1
file2
file3
[root@linux220 /test]# ls
file1  file2  file3  filr.tar.bz2
[root@linux220 /test]# file filr.tar.bz2
filr.tar.bz2: bzip2 compressed data, block size = 900k
[root@linux220 /test]# tar tvjf filr.tar.bz2
-rw-r--r-- root/root    362031 2016-04-18 20:34:41 file1
-rw-r--r-- root/root    362031 2016-04-18 20:34:43 file2
-rw-r--r-- root/root    362031 2016-04-18 20:34:45 file3
[root@linux220 /test]# rm -f file?
[root@linux220 /test]# ls
filr.tar.bz2
[root@linux220 /test]# tar xvjf filr.tar.bz2
file1
file2
file3
[root@linux220 /test]# ls
file1  file2  file3  filr.tar.bz2
[root@linux220 /test]# rm -f filr.tar.bz2

[참고] jar CMD
 [jar 명령어 형식] JDK/SDK 설치시 존재
 # jar cvf file.jar file1 file2 file3
 # jar tvf file.jar
 # jar xcf file.jar

tar는 아카이빙, jar는 아카이빙+컴프레스 동시에


[zip 명령어 형식] 윈도우와 호환 가능 (같은 알고리즘을 사용하기 때문)
# zip file.zip file1 file2 file3
# unzip -l file.zip
# unzip file.zip
[참고] 알집, 빵집, Winzip, ...

[root@linux220 /test]# ls
file1  file2  file3
[root@linux220 /test]# zip file.zip file1 file2 file3
  adding: file1 (deflated 73%)
  adding: file2 (deflated 73%)
  adding: file3 (deflated 73%)
[root@linux220 /test]# ls -l
합계 1376
-rw-r--r-- 1 root root 296887  4월 18 21:16 file.zip
-rw-r--r-- 1 root root 362031  4월 18 20:34 file1
-rw-r--r-- 1 root root 362031  4월 18 20:34 file2
-rw-r--r-- 1 root root 362031  4월 18 20:34 file3
[root@linux220 /test]# unzip -l file.zip
Archive:  file.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
   362031  04-18-16 20:34   file1
   362031  04-18-16 20:34   file2
   362031  04-18-16 20:34   file3
 --------                   -------
  1086093                   3 files
[root@linux220 /test]# rm -rf file?
[root@linux220 /test]# unzip file.zip
Archive:  file.zip
  inflating: file1                  
  inflating: file2                  
  inflating: file3                  
[root@linux220 /test]# ls
file.zip  file1  file2  file3




(실무 예) 인터넷상에 받은 파일 압축 해제 방법

file.gz  ---- gzip -----> # gunzip file.gz  (# gzip -d file.gz)
file.bz2 ---- bzip2 ----> # bunzip2 file.bz2  (# bzip2 -d file.bz2)
file.tar.gz ---- tar/gzip -> # tar xvzf file.tar.gz  (# tar xvf file.tar.gz)
file.tgz
file.tar.bz2 ---- tar/bzip2-> # tar xvjf file.tar.bz2 (# tar xvf file.tar.bz2)
file.tbz
file.zip ---- zip ------> # unzip file.zip
file.jar ---- jar ------> # jar xvf file.jar


(실무 예) file.tar.gz

# tar xvzf file.tar.gz
# gzip –d file.tar.gz



13장 쉘의 특성

쉘은 명령어해석기다 내가 타이핑한 명령어를 해석해서 커널로 보내준다

리다이렉션(Redirection)
파이프(Pipe)
쉘 기능(Shell Function)
변수(Variable)
히스토리(History)
환경파일(Environment Files) 가장 중요!! .bashrc




리다이렉션(방향 재지정)

fd (파일 기술자, File Descriptor)

파일 기술자(File Description)란?
 프로세스가 파일을 열때 할당되는 번호
 프로세스의 열린 파일을 구분할 때 사용하는 식별 번호

[참고] C 언어
# vi test.c
------------------------
int fd;
fd=open(.....);
------------------------

파일을 열면 버퍼공간에 임시파일을 만들어서 작업하게 된다
동시에 파일을 열어서 어떤유저는 내용을 추가, 수정, 삭제를 한다
운영체제가 여러명이 연 파일을 구분할 수 있어야 한다 그때 부여되는게 fd다

예약되어진 파일 기술자(File Descriptor)
0 stdin  스탠다드인풋 표준입력 기본값이면 키보드로 입력받는다
1 stdout 스탠다드아웃풋 표준출력 기본값이면 모니터로 출력된다
2 stderr 스탠다드에러 표준에러 기본값이면 모니터로 출력된다


[형식]
CMD  < filename (표준입력)
CMD 0< filename (표준입력 - 파일 기술자 0번)

출력 재지정
CMD  >  filename //표준출력, 덮어쓰기 기능
CMD 1>  filename //표준출력, 덮어쓰기 기능
CMD  >> filename //표준출력, 이어쓰기 기능
CMD 1>> filename //표준출력, 이어쓰기 기능

[root@linux220 /test]# echo 1111
1111
[root@linux220 /test]# echo 1111 > filename1
[root@linux220 /test]# cat filename1
1111
[root@linux220 /test]# echo 2222 > filename1 //덮어쓰기
[root@linux220 /test]# cat filename1
2222
[root@linux220 /test]# echo 1111 >> filename1 //이어쓰기
[root@linux220 /test]# cat filename1
2222
1111

에러 재지정
CMD 2>  filename //표준에러출력, 덮어쓰기, 2 생략불가!!
CMD 2>> filename //표준에러출력, 이어쓰기, 2 생략불가!!


[root@linux220 /test]# touch file1 file2 file3
[root@linux220 /test]# ls -l /test /nodir
ls: /nodir: 그런 파일이나 디렉토리가 없음   //에러출력
/test:        //정상출력
합계 0
-rw-r--r-- 1 root root 0  4월 18 22:04 file1
-rw-r--r-- 1 root root 0  4월 18 22:04 file2
-rw-r--r-- 1 root root 0  4월 18 22:04 file3
[root@linux220 /test]# ls -l /test /nodir > dirfilename1 //1이 생략, 정상적인 출력결과만 dirfilename1에 입력
ls: /nodir: 그런 파일이나 디렉토리가 없음   //에러출력만 나옴
[root@linux220 /test]# cat dirfilename1
/test:
합계 0
-rw-r--r-- 1 root root 0  4월 18 22:05 dirfilename1
-rw-r--r-- 1 root root 0  4월 18 22:04 file1
-rw-r--r-- 1 root root 0  4월 18 22:04 file2
-rw-r--r-- 1 root root 0  4월 18 22:04 file3
[root@linux220 /test]# ls -l /test /nodir 2> dirfilename2 //2, 에러출력결과만 dirfilename2에 입력
/test:
합계 8
-rw-r--r-- 1 root root 207  4월 18 22:05 dirfilename1
-rw-r--r-- 1 root root  55  4월 18 22:07 dirfilename2
-rw-r--r-- 1 root root   0  4월 18 22:04 file1
-rw-r--r-- 1 root root   0  4월 18 22:04 file2
-rw-r--r-- 1 root root   0  4월 18 22:04 file3
[root@linux220 /test]# cat dirfilename2
ls: /nodir: 그런 파일이나 디렉토리가 없음

[root@linux220 /test]# ls /test /nodir > dirfilename1 2> dirfilename2
[root@linux220 /test]# cat dirfilename1
/test:
dirfilename1
dirfilename2
file1
file2
file3
[root@linux220 /test]# cat dirfilename2
ls: /nodir: 그런 파일이나 디렉토리가 없음


[root@linux220 /test]# ls /test /nodir > dirfilename 2>&1 //에러메시지를 1> 의 입력으로 다시 보낸다
[root@linux220 /test]# cat dirfilename
ls: /nodir: 그런 파일이나 디렉토리가 없음
/test:
dirfilename
dirfilename1
dirfilename2
file1
file2
file3



쉘 스크립트는 명령어의 집합형태


clear.sh

cd /test
rm -rf /test/*
cp -p /etc/passwd file1
cp file1 file2
cp file2 file3



(실무 예) script.sh(스크립트) 로그 파일 생성
(X) # ./script.sh > file.log
(0) # ./script.sh > file.log 2>&1

(실무 예) ./configure —prefix=/usr/local/apache2
# ./configure --perfix=/usr/local/apache2 2> file.log
# ./configure --perfix=/usr/local/apache2 > file.log 2>&1

(실무 예) 일반 사용자(EX: oracle, wasuser)가 검색하는 경우
$ find / -name core –type f
$ find / -name core –type f 2>/dev/null //에러메시지를 /dev/null 로 보내라 에러메시지 보기싫으니까


'모의해킹 침해대응 전문가 과정' 카테고리의 다른 글

20160420 리눅스 기초  (0) 2016.04.21
20160419 리눅스 기초  (0) 2016.04.20
20160415 리눅스 기초  (0) 2016.04.15
20160414 리눅스 기초  (0) 2016.04.15
20160412 리눅스 기초  (0) 2016.04.13
Posted by 22Hz
, |

최근에 달린 댓글

최근에 받은 트랙백

글 보관함