본문으로 바로가기

PS1="[\u@\h \$PWD]# "

'ls' 명령어

[root@CentOS /test]# ls -ld    -------------> 디렉토리 이름을 안붙였기 때문에 점(현재 디렉토리로) 나타냄
drwxr-xr-x 3 root root 4096 2017-04-18 17:06 .

[root@CentOS /test]# ls -ld dir1
drwxr-xr-x 2 root root 4096 2017-04-18 17:06 dir1


* [root@CentOS /root]# ls -R 해당 디렉토리와 하위 디렉토리 모든 내용 출력

* [root@CentOS /test]# ls -lR /test
/test:
합계 4
drwxr-xr-x 2 root root 4096 2017-04-18 17:06 dir1
-rw-r--r-- 1 root root    0 2017-04-18 17:06 file1

/test/dir1:
합계 0
-rw-r--r-- 1 root root 0 2017-04-18 17:06 file2

 

* [root@CentOS /root]# ls -al
합계 400
dr-xr-x---. 33 root root   4096 2017-04-18 15:24 .
drwxr-xr-x. 26 root root   4096 2017-04-18 17:05 ..
- 중간생략

-rw-r--r--   1 root root     15 2017-04-18 15:08 file4
-rw-r--r--   1 root root     10 2017-04-18 15:08 file5
-rw-r--r--.  1 root root  46035 2017-04-17 22:31 install.log
-rw-r--r--.  1 root root  10033 2017-04-17 22:29 install.log.syslog
-rwxr-xr-x   1 root root 117024 2017-04-17 16:59 newls
drwxr-xr-x.  2 root root   4096 2017-04-17 22:35 공개
drwxr-xr-x.  2 root root   4096 2017-04-17 22:35 다운로드
drwxr-xr-x.  2 root root   4096 2017-04-17 22:35 문서
drwxr-xr-x.  2 root root   4096 2017-04-17 22:35 바탕화면
drwxr-xr-x.  2 root root   4096 2017-04-17 22:35 비디오
drwxr-xr-x.  2 root root   4096 2017-04-17 22:35 사진
drwxr-xr-x.  2 root root   4096 2017-04-17 22:35 음악
drwxr-xr-x.  2 root root   4096 2017-04-17 22:35 템플릿

ls -F 명령어
[root@CentOS /root]# cd /test
[root@CentOS /test]# rm -rf *
[root@CentOS /test]# cp /etc/passwd file1
[root@CentOS /test]# ln -s file1 file2
[root@CentOS /test]# cp /bin/ls file3
[root@CentOS /test]# mkdir dir1
[root@CentOS /test]# ls -F
dir1/  file1  file2@  file3*
----------------------------------------------------------------------------------------------
*alias  : 단축 명령어라고 생각하면 됨

alias lsf='ls -l | grep "^-"'
alias lsd='ls -l | grep "^d"'

[root@CentOS /root]# lsf
-rw-------. 1 root root   1594 2017-04-17 22:31 anaconda-ks.cfg
-rw-r--r--  1 root root     15 2017-04-18 15:06 file1
-rw-r--r--  1 root root     15 2017-04-18 15:06 file2
-rw-r--r--  1 root root     15 2017-04-18 15:07 file3
-rw-r--r--  1 root root     15 2017-04-18 15:08 file4
-rw-r--r--  1 root root     10 2017-04-18 15:08 file5
-rw-r--r--. 1 root root  46035 2017-04-17 22:31 install.log
-rw-r--r--. 1 root root  10033 2017-04-17 22:29 install.log.syslog
-rwxr-xr-x  1 root root 117024 2017-04-17 16:59 newls

