서버/Linux I 2015. 12. 23. 15:29

Linux I - 06. 리눅스 기본 명령어 (기타 관리 및 검색 명령어)

 

 

 

[root@CentOS ~]# echo $PS1
[\u@\h \W]\$


[root@CentOS ~]# PS1="[\u@\h \$PWD]# "
[root@CentOS /root]# cd ..
[root@CentOS /]#

 

 

 

 

1. 기타 관리 명령어

 

@ 'wc' 명령어

 

Ex1) 'wc -l', 'wc -w', 'wc -c' 옵션 명령어

 

[root@CentOS /]# mkdir test
[root@CentOS /]# cd test

[root@CentOS /test]# wc /etc/passwd
  39   58 1816 /etc/passwd

 

[root@CentOS /test]# wc -l /etc/passwd (라인 개수)
39 /etc/passwd

 

[root@CentOS /test]# wc -w /etc/passwd (단어 개수)
58 /etc/passwd

 

[root@CentOS /test]# wc -c /etc/passwd (문자 개수)
1816 /etc/passwd

 


Ex2) 시스템 사용자 수 확인

 

[root@CentOS /test]# cat /etc/passwd
[root@CentOS /test]# cat /etc/passwd | wc -l
39

 

[root@CentOS /test]# wc -l /etc/passwd
39 /etc/passwd

 


Ex3) 실행중인 프로세스 개수 확인

 

[root@CentOS /test]# ps -ef


[root@CentOS /test]# ps -ef | wc -l
236

 


Ex4) 설치된 패케지 개수 확인

 

[root@CentOS /test]# rpm -qa


[root@CentOS /test]# rpm -qa | wc -l
1119

 


Ex5) 시스템 성능 및 사용량 카운트 수집

 

[root@CentOS /test]# service httpd restart
httpd 를 정지 중:                                          [  OK  ]
httpd (을)를 시작 중: httpd: apr_sockaddr_info_get() failed for CentOS
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

 

[root@CentOS /test]# ps -ef | grep httpd
root     24849     1  0 12:28 ?        00:00:00 /usr/sbin/httpd
apache   24853 24849  0 12:28 ?        00:00:00 /usr/sbin/httpd
apache   24854 24849  0 12:28 ?        00:00:00 /usr/sbin/httpd
apache   24855 24849  0 12:28 ?        00:00:00 /usr/sbin/httpd
apache   24856 24849  0 12:28 ?        00:00:00 /usr/sbin/httpd
apache   24857 24849  0 12:28 ?        00:00:00 /usr/sbin/httpd
apache   24858 24849  0 12:28 ?        00:00:00 /usr/sbin/httpd
apache   24859 24849  0 12:28 ?        00:00:00 /usr/sbin/httpd
apache   24860 24849  0 12:28 ?        00:00:00 /usr/sbin/httpd
root     25336 16641  0 13:31 pts/1    00:00:00 grep httpd

 

[root@CentOS /test]# ps -ef | grep httpd | wc -l

[root@CentOS /test]# ps -ef | grep httpd | wc -l > apache.count
[root@CentOS /test]# cat apache.count
10

 


Ex6) 디스크 사용량 모니터링

 

[root@CentOS /test]# df -k
Filesystem           1K-blocks    Used Available Use% Mounted on
/dev/mapper/vg_centos-lv_root
                      18003272 4024280  13057804  24% /
tmpfs                   506144     228    505916   1% /dev/shm
/dev/sda1               487652   28703    433349   7% /boot
/dev/sr0               4523182 4523182         0 100% /media/CentOS_6.6_Final

 

[root@CentOS /test]# df -k /dev/sda1 | tail -1
/dev/sda1         487652 28703    433349   7% /boot

 

[root@CentOS /test]# df -k /dev/sda1 | tail -1 | awk '{print $5}'
7%

 

[root@CentOS /test]# df -k /dev/sda1 | tail -1 | awk '{print $5}' > df.count
[root@CentOS /test]# cat df.count
7%

 

 


@ 'su' 명령어

 

 

su user1   : 'user1'으로 사용자로 전환되지만, 이전 사용자의 환경 변수 설정이 계승된다.

su - user1 : 새로 로그인한것 처럼 'user1'으로 전환되며, 이전 사용자의 환경 변수 설정이 계승되지 않는다. (권장 사항)

 

Ex1) su user1

 

[root@CentOS /test]# su user1


[user1@CentOS test]$ id
uid=500(user1) gid=500(user1) groups=500(user1)

 

[user1@CentOS test]$ pwd
/test

 

