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

Linux I - 07. 리눅스 기본 명령어 (메타 케릭터/리다이렉션/파이프)

 

 

 

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


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

[root@CentOS /test]#

 

 

 

 

@ 쉘(Shell)

 

셀은 명령어가 입력되거나 스크립트 파일에서 읽어 들이는 명령어들을 해석하여 사용자와 커널 중계 역할을 담당하는 도구이다. 사용자가 로그인하면 쉘이 자동으로 시작하여 명령어가 입력되면 명령어 구분을 분석하고, 리다이렉션, 파이프, 와일드 카드, 작업 제어 등을 처리하고, 명령어를 PATH 변수에서 검색하여 존재하면 실행한다. 쉘이 담당하는 기능은 다음과 같다.

 

 - PATH 변수에 정의된 모든 디렉토리를 참조하여 명령어를 검색 및 실행한다.

 - 입출력 리다이렉션, 파잎, 백그라운드 프로세싱을 설정한다.

 - 명령어의 alias, 쉘 함수를 검색하여 실행한다.

 - TERM 변수를 사용하여 서로 다른 터미널 환경을 초기화한다.

 - 명령어 자동 완성 기능을 사용하여 입력한 명령어를 기록한다.

 - 사용자 환경 정의 파일을 가지고 사용자 환경을 초기호화한다.

 

 

 

1. 메타 케릭터(meta character)

 

 Shell이 특별한 의미로 해석하는 문자, 기호를 의미한다.

 

 ' '

작음 따옴표

쉘이 해석할 수 없도록 막아 줌

 " "

큰 따옴표

쉘이 해석할 수 없도록 막아 줌, 단 $, ``, \은 인식됨

`  `

역 따옴표

쉘이 해석할 때 명령어로 인식함, 그렇기 때문에 역 따옴표 안의 내용을 실행한다.

$( )

역 따움표 동일

역 따옴표와 동일함

\

역 슬래쉬

역 슬래쉬 다음에 나온 문자를 쉘이 해석할 수 없도록 막아줌

;

세미콜론

한개의 라인에 여러개 명령어를 수행해줌

 *

 애스터리스크

 모든 파일 의미함

 <

 입력 재지정

입력을 파일로 할때 사용함 

 <<

 입력 완료

 이 문자열까지만 입력을 받음(here document)

 >

 출력 재지정

 출력 내용을 파일로 저장할때 사용함 (덮어쓰기됨)

 >>

 출력 재지정

 출력 내용을 파일로 저장할때 사용함 (이어쓰기됨)

 |

 파이프

앞에서 실행한 명령어 출력 결과를 뒤에서 실행하는 명령어 입력값으로 처리함

 

 

 

Ex1) 작은 따옴표


[root@CentOS /test]# echo $HOME
/root


[root@CentOS /test]# echo '$HOME'
$HOME


[root@CentOS /test]#


 

Ex2) 큰 따옴표

 

[root@CentOS /test]# echo $HOME
/root


[root@CentOS /test]# echo "$HOME"
/root


[root@CentOS /test]# echo "$HOME is my directory"
/root is my directory


[root@CentOS /test]#

 

 

Ex3) 역 따옴표

 

[root@CentOS /test]# date
2016. 01. 22. (금) 15:36:14 KST


[root@CentOS /test]# echo `date`
2016. 01. 22. (금) 15:36:19 KST


[root@CentOS /test]# echo `hostname`
CentOS


[root@CentOS /test]# echo "`hostname` is my hostanme"
CentOS is my hostanme

 

 

Ex4) 역슬래쉬


[root@CentOS /test]# echo $HOME
/root


[root@CentOS /test]# echo \$HOME
$HOME


[root@CentOS /test]# find / \( -perm -4000 -o -perm -2000 \) -type f

 

 

Ex5) 세미콜론


[root@CentOS /test]# echo $HOME ; date ; cal
/root
2016. 01. 22. (금) 15:39:38 KST
      1월 2016
일 월 화 수 목 금 토
                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

 

 

 

2. 리다이렉션(redirection)

 

입출력 방향을 재지정하는 기능이다.

 

 