[root@CentOS /root]# lsd
drwxr-xr-x  2 root root   4096 2017-04-18 15:24 dir1
drwxr-xr-x  2 root root   4096 2017-04-18 15:24 dir2
drwxr-xr-x. 2 root root   4096 2017-04-17 22:35 공개
drwxr-xr-x. 2 root root   4096 2017-04-17 22:35 다운로드
drwxr-xr-x. 2 root root   4096 2017-04-17 22:35 문서
drwxr-xr-x. 2 root root   4096 2017-04-17 22:35 바탕화면
drwxr-xr-x. 2 root root   4096 2017-04-17 22:35 비디오
drwxr-xr-x. 2 root root   4096 2017-04-17 22:35 사진
drwxr-xr-x. 2 root root   4096 2017-04-17 22:35 음악
drwxr-xr-x. 2 root root   4096 2017-04-17 22:35 템플릿

----------------------------------------------------------------------------------------------
mkdir 명령어 : 디렉토리 생성

[root@CentOS /root]# cd /test
[root@CentOS /test]# rm -rf /test/*
[root@CentOS /test]# mkdir dir5  -> 디렉토리 dir5 생성
[root@CentOS /test]# ls
dir5
[root@CentOS /test]# mkdir dir6 dir7
[root@CentOS /test]# ls
dir5 dir6 dir7


참고
rm -f
?강제 삭제
?파일을 지우기 전에 물어보지 않도록 함

rm -r
?재귀적 삭제
?디렉토리 삭제 가능
?하위 폴더/파일을 모두 삭제함


[root@CentOS /test]# mkdir -p dir4/dir2/dir1 
 ---> 하위디렉토리까지 만들려면 p옵션을 사용한다.


----------------------------------------------------------------------------------------------
rmdir : 디렉토리 삭제


[root@CentOS /test]# ls
dir4 dir5 dir6 dir7
[root@CentOS /test]# rmdir dir5   ---> 디렉토리 제거
[root@CentOS /test]# rmdir dir6 dir7
[root@CentOS /test]# ls
dir4
[root@CentOS /test]# rmdir dir4
rmdir: failed to remove `dir4': 디렉터리가 비어있지 않음
[root@CentOS /test]# rmdir -p dir4/dir2/dir1
[root@CentOS /test]# ls
[root@CentOS /test]# mkdir -p dir4/dir2/dir1
[root@CentOS /test]# rm -rf dir4/*
[root@CentOS /test]# ls
dir4

 

----------------------------------------------------------------------------------------------
touch 명령어 : 파일 생성


[root@CentOS /test]# cp -p /etc/passwd file2
[root@CentOS /test]# ls -l
합계 4
-rw-r--r--. 1 root root 0 2015-12-23 10:22 file1
-rw-r--r--. 1 root root 1816 2015-12-21 15:10 file2


[root@CentOS /test]# touch file2  ----> file2에 현재 시간으로 동기화
[root@CentOS /test]# ls -l
합계 4
-rw-r--r--. 1 root root 0 2015-12-23 10:22 file1
-rw-r--r--. 1 root root 1816 2015-12-23 10:23 file2
[root@CentOS /test]# date
2015. 12. 23. (수) 10:23:58 KST

[root@CentOS /test]# touch -t 12151400 file2 ---> file2에 시간을 t옵션으로 수동 셋팅
[root@CentOS /test]# ls -l file2
-rw-r--r-- 1 root root 1751 2017-12-15 14:00 file2

 

----------------------------------------------------------------------------------------------

'cp' 명령어 : cp -r’ 옵션을 이용한 하위 디렉토리까지 복사 실시

 

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


[root@CentOS /test]#ls -lR
.:
합계 8
drwxr-xr-x 2 root root 4096 2017-04-18 18:00 dir1
drwxr-xr-x 2 root root 4096 2017-04-18 18:01 dir2

./dir1:
합계 0
-rw-r--r-- 1 root root 0 2017-04-18 18:00 file1    ---> dir1  디렉토리에 file1 , file2
-rw-r--r-- 1 root root 0 2017-04-18 18:00 file2

./dir2:
합계 0
-rw-r--r-- 1 root root 0 2017-04-18 18:01 file1  ---> dir2  디렉토리에 file1 , file2
-rw-r--r-- 1 root root 0 2017-04-18 18:01 file2


├── dir1
│?? ├── file1
│?? └── file2
└── dir2
    ├── file1
    └── file2


[root@CentOS /test]#cp -r dir1 dir2  ---> dir1을 dir2에 복사됨
[root@CentOS /test]#tree
.
├── dir1
│?? ├── file1
│?? └── file2
└── dir2
    ├── dir1
    │?? ├── file1
    │?? └── file2
    ├── file1
    └── file2


[root@CentOS /test]#rm -rf *
[root@CentOS /test]#mkdir dir1
[root@CentOS /test]#echo "linux200" > file1
[root@CentOS /test]#cat file1
linux200
[root@CentOS /test]#
[root@CentOS /test]#touch dir1/file1
[root@CentOS /test]#cat dir1/file1
[root@CentOS /test]#cp file1 dir1  ----- test 디렉토리에 있는 file1을 dir1로 복사.....
dir1에 file1이 있기 때문에 덮어씌우기할거냐고 질의된다.
cp: overwrite `dir1/file1'? y