[user1@CentOS test]$ echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

 

[user1@CentOS test]$ exit

[root@CentOS /test]#

 


Ex2) su - user1

 

[root@CentOS /test]# su - user1


[user1@CentOS ~]$ id
uid=500(user1) gid=500(user1) groups=500(user1)

 

[user1@CentOS ~]$ pwd
/home/user1

 

[user1@CentOS ~]$ echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/user1/bin


[user1@CentOS ~]$ exit
logout


[root@CentOS /test]#

 

 

Ex3) 'user1' 사용자가 'root'나 다른 사용자로 접속할 경우

 

[root@CentOS /test]# ssh user1@localhost
The authenticity of host 'localhost (::1)' can't be established.
RSA key fingerprint is e0:13:4f:de:b3:a3:46:f6:0e:7c:60:f2:af:03:33:ee.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
user1@localhost's password:
Last login: Wed Dec 23 13:43:38 2015 from localhost
[user1@CentOS ~]$
[user1@CentOS ~]$ pwd
/home/user1

 

[user1@CentOS ~]$ cat /etc/shadow
cat: /etc/shadow: 허가 거부

 

[user1@CentOS ~]$ ls -l /etc/shadow
----------. 1 root root 1663 2015-12-22 11:10 /etc/shadow

 

[user1@CentOS ~]$ su - root
암호:

 

[root@CentOS ~]# pwd
/root

 

[root@CentOS ~]# id
uid=0(root) gid=0(root) groups=0(root)

 

[root@CentOS ~]# cat /etc/shadow

 

[root@CentOS ~]# exit
logout


[user1@CentOS ~]$ exit
logout


Connection to localhost closed.
[root@CentOS /test]#

 

 


@ 'id' 명령어 & 'groups' 명령어

 

Ex1) 'id' 명령어

 

[root@CentOS /test]# id
uid=0(root) gid=0(root) groups=0(root)

 

 

Ex2) 'group' 명령어

 

[root@CentOS /test]# groups
root

 

[root@CentOS /test]# groups root
root : root

 

[root@CentOS /test]# groups root user1
root : root
user1 : user1

 

[root@CentOS /test]# cat /etc/group

 

 


@ 'last' 명령어

 

Ex) 'last' 옵션 명령어

 

[root@CentOS /test]# last               마지막 로그인 전체 정보를 보여줌
[root@CentOS /test]# last root         'root' 계정 마지막 로그인 정보를 보여줌
[root@CentOS /test]# last -5           지정한 라인개수 만큼 로그인 정보를 보여줌
[root@CentOS /test]# last -R           Hostname 또는 IP 주소 정보는 안보여줌
[root@CentOS /test]# last -a           Hostname 또는 IP 주소 정보를 가장 마지막 필드에 보여줌

 

 


@ 'lastlog', 'lastb'  명령어

 

Ex1) 'lastlog' 명령어 형식

 

[root@CentOS /test]# lastlog                   모든 사용자 마지막 로그인 정보만 출력해줌
[root@CentOS /test]# lastlog -u root        'root' 사용자 마지막 로그인 정보만 출력해줌
[root@CentOS /test]# lastlog -u user1      'user1' 사용자 마지막 로그인 정보만 출력해줌
[root@CentOS /test]# lastlog -t 3              3일 이내에 접속한 마지막 사용자 로그인 정보만 출력해줌

 


Ex2) 'lastlog' 원격 접속 정보 확인

 

[root@CentOS /test]# ssh user1@localhost
user1@localhost's password:
Last login: Wed Dec 23 14:00:30 2015 from localhost
[user1@CentOS ~]$
[user1@CentOS ~]$ lastlog

 

[user1@CentOS ~]$ lastlog -u user1
사용자이름       포트     어디서           최근정보
user1            pts/5    localhost        수 12월 23 14:14:37 +0900 2015

[user1@CentOS ~]$ exit
logout
Connection to localhost closed.
[root@CentOS /test]#

 

 

Ex3) 'lastb' 명령어 확인 (접속 실패를 보여줌)


[root@CentOS /test]# cat /etc/passwd | grep user1
user1:x:500:500::/home/user1:/bin/bash

 

[root@CentOS /test]# ssh user1@localhost
user1@localhost's password: (엔터)
Permission denied, please try again.
user1@localhost's password: (엔터)
Permission denied, please try again.
user1@localhost's password: (엔터)
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[root@CentOS /test]#
[root@CentOS /test]# lastb

btmp begins Fri Dec 18 22:23:27 2015