1) FD(File Descriptor)

 

각 프로세스를 위한 FD가 쉘에 의해서 생성된다. 한개 파일을 열면 그 파일을 나타내는 특별한 숫자를 쉘이 생성하는데 이것을 FD라고 한다.

 

[예약된 FD 번호]

 fd : 0

 STDIN

  표준 입력, 특별히 입력이 지정되지 않는 경우 키보드에서 입력을 받는다.

 fd : 1

 STDOUT

  표준 출력, 특별히 출력이 지정되지 않는 경우 정상적인 출력 결과를 모니터로 출력한다.

 fd : 2

 STDERR

  표준에러, 특별히 출력이 지정되지 않는 경우 에러의 출력 결과를 모니터로 출력한다.

 

위에 FD들은 예약되어 있기 때문에 다른 실행 관련 내용들은 이후, 번호를 할당받게 된다.

 

Ex1) FD 확인

 

 - Putty를 이용하여 root 계정으로 2개를 접속한다.

 

<Putty 1>

[root@CentOS /test]# vi /etc/passwd

 

<Putty 2>

[root@CentOS /root]# ps -ef | grep vi
root      5790  2651  0 16:24 pts/0    00:00:00 vim /etc/passwd
root      5841  5819  0 16:26 pts/1    00:00:00 grep vi

[root@CentOS /root]# cd /proc/5790/fd
[root@CentOS /proc/5790/fd]# ls -l
합계 0
lrwx------. 1 root root 64 2016-01-22 16:27 0 -> /dev/pts/0
lrwx------. 1 root root 64 2016-01-22 16:27 1 -> /dev/pts/0
lrwx------. 1 root root 64 2016-01-22 16:26 2 -> /dev/pts/0
lrwx------. 1 root root 64 2016-01-22 16:27 4 -> /etc/.passwd.swp

 

<Putty 1>

vi 편집기 종료

 

<Putty 2>
[root@CentOS /proc/5790/fd]# cd -
/root
[root@CentOS /root]# cd /proc/5790/fd
-bash: cd: /proc/5790/fd: 그런 파일이나 디렉터리가 없습니다

 

 

Ex2) proc 디렉토리에 프로세스 번호 생성 확인

 

<Putty 2>

[root@CentOS /root]# cd /proc
[root@CentOS /proc]# ls > /test/proc_`date +%H%M`.txt

 

<Putty 1>

[root@CentOS /test]# vi /etc/passwd

 

<Putty 2>
[root@CentOS /proc]# ps -ef | grep vi
root      5902  2651  0 16:34 pts/0    00:00:00 vim /etc/passwd
root      5905  5819  0 16:34 pts/1    00:00:00 grep vi

[root@CentOS /proc]# ls /test
proc_1634.txt


[root@CentOS /proc]# grep 5902 /test/proc_1634.txt
[root@CentOS /proc]#
[root@CentOS /proc]# ls > /test/proc_`date +%H%M`.txt
[root@CentOS /proc]# ls /test
proc_1634.txt  proc_1636.txt


[root@CentOS /proc]# grep 5902 /test/proc_1636.txt
5902

 

<Putty 1>

vi 편집기 종료

 

<Putty 2>

[root@CentOS /proc]# ls > /test/proc_`date +%H%M`.txt
[root@CentOS /proc]# ls /test
proc_1634.txt  proc_1636.txt  proc_1639.txt

[root@CentOS /proc]# grep 5902 /test/proc_1639.txt

 

<Putty 1>
[root@CentOS /test]# ls
proc_1634.txt  proc_1636.txt  proc_1639.txt

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

 


 

2) 표준 입력 & 표준 출력 & 표준 에러

 

쉘은 명령어를 키보드를 통하여 입력받는다. 그리고 명령어 출력 결과는 모니터에 출력한다. 이러한 명령어 출력 결과나 입출력을 리다이렉션 심볼을 이용하여 방향을 재지정할 수 있다. 또한 방향을 재지정할때 모니터가 아닌 파일로 저장할 수 있다.

 