[root@CentOS /test]#ls -lR
.:
합계 4
drwxr-xr-x 2 root root 4096 2017-04-18 18:12 dir1  ----> test 디렉토리에 dir1과 file1이 있다.
-rw-r--r-- 1 root root    0 2017-04-18 18:13 file1

./dir1:  ---? dir1 디렉토리에 file1이 있다.
합계 4
-rw-r--r-- 1 root root 9 2017-04-18 18:13 file1 

 


[root@CentOS /test]#cd /test
[root@CentOS /test]#rm -rf /test/*
[root@CentOS /test]#
[root@CentOS /test]#
[root@CentOS /test]#touch file1
[root@CentOS /test]#ls -l file1
-rw-r--r-- 1 root root 0 2017-04-18 18:16 file1
[root@CentOS /test]#
[root@CentOS /test]#chmod 777 file1
[root@CentOS /test]#ls -l
합계 0
-rwxrwxrwx 1 root root 0 2017-04-18 18:16 file1
[root@CentOS /test]#
[root@CentOS /test]#
[root@CentOS /test]#cp file1 file2
[root@CentOS /test]#ls -l
합계 0
-rwxrwxrwx 1 root root 0 2017-04-18 18:16 file1
-rwxr-xr-x 1 root root 0 2017-04-18 18:18 file2 -----> 그냥 cp로 복사하면 파일허가권이 설정한값이 들어가지 않는다.
[root@CentOS /test]#
[root@CentOS /test]#
[root@CentOS /test]#cp -p file1 file3  --->
[root@CentOS /test]#ls -l
합계 0
-rwxrwxrwx 1 root root 0 2017-04-18 18:16 file1
-rwxr-xr-x 1 root root 0 2017-04-18 18:18 file2
-rwxrwxrwx 1 root root 0 2017-04-18 18:16 file3

 


----------------------------------------------------------------------------------------------

mv 명령어 : 파일 또는 디렉토리를 이동 ,변경하고자 할때 명령어를 사용


[root@CentOS /]#mkdir test
[root@CentOS /]#cd test
[root@CentOS /test]#mkdir dir1
[root@CentOS /test]#touch file1
[root@CentOS /test]#touch dir1/file2
[root@CentOS /test]#ls -Rl
.:
합계 4
drwxr-xr-x 2 root root 4096 2017-04-18 20:29 dir1
-rw-r--r-- 1 root root    0 2017-04-18 20:29 file1

./dir1:
합계 0
-rw-r--r-- 1 root root 0 2017-04-18 20:29 file2
[root@CentOS /test]#mv file1 file3
[root@CentOS /test]#ls -l
합계 4
drwxr-xr-x 2 root root 4096 2017-04-18 20:29 dir1
-rw-r--r-- 1 root root    0 2017-04-18 20:29 file3
[root@CentOS /test]#tree
.
├── dir1
│   └── file2
└── file3

1 directory, 2 files
[root@CentOS /test]#mv file3 dir1
[root@CentOS /test]#ls dir1
file2  file3
[root@CentOS /test]#tree
.
└── dir1
    ├── file2
    └── file3