[root@CentOS /test]# lastb user1
user1    ssh:notty    localhost        Wed Dec 23 14:17 - 14:17  (00:00)
user1    ssh:notty    localhost        Wed Dec 23 14:17 - 14:17  (00:00)
user1    ssh:notty    localhost        Wed Dec 23 14:17 - 14:17  (00:00)

btmp begins Fri Dec 18 22:23:27 2015


[root@CentOS /test]#

 

 

Ex4) 'lastb' 옵션 명령어

 

[root@CentOS /test]# last -3  (접속 실패 3개 라인만 출력해줌)
user1    pts/5        localhost        Wed Dec 23 14:14 - 14:15  (00:00)
root     pts/5        localhost        Wed Dec 23 14:14 - 14:14  (00:00)
root     pts/5        localhost        Wed Dec 23 14:13 - 14:14  (00:00)

wtmp begins Fri Dec 18 22:20:21 2015

 

[root@CentOS /test]# lastb | grep user1  (user1 접속 실패만 보여줌)
user1    ssh:notty    localhost        Wed Dec 23 14:17 - 14:17  (00:00)
user1    ssh:notty    localhost        Wed Dec 23 14:17 - 14:17  (00:00)
user1    ssh:notty    localhost        Wed Dec 23 14:17 - 14:17  (00:00)

 

[root@CentOS /test]# lastb | grep user1 | wc -l
3

 

[root@CentOS /test]#

 

 

 

@ 'who' 명령어

 

Ex1) 'who' 옵션 명령어

 

[root@CentOS /test]# who               로컬 시스템에 로그인한 정보를 보여줌
[root@CentOS /test]# who -H           헤드라인을 포함하여 출력해줌
[root@CentOS /test]# who -r            현재 사용자의 Run Level을 보여줌
[root@CentOS /test]# who am i         현재 접속한 사용자 로그인 전체정보를 보여줌
[root@CentOS /test]# whoami           현재 접속한 사용자 로그인명만 보여줌

 


Ex2) 'who' 원격 접속 정보 확인

 

 -'putty' 새로 열기 -> 'user1'로 접속 실시

 

login as: user1
user1@192.168.35.130's password:
Last login: Wed Dec 23 14:28:26 2015 from localhost
[user1@CentOS ~]$
[user1@CentOS ~]$ id
uid=500(user1) gid=500(user1) groups=500(user1)

[user1@CentOS ~]$ pwd
/home/user1

[user1@CentOS ~]$ tty
/dev/pts/1

 

 - 'root'에서 정보 확인

 

[root@CentOS /test]# who
root     tty1           2015-12-21 19:46 (:0)
root     pts/0        2015-12-23 14:35 (192.168.1.1)
user1   pts/1        2015-12-23 14:35 (192.168.1.1)

 

 

 

@ 'w' 명령어

 

Ex1) 'w' 옵션 명령어

 

[root@CentOS /test]# w                  로컬 시스템에 접속한 사용자가 어떤 명령어를 실행하고 있는지를 보여줌
[root@CentOS /test]# w root           'root'로 접속한 사용자가 어떤 명령어를 실행하고 있는지를 보여줌
[root@CentOS /test]# w user1         'user1'로 접속한 사용자가 어떤 명령어를 실행하고 있는지를 보여줌

 

Ex2)  'w' 원격 접속 정보 확인

 

 - user1에서 'vi /etc/passwd' 실행

 

[user1@CentOS ~]$ vi /etc/passwd


 - 'root'에서 정보 확인

 

[root@CentOS /test]# w user1
 14:37:46 up 1 day, 18:53,  3 users,  load average: 0.04, 0.03, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
user1    pts/1    192.168.35.1     14:35    5.00s  0.07s  0.03s vim /etc/passwd

 

 

Ex3) 'root'에서 'user1' 로그아웃 실행하기

 

[root@CentOS /test]# w
 14:39:20 up 1 day, 18:55,  3 users,  load average: 0.00, 0.02, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     :0               Mon19   42:55m 30.04s 30.04s /usr/bin/Xorg :0 -br
root     pts/0    192.168.35.1     14:35    0.00s  0.10s  0.03s w
user1    pts/1    192.168.35.1     14:35    1:30   0.04s  0.04s -bash

 

[root@CentOS /test]# w user1
 14:39:25 up 1 day, 18:55,  3 users,  load average: 0.00, 0.02, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
user1    pts/1    192.168.35.1     14:35    1:35   0.04s  0.04s -bash

 