[쉘이 처리하는 표준 입력/표준 출력/표준 에러]

 

                                                                           -------------> 표준 출력(1, STDOUT)

  표준 입력(0, STDIN) --------------> 쉘(Shell/Bash)

                                                                           -------------> 표준 에러(2, STDERR)

              

       

 

Ex) 표준 입력/출력/에러 예제

 

[root@CentOS /test]# ls /var /nodir
ls: cannot access /nodir: 그런 파일이나 디렉터리가 없습니다   (표준 에러)
r:
account  crash  db     games  lib    lock  mail  opt       run    tmp  yp
cache    cvs    empty  gdm    local  log   nis   preserve  spool  www


[root@CentOS /test]#
[root@CentOS /test]# cat
ccna  <------- 엔터 (표준 입력)
ccna  <------------ (표준 출력)
ccnp  <------- 엔터 (표준 입력)
ccnp <------------ (표준 출력)

(ctrl+d)

 

[root@CentOS /test]# tr linux LINUX
hello
heLLo
unix
UNIX
linux
LINUX
1234
1234

[root@CentOS /test]# tr a-z A-Z
hello
HELLO
abcde
ABCDE

 

 

3) 입력 재지정(Redirection STDIN)

 

입력 재지정은 표준 입력에서 입력을 받지 않고, 다른쪽으로부터 입력을 받는 것을 의미한다. 즉, 키보드로부터 입력을 받는게 아니라 파일로부터 입력을 받는 것을 의미한다.

 

Ex1) 입력 재지정 예제 I

 

[root@CentOS /test]# cat 0< /etc/passwd

 

[root@CentOS /test]# cat < /etc/passwd           (0은 생략해도 됨)

 

 

Ex2) 입력 재지정 예제 II

 

[root@CentOS /test]# tr a-z A-Z < /etc/passwd

[root@CentOS /test]# tr a-z A-Z < /etc/passwd > p.txt
[root@CentOS /test]# ls
p.txt


[root@CentOS /test]# cat p.txt

[root@CentOS /test]# rm p.txt
rm: remove 일반 파일 `p.txt'? y

 

Ex3) 입력 재지정 예제 III

 

[root@CentOS /test]# cat <<END
> hello
> linux
> END
hello
linux

[root@CentOS /test]# cat <<END
> hello
> linux END
> END
hello
linux END

 

 

4) 출력 재지정(Redirection STDOUT)

 

입력 재지정은 출력 결과를 표준 출력으로 출력하지 않고, 다른쪽으로 출력하는 것을 의미한다. 즉, 모니터로 출력할 내용을 파일로 저장하는 것을 의미한다.

 

Ex1) 출력 재지정 예제 I

 

[root@CentOS /test]# cd
[root@CentOS /test]# ls -l /root
합계 2096
-rw-------. 1 root root    1594 2016-01-12 00:27 anaconda-ks.cfg
-rw-r--r--. 1 root root 2028252 2016-01-11 13:13 cfile4.uf@2752A13656932BE71E15DA.rpm
-rw-------. 1 root root       1 2016-01-22 17:16 dead.letter
drwxr-xr-x. 3 root root    4096 2016-01-13 14:50 home
~ 중간 생략 ~

[root@CentOS /test]# ls -l /root > lsfile
[root@CentOS /test]# ls -l lsfile
-rw-r--r--. 1 root root 990 2016-01-22 17:21 lsfile

[root@CentOS /test]# cat lsfile
합계 2096
-rw-------. 1 root root    1594 2016-01-12 00:27 anaconda-ks.cfg
-rw-r--r--. 1 root root 2028252 2016-01-11 13:13 cfile4.uf@2752A13656932BE71E15DA.rpm
-rw-------. 1 root root       1 2016-01-22 17:16 dead.letter
drwxr-xr-x. 3 root root    4096 2016-01-13 14:50 home
~ 중간 생략 ~

[root@CentOS /test]# rm lsfile
rm: remove 일반 파일 `lsfile'? y

 

 

Ex2)  출력 재지정 예제 II

[root@CentOS /test]# echo 1111
1111

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

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

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

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

 

 

Ex3) root에서 user1에게 메세지 출력

 

 - Putty를 이용한 user1 접속 실시

 