1 directory, 2 files
[root@CentOS /test]#clear
[root@CentOS /test]#rm -rf /test/*
[root@CentOS /test]#mkdir dir1
[root@CentOS /test]#touch file1
[root@CentOS /test]#touch dir1/file2
[root@CentOS /test]#ls
dir1  file1
[root@CentOS /test]#ls -Rl
.:
합계 4
drwxr-xr-x 2 root root 4096 2017-04-18 20:35 dir1
-rw-r--r-- 1 root root    0 2017-04-18 20:35 file1

./dir1:
합계 0
-rw-r--r-- 1 root root 0 2017-04-18 20:35 file2
[root@CentOS /test]#tree
.
├── dir1
│   └── file2
└── file1

1 directory, 2 files
[root@CentOS /test]#mv file1 file3
[root@CentOS /test]#ls
dir1  file3
[root@CentOS /test]#tree
.
├── dir1
│   └── file2
└── file3

1 directory, 2 files
[root@CentOS /test]#mv file3 dir1
[root@CentOS /test]#ls -Rl
.:
합계 4
drwxr-xr-x 2 root root 4096 2017-04-18 20:37 dir1

./dir1:
합계 0
-rw-r--r-- 1 root root 0 2017-04-18 20:35 file2
-rw-r--r-- 1 root root 0 2017-04-18 20:35 file3
[root@CentOS /test]#tree
.
└── dir1
    ├── file2
    └── file3

1 directory, 2 files
[root@CentOS /test]#mv dir1 dir2
[root@CentOS /test]#ls
dir2
[root@CentOS /test]#tree
.
└── dir2
    ├── file2
    └── file3