[root@CentOS /test]# ps -fu user1
UID        PID  PPID  C STIME TTY          TIME CMD
user1    26421 26413  0 14:35 ?        00:00:00 sshd: user1@pts/1
user1    26422 26421  0 14:35 pts/1    00:00:00 -bash

 

[root@CentOS /test]# kill -9 26422

 

[root@CentOS /test]# ps -fu user1
UID        PID  PPID  C STIME TTY          TIME CMD

[root@CentOS /test]# w
 14:44:13 up 1 day, 19:00,  2 users,  load average: 0.60, 0.17, 0.05
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     :0               Mon19   43:00m 30.09s 30.09s /usr/bin/Xorg :0 -br
root     pts/0    192.168.35.1     14:35    0.00s  0.28s  0.01s w

 

 

 

@ 'cmp' & 'diff' 명령어

 

서로 다른 파일을 비교하여 다른 내용을 출력해 준다.

 

Ex) 'cm' 명령어

 

[root@CentOS /test]# cat > file1
1111
2222
3333
(ctrl + d)


[root@CentOS /test]# cat > file2
1111
2222
3333

(ctrl + d)


[root@CentOS /test]# cat > file3
1111
2222
4444

(ctrl + d)


[root@CentOS /test]# cmp file1 file2
[root@CentOS /test]# cmp file1 file3
file1 file3 differ: byte 11, line 3

 

 

Ex2) 'diff' 명령어 I

 

[root@CentOS /test]# diff file1 file2
[root@CentOS /test]# diff file1 file3
3c3
< 3333
---
> 4444

[root@CentOS /test]# diff -c file1 file3
*** file1       2016-01-22 13:47:47.739086975 +0900
--- file3       2016-01-22 13:47:58.861090488 +0900
***************
*** 1,3 ****
  1111
  2222
! 3333
--- 1,3 ----
  1111
  2222
! 4444

 

 

Ex2) 'diff' 명령어 II (대소문자 구분 및 대소문자 구분 없이 비교)


[root@CentOS /test]# cat > file4
1111
2222
aaaa
(ctrl + d)


[root@CentOS /test]# cat > file5
1111
2222
AAAA
(ctrl + d)


[root@CentOS /test]# diff file4 file5
3c3
< aaaa
---
> AAAA

[root@CentOS /test]# diff -i file4 file5

 

 

Ex3) 'diff' 명령어 II (디렉토리 비교)

 

[root@CentOS /test]# mkdir dir1
[root@CentOS /test]# cat > dir1/file1
1111
2222

[root@CentOS /test]# mkdir dir2
[root@CentOS /test]# cat > dir2/file1
1111
3333

[root@CentOS /test]# diff --recursive dir1 dir2
diff --recursive dir1/file1 dir2/file1
2c2
< 2222
---
> 3333