login as: user1
user1@192.168.1.2's password:
Last login: Fri Jan 22 10:52:47 2016 from 192.168.1.1
[user1@CentOS /home/user1]$
[user1@CentOS /home/user1]$ tty
/dev/pts/1

 

 - root Putty 창

 

[root@CentOS /test]# who
root     pts/0        2016-01-22 10:52 (192.168.1.1)
user1    pts/1        2016-01-22 18:10 (192.168.1.1)

[root@CentOS /test]# echo "hi user1" > /dev/pts/1

 

 - user1 Putty 확인

 

[user1@CentOS /home/user1]$ hi user1

 

 

 

5) 에러 재지정(Redirection STDERR)

 

에러 재지정은 에러 결과를 표준 에러로 출력하지 않고, 다른쪽으로 출력하는 것을 의미한다. 즉, 모니터로 출력할 에러 내용을 파일로 저장하는 것을 의미한다.

 

Ex1) 에러 재지정 예제

 

[root@CentOS /test]# touch file1 file2 file3
[root@CentOS /test]# ls -l /test /nodir
ls: cannot access /nodir: 그런 파일이나 디렉터리가 없습니다
/test:
합계 0
-rw-r--r--. 1 root root 0 2016-01-22 17:35 file1
-rw-r--r--. 1 root root 0 2016-01-22 17:35 file2
-rw-r--r--. 1 root root 0 2016-01-22 17:35 file3

[root@CentOS /test]# ls -l /test /nodir 2> errfile
/test:
합계 4
-rw-r--r--. 1 root root 75 2016-01-22 17:35 errfile
-rw-r--r--. 1 root root  0 2016-01-22 17:35 file1
-rw-r--r--. 1 root root  0 2016-01-22 17:35 file2
-rw-r--r--. 1 root root  0 2016-01-22 17:35 file3

[root@CentOS /test]# cat errfile
ls: cannot access /nodir: 그런 파일이나 디렉터리가 없습니다

 