1 directory, 2 files
[root@CentOS /test]#cp - r dir2 dir1
cp: target `dir1' is not a directory
[root@CentOS /test]#cp -r dir2 dir1
[root@CentOS /test]#ls -Rl
.:
합계 8
drwxr-xr-x 2 root root 4096 2017-04-18 20:38 dir1
drwxr-xr-x 2 root root 4096 2017-04-18 20:37 dir2

./dir1:
합계 0
-rw-r--r-- 1 root root 0 2017-04-18 20:38 file2
-rw-r--r-- 1 root root 0 2017-04-18 20:38 file3

./dir2:
합계 0
-rw-r--r-- 1 root root 0 2017-04-18 20:35 file2
-rw-r--r-- 1 root root 0 2017-04-18 20:35 file3
[root@CentOS /test]#tree
.
├── dir1
│   ├── file2
│   └── file3
└── dir2
    ├── file2
    └── file3

2 directories, 4 files
[root@CentOS /test]#mv dir1 dir2
[root@CentOS /test]#ls
dir2
[root@CentOS /test]#ls -Rl
.:
합계 4
drwxr-xr-x 3 root root 4096 2017-04-18 20:40 dir2

./dir2:
합계 4
drwxr-xr-x 2 root root 4096 2017-04-18 20:38 dir1
-rw-r--r-- 1 root root    0 2017-04-18 20:35 file2
-rw-r--r-- 1 root root    0 2017-04-18 20:35 file3

./dir2/dir1:
합계 0
-rw-r--r-- 1 root root 0 2017-04-18 20:38 file2
-rw-r--r-- 1 root root 0 2017-04-18 20:38 file3
[root@CentOS /test]#tree
.
└── dir2
    ├── dir1
    │   ├── file2
    │   └── file3
    ├── file2
    └── file3

2 directories, 4 files
[root@CentOS /test]#ls dir2
dir1  file2  file3

파일여러개 이동

[root@CentOS /test]#cd /test
[root@CentOS /test]#touch file{1,2,3} test{1,2,3}
[root@CentOS /test]#ls
dir2  file1  file2  file3  test1  test2  test3
[root@CentOS /test]#mkdir dir1
[root@CentOS /test]#mv file* test* dir1
[root@CentOS /test]#ls dir1
file1  file2  file3  test1  test2  test3

 

 


----------------------------------------------------------------------------------------------
'rm' 명령어
‘rm’은 파일이나 디렉토리를 삭제할 때 사용하는 명령어이다. 단, 디렉토리 삭제인 경우 ‘-r’ 옵션을 사용해야
한다.


[root@CentOS /test]#mkdir dir1
[root@CentOS /test]#touch dir1/file{1,2}
[root@CentOS /test]#ls dir1
file1  file2
[root@CentOS /test]#ls -Rl
.:
합계 4
drwxr-xr-x 2 root root 4096 2017-04-18 20:56 dir1

./dir1:
합계 0
-rw-r--r-- 1 root root 0 2017-04-18 20:56 file1
-rw-r--r-- 1 root root 0 2017-04-18 20:56 file2
[root@CentOS /test]#tree
.
└── dir1
    ├── file1
    └── file2

1 directory, 2 files
[root@CentOS /test]#rm -r dir1
rm: descend into directory `dir1'? y
rm: remove 일반 빈 파일 `dir1/file2'? y
rm: remove 일반 빈 파일 `dir1/file1'? y
rm: remove 디렉토리 `dir1'? y


----------------------------------------------------------------------------------------------

 


cat  명령어 : 파일 내용을 볼때 사용하며 옵션 n은 줄 수를 나타낸다.


[root@CentOS /test/test]#cat -n /etc/passwd
     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
     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
     9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10  uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
    11  operator:x:11:0:operator:/root:/sbin/nologin
    12  games:x:12:100:games:/usr/games:/sbin/nologin
    13  gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
    14  ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    15  nobody:x:99:99:Nobody:/:/sbin/nologin
    16  dbus:x:81:81:System message bus:/:/sbin/nologin
    17  usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
    18  vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
    19  rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
    20  rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin

[root@CentOS /test/test]#echo ABC > file1
[root@CentOS /test/test]#echo DEF > file2
[root@CentOS /test/test]#cat file1 file2
ABC
DEF
[root@CentOS /test/test]#cat file1 file2 > file3
[root@CentOS /test/test]#cat file3
ABC
DEF
[root@CentOS /test/test]#ls
file1  file2  file3
[root@CentOS /test/test]#cat file3
ABC
DEF

----------------------------------------------------------------------------------------------
more 명령어 : 길게 출력되는 내용을 한페이지씩 출력해주는 명령어


ex) more /etc/services --> 맨밑에 more라고 표시 , spacebar를 누르면 한페이지씩, enter를 누르면 한줄씩
    cat /etc/services | more


----------------------------------------------------------------------------------------------
less 명령어


less' 명령어
‘more’ 명령어와 비슷하지만, 추가적인 기능이 포함된 명령어이다. 화살표키로 줄이동이 가능하다.

[root@CentOS /test]# less -2 /etc/passwd // 첫 화면은 다 출력되고, 이후 2 줄씩 출력


[root@CentOS /test]# less /etc/passwd /etc/shadow
/etc/passwd (file 1 of 2) <--------- :n 실시 (다음 페이지 이동)
/etc/shadow (file 2 of 2) <--------- :e 실시 (확인할 새로운 파일 추가)
Examine: /etc/hosts <------------- : 입력 (확인할 파일 입력)
/etc/hosts (file 3 of 3) (END) <----- :p 실시 (이전 페이지 이동)
/etc/shadow (file 2 of 3) <--------- : q 실시 (나가기)


----------------------------------------------------------------------------------------------
nl 명령어 : cat -n과 동일


[root@CentOS ~]# nl /etc/passwd
     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
     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
     9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10  uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
    11  operator:x:11:0:operator:/root:/sbin/nologin

----------------------------------------------------------------------------------------------
head 명령어 : 기본적으로 10줄 출력


head /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 ~]# head -c 10 /etc/passwd
root:x:0:0


[root@CentOS ~]# ps -ef | head -1 ; ps -ef | grep gnome
UID        PID  PPID  C STIME TTY          TIME CMD
root      2435  2363  0 20:18 ?        00:00:00 /usr/libexec/gdm-simple-slave --display-id /org/gnome/DisplayManager/Display1
gdm       2527  2435  0 20:18 ?        00:00:00 /usr/bin/gnome-session --autostart=/usr/share/gdm/autostart/LoginWindow/
gdm       2554     1  0 20:18 ?        00:00:00 /usr/libexec/gnome-settings-daemon --gconf-prefix=/apps/gdm/simple-greeter/settings-manager-plugins
gdm       2568  2527  0 20:19 ?        00:00:00 /usr/libexec/polkit-gnome-authentication-agent-1
gdm       2571  2527  0 20:19 ?        00:00:00 gnome-power-manager
root     27367  3082  0 21:37 pts/0    00:00:00 grep gnome


[root@CentOS ~]# alias pps='ps -ef | head -1 ; ps -ef | grep $1'
[root@CentOS ~]# pps syslogd
UID        PID  PPID  C STIME TTY          TIME CMD
root      1687     1  0 20:18 ?        00:00:00 /sbin/rsyslogd -i /var/run/syslogd.pid -c 5
root     27382  3082  0 21:39 pts/0    00:00:00 grep syslogd

[root@CentOS ~]# pps inetd
UID        PID  PPID  C STIME TTY          TIME CMD
root     27391  3082  0 21:39 pts/0    00:00:00 grep inetd


----------------------------------------------------------------------------------------------
tail 명령어


[root@CentOS /test]# tail /etc/passwd // 끝에서부터 기본적으로 10 번째줄까지 출력
[root@CentOS /test]# tail -n 5 /etc/passwd // 끝에서부터 ‘-n’ 옵션 값만큼 라인 출력
[root@CentOS /test]# tail -c 10 /etc/passwd // 끝에서부터 ‘-c’ 옵션 값만큼 라인 출력
/bin/bash
[root@CentOS /test]# tail -c 20 /etc/passwd
/testuser:/


[root@CentOS ~]# cp /etc/passwd /test/filename

ls
filename  test

 

[root@CentOS test]# head -20 filename | tail -10
operator:x:11:0:operator:/root:/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
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin

 

nl filename | head -20
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
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
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
11 operator:x:11:0:operator:/root:/sbin/nologin
12 games:x:12:100:games:/usr/games:/sbin/nologin
13 gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
14 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
15 nobody:x:99:99:Nobody:/:/sbin/nologin
16 dbus:x:81:81:System message bus:/:/sbin/nologin
17 usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
18 vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
19 rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
20 rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin
[root@CentOS /test]# nl filename | head -20 | tail -10
11 operator:x:11:0:operator:/root:/sbin/nologin
12 games:x:12:100:games:/usr/games:/sbin/nologin
13 gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
14 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
15 nobody:x:99:99:Nobody:/:/sbin/nologin
16 dbus:x:81:81:System message bus:/:/sbin/nologin
17 usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
18 vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

19 rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
20 rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin


----------------------------------------------------------------------------------------------
wc 명령어


 wc /etc/passwd
  33   52 1576 /etc/passwd
 라인  단어 글자갯수

wc -l /etc/passwd
33 /etc/passwd  --> 라인 갯수

 wc -w /etc/passwd  ---> 단어 갯수
52 /etc/passwd

 

 wc -l : 라인
 wc -w : 단어


*실행중 프로세스 갯수 확인
ps -ef | wc -l
130

*설치된 패키지 갯수 확인
 rpm -qa | wc -l
1124

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

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

*디스크 사용량 모니터링
df-k /dev/sda1 | tail -1 | awk '{print $5}'
df -k /dev/sda1 | tail -1 | awk '{print $5}' > df.count

cat df.count
7%

 


----------------------------------------------------------------------------------------------

'su', 'su –' 명령어
‘su’, ‘su –‘ 명령어는 로그아웃 없이 임시로 다른 사용자 계정 환경을 사용하는 명령어이다. ‘su user1’을
실행하면 '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  ->> 이전 사용자 root계정의
환경변수 설정이 계승된다.

 


[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
[
----------------------------------------------------------------------------------------------

id & group 명령어


id’는 현재 사용자의 실제 id, 유효 사용자 id, 그룹 id 를 확인하는 명령어이다.

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


'groups' 명령어
‘groups’는 현재 사용자가 속해있는 그룹, 또는 특정 사용자가 속해는 그룹을 확인하는 명령어이다


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

cat /etc/group  순서 (그룹명,그룹패스워드, GID)
root:x:0:
bin:x:1:bin,daemon
daemon:x:2:bin,daemon
sys:x:3:bin,adm
adm:x:4:adm,daemon
tty:x:5:
disk:x:6:
lp:x:7:daemon

----------------------------------------------------------------------------------------------


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 일 이내에 접속한 마지막 사용자 로그인 정보만 출력해줌

----------------------------------------------------------------------------------------------


lastb 명령어 : 접속 실패 기록 정보를 출력하는 명령어이다.

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]# 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
----------------------------------------------------------------------------------------------


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 현재 접속한 사용자 로그인명만 보여줌


'who' 원격 접속 정보 확인
-'putty' 새로 열기 -> 'user1'로 접속 실시
login as: user1
user1@192.168.35.130's password: centos
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 명령어 : w’는 해당 시스템에 로그인한 사용자가 실행한 작업 정보들을 출력하는 명령어이다.


user1 에서 'vi /etc/passwd' 실행
[user1@CentOS ~]$ vi /etc/passwd
- '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]# ps -fu user1
UID        PID  PPID  C STIME TTY          TIME CMD
user1    27912 27908  0 22:32 ?        00:00:00 sshd: user1@pts/1
user1    27913 27912  0 22:32 pts/1    00:00:00 -bash
user1    27950 27913  0 22:34 pts/1    00:00:00 vim /etc/passwd

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

[root@CentOS /test]# ps -fu user1
UID        PID  PPID  C STIME TTY          TIME CMD
user1    27912 27908  0 22:32 ?        00:00:00 sshd: user1@pts/1
user1    27913 27912  0 22:32 pts/1    00:00:00 -bash

[root@CentOS /test]# w
 22:37:28 up  2:19,  2 users,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.2.1      21:07    0.00s  0.81s  0.00s w
user1    pts/1    192.168.2.1      22:32    2:57   0.02s  0.02s -bash
[root@CentOS /test]#


----------------------------------------------------------------------------------------------


cmf & diff 명령어 : cmp’, ‘diff’는 서로 다른 파일을 비교하여 다른 내용을 출력하는 명령어이다.


[root@CentOS /test]# cmp file1 file2
[root@CentOS /test]# cmp file1 file3
file1 file3 differ: byte 11, line 3
[root@CentOS /test]#
[root@CentOS /test]# cat > file2
1111
2222
3333
[root@CentOS /test]# cat > file3
1111
2222
4444

[root@CentOS /test]# cmp file1 file2
[root@CentOS /test]# cmp file1 file3
file1 file3 differ: byte 11, line 3
[root@CentOS /test]# diff file1 file2
[root@CentOS /test]# diff file1 file3
3c3
< 3333
---
> 4444

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


----------------------------------------------------------------------------------------------


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 정렬할 필드 지정


[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


----------------------------------------------------------------------------------------------


file 명령어 : 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 // 디렉토리


----------------------------------------------------------------------------------------------


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


----------------------------------------------------------------------------------------------


whereis 명령어


whereis’는 $PATH 변수와 관계 없이 대부분의 명령어를 검색하는 명령어이다.
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

----------------------------------------------------------------------------------------------

 

which 명령어

which’는 $PATH 변수에 설정된 명령어 및 alias 설정된 명령어를 검색 및 절대경로를 알려준다.


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


----------------------------------------------------------------------------------------------

 

find 명령어


----------------------------------------------------------------------------------------------
[root@CentOS /test]#cat /etc/passwd /etc/shadow | grep ^root
root:x:0:0:root:/root:/bin/bash
root:$6$Xzs3yZV15dtZc1bZ$TqzUePpL260ihpRgvaNRV9dJBOz6zpvTo/JKkUp3.bQIPd/evgKlFYPg.HXxegfGTp0zkGxDRRj73tWNayPLV/:17273:0:99999:7:::

 

[root@CentOS /test]#cat -n filename | head -20 | tail -10
    11  operator:x:11:0:operator:/root:/sbin/nologin
    12  games:x:12:100:games:/usr/games:/sbin/nologin
    13  gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
    14  ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    15  nobody:x:99:99:Nobody:/:/sbin/nologin
    16  dbus:x:81:81:System message bus:/:/sbin/nologin
    17  usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
    18  vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
    19  rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
    20  rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin

more 명령어

키[편집]
?q: 종료 ★
?Ctrl+C: 종료
?? Enter: 다음 줄
?Space Bar: 다음 페이지


실습 1: 파일보기[편집]

[root@zetawiki ~]# more /etc/protocols
... (생략)
trunk-1 23 TRUNK-1  # Trunk-1
trunk-2 24 TRUNK-2  # Trunk-2
leaf-1 25 LEAF-1  # Leaf-1
leaf-2 26 LEAF-2  # Leaf-2
rdp 27 RDP  # "reliable datagram" protocol
--More--(23%)


[root@zetawiki ~]# cat /etc/protocols | more
... (생략)
trunk-1 23 TRUNK-1  # Trunk-1
trunk-2 24 TRUNK-2  # Trunk-2
leaf-1 25 LEAF-1  # Leaf-1
leaf-2 26 LEAF-2  # Leaf-2
rdp 27 RDP  # "reliable datagram" protocol

less 명령어

 

?리눅스용 텍스트 파일 뷰어
? vi와 유사하지만 읽기전용
vi처럼 정규식 검색 가능? more와 유사하지만 위/아래로 이동 가능 ★[1]
? 큰 파일을 열 때도 빠름 ★★
? gzip 압축파일도 열 수 있음[2] ★
? 터미널 창 높이에 맞추어 출력됨

키 입력[편집]
?q: 종료 ★
?? Enter: 아래로 1 이동
?Space Bar: 아래로 1페이지 이동
?↑: 위로 1행 이동
?↓: 아래로 1행 이동
?Ctrl+f: 아래로 1페이지 이동
?Ctrl+b: 위로 1페이지 이동
?PageUp: 위로 1페이지 이동
?PageDn: 아래로 1페이지 이동

실습 1: 파일보기[편집]

[root@zetawiki ~]# less /etc/protocols
... (생략)
trunk-1 23      TRUNK-1         # Trunk-1
trunk-2 24      TRUNK-2         # Trunk-2
leaf-1  25      LEAF-1          # Leaf-1
leaf-2  26      LEAF-2          # Leaf-2
rdp     27      RDP             # "reliable datagram" protocol
/etc/protocols


[root@zetawiki ~]# cat /etc/protocols | less
... (생략)
trunk-1 23      TRUNK-1         # Trunk-1
trunk-2 24      TRUNK-2         # Trunk-2
leaf-1  25      LEAF-1          # Leaf-1
leaf-2  26      LEAF-2          # Leaf-2
rdp     27      RDP             # "reliable datagram" protocol


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

196개

[root@CentOS /]#rpm -qa | wc -l
1126개

[root@CentOS /]#wc /etc/passwd
  37   56 1751 /etc/passwd

라인  단어 글자갯수


w/usr/bin/w

?누가 로그인했고 뭘하고 있는지 보여줌


[root@CentOS test]# grep ^root -n /etc/passwd
1:root:x:0:0:root:/root:/bin/bash


rpm -qa | grep sendmail
sendmail-8.14.4-9.el6_8.1.x86_64


[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]# grep user1 /etc/passwd
user1:x:500:500::/home/user1:/bin/bash

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

 

date 012212362016
2016. 01. 22. (금) 12:36:00 KST