[root@CentOS /test]# rm -r dir2/file1
rm: remove 일반 파일 `dir2/file1'? y

[root@CentOS /test]# cat > dir2/file1
1111
2222

(ctrl + d)


[root@CentOS /test]# cat > dir2/file2
3333

(ctrl + d)


[root@CentOS /test]# diff --recursive dir1 dir2
Only in dir2: file2

 

[root@CentOS /test]# rm -rf *
[root@CentOS /test]# ls

 

 

@ 'sort' 명령어

 

하나 또는, 다수 파일의 텍스트 라인을 정렬할때 사용한다.

(옵션 없이 사용할 경우, 필드 구분자는 공백이며, 오름차순 및 문자열 순으로 정렬한다.)

 

[root@CentOS /test]# sort -n         숫자 순번으로 정렬

[root@CentOS /test]# sort -r          내림차순으로 정렬

[root@CentOS /test]# sort -o         출력 결과를 파일에 저장

[root@CentOS /test]# sort -t          필드 구분자 지정

[root@CentOS /test]# sort -k         정렬할 필드 지정

 

 

Ex1) 'sort' 정렬 예제 I

 

[root@CentOS /test]# cat > file1
linux100 10 20 31 50
linux200 20 25 31 20
linux300 30 20 30 40
linux400 50 20 30 80

(ctrl + d)


[root@CentOS /test]# sort file1                 (오름차순으로 문자열 정렬)
linux100 10 20 31 50
linux200 20 25 31 20
linux300 30 20 30 40
linux400 50 20 30 80

[root@CentOS /test]# sort -r file1              (내림차순으로 정렬)
linux400 50 20 30 80
linux300 30 20 30 40
linux200 20 25 31 20
linux100 10 20 31 50

[root@CentOS /test]# sort -k 3 -n file1       (3번째 필드 기준으로 숫자로 정렬)
linux100 10 20 31 50
linux300 30 20 30 40
linux400 50 20 30 80
linux200 20 25 31 20

 

[root@CentOS /test]# sort -t : -k 1 /etc/passwd      (':'를 필드 구분자로 하여 1번째 필드 문자로 정렬)
abrt:x:173:173::/etc/abrt:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin

[root@CentOS /test]# sort -t : -k 3 -n /etc/passwd   (':'를 필드 구분자로 하여 3번째 필드 숫자로 정렬)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

 

 

Ex2) 'sort' 정렬 예제 II

 

[root@CentOS /test]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root
                       35G  3.7G   30G  12% /
tmpfs                1000M   72K 1000M   1% /dev/shm
/dev/sda1             477M   34M  419M   8% /boot

[root@CentOS /test]# du -sk /var
188988  /var

[root@CentOS /test]# cd /var
[root@CentOS /var]# du -sk * | sort
0       mail
111508  cache
1172    www
12      yp
132     spool
16      lock
280     run
4       account
4       crash
4       cvs
4       games
4       gdm
4       local
4       nis
4       opt
4       preserve
4       tmp
4280    log
71532   lib
8       db
8       empty

[root@CentOS /var]# du -sk * | sort -n
0       mail
4       account
4       crash
4       cvs
4       games
4       gdm
4       local
4       nis
4       opt
4       preserve
4       tmp
8       db
8       empty
12      yp
16      lock
132     spool
280     run
1172    www
4280    log
71532   lib
111508  cache

 

[root@CentOS /var]# du -sk * | sort -nr
111508  cache
71532   lib
4280    log
1172    www
280     run
132     spool
16      lock
12      yp
8       empty
8       db
4       tmp
4       preserve
4       opt
4       nis
4       local
4       gdm
4       games
4       cvs
4       crash
4       account
0       mail

[root@CentOS /var]# cd -
/test


[root@CentOS /test]# rm -rf *

 

 

 

@ 'file' 명령어

 

파일 유형을 확인할 수 있다.

 

Ex) 'file' 명령어

 

[root@CentOS /test]# file /etc/passwd
/etc/passwd: ASCII text          

(ASCII 파일)


[root@CentOS /test]# file /bin/ls
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped                                  

(Binary 파일)


[root@CentOS /test]# file /var/run/utmp
/var/run/utmp: data                

(data 파일)


[root@CentOS /test]# file /etc/rc.d/init.d/httpd
/etc/rc.d/init.d/httpd: Bourne-Again shell script text executable          

 (Script 파일)

[root@CentOS /test]# file /etc/hosts /etc     
/etc/hosts: ASCII text       (ASCII 파일)
/etc:       directory            (디렉토리)

[root@CentOS /test]# cd /etc
[root@CentOS /etc]# file *

[root@CentOS /etc]# cd -
/test

[root@CentOS /test]#

 

 

2. 검색 명령어

 

@ 'grep' 명령어

 

[root@CentOS /test]# grep               소/대문자를 구분하여 패턴 검색

[root@CentOS /test]# grep -i            소/대문자를 구분하지 않고 패턴 검색

[root@CentOS /test]# grep -n           패턴 검색이 해당 라인 번호까지 출력

[root@CentOS /test]# grep -l            패턴 내용이 있는 파일만 출력

[root@CentOS /test]# grep -c           패턴 검색 라인 개수 출력  

[root@CentOS /test]# grep -v           패턴 검색 내용 제외 출력

[root@CentOS /test]# grep -w           한 단어 패턴 검색

 

 

Ex1) 'grep' 옵션 명령어


[root@CentOS /test]# useradd USER1
[root@CentOS /test]# passwd USER1

[root@CentOS /test]# grep user1 /etc/passwd
user1:x:500:500::/home/user1:/bin/bash

[root@CentOS /test]# grep -i user1 /etc/passwd
user1:x:500:500::/home/user1:/bin/bash
USER1:x:502:502::/home/USER1:/bin/bash

[root@CentOS /test]# grep -n user1 /etc/passwd
34:user1:x:500:500::/home/user1:/bin/bash

[root@CentOS /test]# grep -n root /etc/group
1:root:x:0:

 

[root@CentOS /test]# grep -l root /etc/group /etc/passwd /etc/hosts
/etc/group
/etc/passwd

[root@CentOS /test]# grep -c user /etc/passwd
5

 


Ex2) 'grep' 명령어 패턴 예제 I

 

[root@CentOS /test]# 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@CentOS /test]# grep -n "network-function" /etc/rc.d/rc0.d/K90network
39:. ./network-functions

[root@CentOS /test]# cat -n /etc/rc.d/rc0.d/K90network | grep network-function
    39  . ./network-functions

 

 

Ex3) 'grep' 명령어 패턴 예제 II

 

[root@CentOS /test]# cat /var/log/messages | grep -i jan

[root@CentOS /test]# cat /var/log/messages | grep -i 'jan 20'

 

 

Ex4) 'grep' 명령어 패턴 예제 III

 

[root@CentOS /test]# ps
  PID TTY          TIME CMD
25889 pts/0    00:00:00 bash
28166 pts/0    00:00:00 ps

[root@CentOS /test]# ps -ef | grep 29160
root     28168 25889  0 21:32 pts/0    00:00:00 grep 29160

[root@CentOS /test]# ps -ef | grep bash
root     25889 25885  0 18:18 pts/0    00:00:00 -bash
user2    25943 25942  0 18:18 pts/2    00:00:00 -bash
user1    27270 27269  0 20:05 pts/1    00:00:00 -bash
root     28173 25889  0 21:32 pts/0    00:00:00 grep bash

[root@CentOS /test]# rpm -qa | grep sendmail


[root@CentOS /test]# rpm -qa | grep gcc
libgcc-4.4.7-11.el6.x86_64

Ex4) 파일 내에 특정 패턴 여러개 검색


[root@CentOS /test]# egrep "user1|user2" /etc/passwd
user1:x:500:500::/home/user1:/bin/bash
user2:x:501:501::/home/user2:/bin/bash

 

[root@CentOS /test]# cat /var/log/messages | egrep -i '(warn|err|crit|alert|emerg)'
Jan 21 18:06:10 CentOS NetworkManager[1809]: <info> (eth0): carrier now OFF (device state 8, deferring action for 4 seconds)
Jan 21 18:06:49 CentOS vmsvc[1405]: [ warning] [powerops] Unable to send the status RPC.

[root@CentOS /test]# egrep -v "user1|user2" /etc/passwd

 

 

 

@ 'whereis' 명령어

 

$PATH 변수와 관계 없이 대부분의 명령어를 검색한다. (도움말 파일과 /etc 디렉토리 하위에 있는 파일 검색)

 

Ex) 'whereis' 명령어

 

[root@CentOS /test]# whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

[root@CentOS /test]# whereis cp
cp: /bin/cp /usr/share/man/man1/cp.1.gz /usr/share/man/man1p/cp.1p.gz

[root@CentOS /test]# whereis cd
cd: /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz

[root@CentOS /test]# whereis man
man: /usr/bin/man /etc/man.config /usr/share/man /usr/share/man/man1/man.1.gz /usr/share/man/man1p/man.1p.gz /usr/share/man/man7/man.7.gz

[root@CentOS /test]# whereis vi
vi: /bin/vi /usr/share/man/man1/vi.1.gz /usr/share/man/man1p/vi.1p.gz

[root@CentOS /test]# whereis vim
vim: /usr/bin/vim /usr/share/vim /usr/share/man/man1/vim.1.gz

 

@ 'which' 명령어

 

$PATH 변수에 설정된 명령어 및 alias 설정된 명령어를 검색한다.

 

Ex) 'which' 명령어

 

[root@CentOS /test]# which ifconfig
/sbin/ifconfig

[root@CentOS /test]# which ls
alias ls='ls --color=auto'
        /bin/ls

 

 

 

@ 'find' 명령어

 

디렉토리 안에 있는 파일을 검색할 때 사용한다.

 

 

Ex1) 'find' 명령어

 

[root@CentOS /root]# find /etc -name pass*

/etc 디렉토리에 'pass' 단어로 시작하는 파일을 검색함

 

[root@CentOS /root]# find /etc -name *conf

/etc 디렉토리에 'conf' 단어로 끝나는 파일을 검색함

 

[root@CentOS /root]# find /etc/rc.d -type d

/etc/rc.d 디렉토리에 있는 디렉토리를 검색함

[root@CentOS /root]# find /etc/rc.d -type f

/etc/rc.d 디렉토리에 있는 파일을 검색함

 

 

Ex2) 'find' 명령어을 이용한 파일 검색

 

[root@CentOS /test]# touch file1 file2
[root@CentOS /test]# mkdir dir1 dir2
[root@CentOS /test]# touch dir1/file1 dir2/file3
[root@CentOS /test]# tree
.
├── dir1
│   └── file1
├── dir2
│   └── file3
├── file1
└── file2

2 directories, 4 files

[root@CentOS /test]# find . -name file1
./file1
./dir1/file1

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

 

[root@CentOS /test]# find /usr/share -name "*.log" -type f
/usr/share/doc/libjpeg-turbo-1.2.1/change.log

 

 

Ex3) 'find' 명령어을 이용한 사용자/그룹 검색

 

[root@CentOS /test]# find /home -user user1 -group user1
/home/user1
/home/user1/.viminfo
/home/user1/.bash_history
/home/user1/.bashrc
/home/user1/.gnome2
/home/user1/.bash_profile
/home/user1/.vimrc
/home/user1/.bash_logout
/home/user1/.mozilla
/home/user1/.mozilla/extensions
/home/user1/.mozilla/plugins

 

[root@CentOS /test]# find /home -user user1 -group user1 -ls
1703941    4 drwx------   4 user1    user1        4096  1월 21 20:37 /home/user1
1703960    4 -rw-------   1 user1    user1        3689  1월 21 20:37 /home/user1/.viminfo
1703949    4 -rw-------   1 user1    user1        1408  1월 21 21:44 /home/user1/.bash_history
1703951    4 -rw-r--r--   1 user1    user1         157  1월 20 13:18 /home/user1/.bashrc
1703943    4 drwxr-xr-x   2 user1    user1        4096 11월 12  2010 /home/user1/.gnome2
1703944    4 -rw-r--r--   1 user1    user1         176 10월 16  2014 /home/user1/.bash_profile
1703953    4 -rw-rw-r--   1 user1    user1          34  1월 20 14:14 /home/user1/.vimrc
1703945    4 -rw-r--r--   1 user1    user1          18 10월 16  2014 /home/user1/.bash_logout
1703946    4 drwxr-xr-x   4 user1    user1        4096  1월 12 00:17 /home/user1/.mozilla
1703947    4 drwxr-xr-x   2 user1    user1        4096  8월 19  2010 /home/user1/.mozilla/extensions
1703948    4 drwxr-xr-x   2 user1    user1        4096  8월 19  2010 /home/user1/.mozilla/plugins

 

 

Ex4)  'find' 명령어을 이용한 날짜 검색

 

[root@CentOS /test]# cd dir2
[root@CentOS /test/dir2]# touch file1 file2 file4 file5 file6
[root@CentOS /test/dir2]# ls
file1  file2  file3  file4  file5  file6

 

[root@CentOS /test/dir2]# date
2016. 01. 22. (금) 12:36:16 KST


[root@CentOS /test/dir2]# touch -t 01211000 file1
[root@CentOS /test/dir2]# touch -t 01201000 file2
[root@CentOS /test/dir2]# touch -t 01191000 file3
[root@CentOS /test/dir2]# touch -t 01181000 file4
[root@CentOS /test/dir2]# touch -t 01171000 file5
[root@CentOS /test/dir2]# touch -t 01161000 file6

[root@CentOS /test/dir2]# ls -l
합계 0
-rw-r--r--. 1 root root 0 2016-01-21 10:00 file1
-rw-r--r--. 1 root root 0 2016-01-20 10:00 file2
-rw-r--r--. 1 root root 0 2016-01-19 10:00 file3
-rw-r--r--. 1 root root 0 2016-01-18 10:00 file4
-rw-r--r--. 1 root root 0 2016-01-17 10:00 file5
-rw-r--r--. 1 root root 0 2016-01-16 10:00 file6

[root@CentOS /test/dir2]# find . -mtime 3 -type f       수정 날짜가 3일전 파일 검색
./file3

[root@CentOS /test/dir2]# find . -mtime -3 -type f     수정 날짜가 3일 안된 파일 검색
./file1
./file2

[root@CentOS /test/dir2]# find . -mtime +3 -type f     수정 날짜가 3일 지난 파일 검색
./file4
./file5
./file6

 

 

Ex)  'find' 명령어을 퍼미션 검색

 

[root@CentOS /test/dir2]# cd /test
[root@CentOS /test]# mkdir dir3
[root@CentOS /test]# cd dir3
[root@CentOS /test/dir3]# touch file{1,2,3,4,5,6,7,8}
[root@CentOS /test/dir3]# ls
file1  file2  file3  file4  file5  file6  file7  file8

 

[root@CentOS /test/dir3]# chmod 000 file1   (---)
[root@CentOS /test/dir3]# chmod 100 file2   (--x)

[root@CentOS /test/dir3]# chmod 200 file3   (-w-)
[root@CentOS /test/dir3]# chmod 300 file4   (-wx)
[root@CentOS /test/dir3]# chmod 400 file5   (r--)
[root@CentOS /test/dir3]# chmod 500 file6   (r-x)
[root@CentOS /test/dir3]# chmod 600 file7   (rw-)
[root@CentOS /test/dir3]# chmod 700 file8   (rwx)
[root@CentOS /test/dir3]# ls -l
합계 0
----------. 1 root root 0 2016-01-22 12:39 file1
---x------. 1 root root 0 2016-01-22 12:39 file2
--w-------. 1 root root 0 2016-01-22 12:39 file3
--wx------. 1 root root 0 2016-01-22 12:39 file4
-r--------. 1 root root 0 2016-01-22 12:39 file5
-r-x------. 1 root root 0 2016-01-22 12:39 file6
-rw-------. 1 root root 0 2016-01-22 12:39 file7
-rwx------. 1 root root 0 2016-01-22 12:39 file8

 

[root@CentOS /test/dir3]# find . -perm 500 -type f -ls
1966100    0 -r-x------   1 root     root            0  1월 22 12:39 ./file6

[root@CentOS /test/dir3]# find . -perm -100 -type f -ls
1966098    0 --wx------   1 root     root            0  1월 22 12:39 ./file4
1966102    0 -rwx------   1 root     root            0  1월 22 12:39 ./file8
1966096    0 ---x------   1 root     root            0  1월 22 12:39 ./file2
1966100    0 -r-x------   1 root     root            0  1월 22 12:39 ./file6

 

 

Ex)  'find' 명령어을 파일 크기 검색

 

[root@CentOS /test/dir3]# cp /etc/passwd file9
[root@CentOS /test/dir3]# cp /etc/services file10
[root@CentOS /test/dir3]# ls
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9

 

[root@CentOS /test/dir3]# ls -l
합계 632
----------. 1 root root      0 2016-01-22 12:39 file1
-rw-r--r--. 1 root root 641020 2016-01-22 13:32 file10
---x------. 1 root root      0 2016-01-22 12:39 file2
--w-------. 1 root root      0 2016-01-22 12:39 file3
--wx------. 1 root root      0 2016-01-22 12:39 file4
-r--------. 1 root root      0 2016-01-22 12:39 file5
-r-x------. 1 root root      0 2016-01-22 12:39 file6
-rw-------. 1 root root      0 2016-01-22 12:39 file7
-rwx------. 1 root root      0 2016-01-22 12:39 file8
-rw-r--r--. 1 root root   1693 2016-01-22 13:31 file9


[root@CentOS /test/dir3]# find . -size 1693c -type f         단위 : c(byte), k(kilo byte), M(mega byte), G(giga byte)
./file9

[root@CentOS /test/dir3]# find . -size -1693c -type f
./file4
./file3
./file1
./file7
./file8
./file2
./file5
./file6

[root@CentOS /test/dir3]# find . -size +1693c -type f
./file10



Ex) 'find' 명령어를 이용한 디렉토리 안에 특정 패턴을 갖고 있는 파일 삭제


[root@CentOS /test]# tree
.
├── dir1
│   └── file1
├── dir2
│   ├── file1
│   ├── file2
│   ├── file3
│   ├── file4
│   ├── file5
│   └── file6
├── dir3
│   ├── file1
│   ├── file10
│   ├── file2
│   ├── file3
│   ├── file4
│   ├── file5
│   ├── file6
│   ├── file7
│   ├── file8
│   └── file9
├── file1
└── file2

 

[root@CentOS /test]# find . -name file1 -type f
./dir2/file1
./file1
./dir3/file1
./dir1/file1

[root@CentOS /test]# find . -name file1 -type f -exec rm -f {} \;
[root@CentOS /test]# find . -name file1 -type f
[root@CentOS /test]# tree
.
├── dir1
├── dir2
│   ├── file2
│   ├── file3
│   ├── file4
│   ├── file5
│   └── file6
├── dir3
│   ├── file10
│   ├── file2
│   ├── file3
│   ├── file4
│   ├── file5
│   ├── file6
│   ├── file7
│   ├── file8
│   └── file9
└── file2

 

 

 

[root@CentOS /test]# cd ..
[root@CentOS /]# rm -rf test

[root@CentOS /]# userdel -r USER1

Posted by 김정우 강사(카카오톡 : kim10322)
,


Q