[root@CentOS /test]# rm errfile
rm: remove 일반 파일 `errfile'? y

 

 

Ex2) 출력 재지정 & 에러 재지정 예제


[root@CentOS /test]# ls -l /test /nodir > lsfile 2> errfile
[root@CentOS /test]# ls -l
합계 8
-rw-r--r--. 1 root root  75 2016-01-22 17:38 errfile
-rw-r--r--. 1 root root   0 2016-01-22 17:35 file1
-rw-r--r--. 1 root root   0 2016-01-22 17:35 file2
-rw-r--r--. 1 root root   0 2016-01-22 17:35 file3
-rw-r--r--. 1 root root 269 2016-01-22 17:38 lsfile

[root@CentOS /test]# cat lsfile
/test:
합계 4
-rw-r--r--. 1 root root 75 2016-01-22 17:38 errfile
-rw-r--r--. 1 root root  0 2016-01-22 17:35 file1
-rw-r--r--. 1 root root  0 2016-01-22 17:35 file2
-rw-r--r--. 1 root root  0 2016-01-22 17:35 file3
-rw-r--r--. 1 root root  0 2016-01-22 17:38 lsfile

[root@CentOS /test]# cat errfile
ls: cannot access /nodir: 그런 파일이나 디렉터리가 없습니다

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

 

 

 

3. 파이프

 

파이프는 앞에 실행한 명령어의 출력 결과를 뒤에 실행하는 명령어의 입력 값으로 처리해주는 기능이다.

 

 

앞 명령어에 의한 결과값 ---------> 파이프 ---------> 뒤 명령어의 입력값 

     [커널 버퍼]

 

 

그리고 파이프 뒤에는 입력 받을 수 있는 명령어로 실행되어야 한다.

 


 

Ex1) 파이프 예제 I

 

[root@CentOS /test]# ps -ef | less

[root@CentOS /test]# ps -ef | grep inetd
root      6663  2651  0 18:02 pts/0    00:00:00 grep inetd

[root@CentOS /test]# ps -ef | grep bash
root      2651  2647  0 10:52 pts/0    00:00:04 -bash
user1     6767  6766  0 18:10 pts/1    00:00:00 -bash
user1     6899  6898  0 18:20 pts/2    00:00:00 bash -i
root      7490  2651  0 19:25 pts/0    00:00:00 grep bash

[root@CentOS /test]# ls -al / | sort -r | more

[root@CentOS /test]# grep nologin /etc/passwd | wc -l
29

[root@CentOS /test]# ifconfig | grep inet
          inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe17:e0c6/64 Scope:Link
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host

 

 

Ex2) 파이프 예제 II


[root@CentOS /test]# cat -n /etc/passwd

[root@CentOS /test]# head -n 10 /etc/passwd
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
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

 

[root@CentOS /test]# cat -n /etc/passwd | head -n 5
     1 root:x:0:0:root:/root:/bin/bash
     2 bin:x:1:1:bin:/bin:/sbin/nologin
     3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4 adm:x:3:4:adm:/var/adm:/sbin/nologin
     5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin


[root@CentOS /test]# cat -n /etc/passwd | head -n 5 | tail -n 2
     4 adm:x:3:4:adm:/var/adm:/sbin/nologin
     5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

[root@CentOS /test]# cat -n /etc/passwd | head -n 5 | tail -n 2 | head -n 1
     4 adm:x:3:4:adm:/var/adm:/sbin/nologin

 

Ex3) 파이프 예제 III

 

[root@CentOS /test]# cat -n /etc/services | head -n 5010 | tail -n 11
  5000 apollo-gms      2759/udp                # APOLLO GMS
  5001 sabams          2760/tcp                # Saba MS
  5002 sabams          2760/udp                # Saba MS
  5003 dicom-iscl      2761/tcp                # DICOM ISCL
  5004 dicom-iscl      2761/udp                # DICOM ISCL
  5005 dicom-tls       2762/tcp                # DICOM TLS
  5006 dicom-tls       2762/udp                # DICOM TLS
  5007 desktop-dna     2763/tcp                # Desktop DNA
  5008 desktop-dna     2763/udp                # Desktop DNA
  5009 data-insurance  2764/tcp                # Data Insurance
  5010 data-insurance  2764/udp                # Data Insurance

 

Ex4) 파이프 예졔 IV

 

[root@CentOS /test]# ls -al /etc

[root@CentOS /test]# ls -al /etc | cat -n

[root@CentOS /test]# ls -al /etc | cat -n | tail -n 1
   255 drwxr-xr-x.   2 root root   4096 2016-01-12 00:20 yum.repos.d

[root@CentOS /test]# ls -al /etc | wc -l
255

 

Ex5) 파이프 예제 V

 

[user1@CentOS /home/user1]$ who
root     pts/0        2016-01-22 10:52 (192.168.1.1)
user1    pts/1        2016-01-22 18:10 (192.168.1.1)
user1    pts/2        2016-01-22 18:20
[user1@CentOS /home/user1]$
[user1@CentOS /home/user1]$ who | wc -l
3

 

Ex6) 파이프 & 'tee' 명령어 예제 I


[root@CentOS /test]# cal 1 2016 > file1.log
[root@CentOS /test]# cal 1 2016 | tee file2.log     ('tee' 명령어 : 입력을 출력으로 보내기 이전에 파일로 기록함)
      1월 2016
일 월 화 수 목 금 토
                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


[root@CentOS /test]# ls
file1.log  file2.log

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


 

Ex7) root에서 실행하는 내용들 user1에서도 출력하는 예제

 

 - root Putty 창


[root@CentOS /test]# script -a /dev/null | tee /dev/pts/1
[root@CentOS /test]# date
2016. 01. 22. (금) 18:21:26 KST

 

 - user1 Putty 창

 

[user1@CentOS /home/user1]$ scritp [root@CentOS /test]#
bash: scritp: command not found
[user1@CentOS /home/user1]$ date
2016. 01. 22. (금) 18:21:26 KST

 

 - root Putty 창

 

[root@CentOS /test]# exit
exit
Script started, file is /dev/null
                                 Script started, file is /dev/null
Script done, file is /dev/null
[root@CentOS /test]#

 

 

 

 

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

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


Q