본문 바로가기

CentOS/Study

[리눅스]01. 08 교육 정리

반응형

1교시 09:30 ~ 10:30>
실습 추천 순서>
VM 생성 DHCP
OVF > Clone
static IP config > Snapshot

2교시 10:40 ~ 11:40>
시스템 지역변수>
[root@server1 ~]# echo $PS1
[\u@\h \W]\$
\u   : username
\h   : hostname
\W   : working directory (예: /home/user01 -> user01)
\w   : working directory (예: /home/user01 -> /home/user01)
\$   : root(#), user01($)

- PS1 시스템 환경 변수 설정하는 방법
- PS2 명령어가 아직 끝나지 않았음을 나타낼 때 사용

[root@server1 ~]# echo $$
1932
[root@server1 ~]# ps
    PID TTY          TIME CMD
   1932 pts/0    00:00:00 bash
   2773 pts/0    00:00:00 ps
[root@server1 ~]# bash
[root@server1 ~]# echo $$
2774
[root@server1 ~]# ps
    PID TTY          TIME CMD
   1932 pts/0    00:00:00 bash
   2774 pts/0    00:00:00 bash
   2798 pts/0    00:00:00 ps
[root@server1 ~]#

[root@server1 ~]# ls
anaconda-ks.cfg  Desktop  Documents  Downloads  initial-setup-ks.cfg  Music  Pictures  Public  Templates  test  Videos
[root@server1 ~]# echo $?
0
[root@server1 ~]# ls /nodir
ls: cannot access '/nodir': No such file or directory
[root@server1 ~]# echo $?
2
[root@server1 ~]#

메타캐릭터

쉘은 특수문자를 특정한 기능으로 이해를 한다.
''(작은 따옴표(single quotation))
""(큰 따옴표(Double quotation))
``(역 따옴표(Back quotation))
\ (역 슬래쉬(Back slash))
; (세미콜론(Semicolon))

[root@server1 test]# echo '$HOME'
$HOME

[root@server1 test]# echo "=======$HOME======="
=======/root=======

[root@server1 test]# echo '=======`date`========'
=======`date`========
[root@server1 test]# echo "=======`date`========"
=======Sat Jan  8 11:01:06 KST 2022========
[root@server1 test]# echo "=====$(date)===="
=====Sat Jan  8 11:02:29 KST 2022====

중첩 쉘을 실행을 해야 하는 경우 -> $(CMD)
단발성으로 사용 `CMD`

[root@server1 test]# ls ; date
a1  a2  a3  a4  a5  a7  file10  file.log
Sat Jan  8 11:09:06 KST 2022
[root@server1 test]# ls date
ls: cannot access 'date': No such file or directory

[root@server1 test2]# cd /nodir && echo 'USE:mkdir /nodir'
bash: cd: /nodir: No such file or directory
[root@server1 test2]# cd /nodir || echo 'USE:mkdir /nodir'
bash: cd: /nodir: No such file or directory
USE:mkdir /nodir
[root@server1 test2]#

; 의 위험성
&& 앞 명령어가 성공($? == 0)일때만 뒤의 명령어를 실행
|| 앞 명령어가 실패($? != 0)일때만 뒤의 명령어를 실행

3교시 11:50 ~ 12:50>
히스토리(HISTORY)
쉘로 로그인>
.bash_history > 로드

history -c
메모리상에 있는 히스토리를 삭제

명령어 입력 < 메모리
쉘을 정상적으로 빠져나가는 작업

패스워드 변경 등 보안상 작업은 bash 를 하나 더 열고 작업 하는 것을 추천한다.
[root@server1 test2]# bash
[root@server1 test2]# echo 'team01' | passwd --stdin team01
Changing password for user team01.
passwd: all authentication tokens updated successfully.
[root@server1 test2]# exit
[root@server1 test2]# history
    1  history
    2  bash
    3  history
[root@server1 test2]#

HISTTIMEFORMAT 변수

.bash_history
export HISTTIMEFORMAT="%F %T     "

[root@server1 ~]# history | grep '11:54'
  343  2022-01-08 11:54:49      cd
  344  2022-01-08 11:54:58      cat .bash_history
  347  2022-01-08 11:56:30      histroy | grep '11:54'
  348  2022-01-08 11:56:47      history | grep '11:54'
[root@server1 ~]#

alias(별칭)
[root@server1 ~]# command ls
anaconda-ks.cfg  Desktop  Documents  Downloads  initial-setup-ks.cfg  Music  Pictures  Public  Templates  test  Videos
[root@server1 ~]# ls
anaconda-ks.cfg  Desktop  Documents  Downloads  initial-setup-ks.cfg  Music  Pictures  Public  Templates  test  Videos
[root@server1 ~]#

[root@server1 ~]# type cd
cd is a shell builtin
[root@server1 ~]# type ls
ls is aliased to `ls --color=auto'
[root@server1 ~]#

[root@server1 ~]# pushd /etc
/etc ~
[root@server1 etc]# pwd
/etc

4교시 11:50 ~ 13:30>
환경 파일>
/etc/profile
~/.bash_profile
~/.bashrc

유용한 명령어>
[root@server1 ~]# diff file1 file2/* 두 개의 파일에 대한 간략한 차이점 */
[root@server1 ~]# diff -c file1 file2 /* 두 개의 파일에 대한 자세한 차이점 확인 */
[root@server1 ~]# diff -i file1 file2 /* -i : 대소문자를 구분하지 말아라. A와 a는 같은 것으로 간주 */

[root@server1 test]# cmp file1 file2
[root@server1 test]# cmp file1 file3
file1 file3 differ: byte 1, line 1
[root@server1 test]# diff file1 file2
[root@server1 test]# diff file1 file3
1,6c1,3
< 11111
< 2222
< 33333
<
<
<
---
> 4
> 22
> 1

5교시 14:30 ~ 15:30>
sort 정렬명령어
-t : 필드 구분자를 지정한다. (기본값은 공백이 기준이 된다)
-k : 정렬할 필드를 지정한다.

- 오름차순 정열                 <---> (-r) 내림차순 정렬
  - 1필드->2필드->3필드-> ... 정열 <---> (-k) 필드 지정 정열
  - 문자열 정열                  <---> (-n) 숫자열 정열
  
필드 구분자 : :
fedora:x:1003:1003::/home/fedora:/bin/bash

# cat /test/file1 | sort -n | uniq

find / -inum inode번호
find / -inum inode번호 -exec rm -rf {} \;

file 명령어>
Windows -> 파일에 확장자(예: file.txt, hwp.exe, ....)
Unix,Linux -> 파일의 확장자가 거의 의미가 없다.

uniq : 동일한 값은 부어있어야 한다.

본인이 직접 만든 파일이 아닌 경우, 외부네트워크를 통해서 다운로드 받은 파일의 경우 무조건 file 명령어로 확인 해야 한다.

wall 접속중인 모든 터미널에 동시 전송

6교시 15:40 ~ 16:40>
grep 파일 내에서 특정한 패턴을 검색하여 그 패턴을 포함하는 모든 줄을 화면에 출력하는 명령어

[root@server1 ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

[root@server1 ~]# cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

[root@server1 ~]# ps -ef | grep httpd
root       64320    8642  0 15:40 pts/0    00:00:00 grep --color=auto httpd
[root@server1 ~]#

[root@server1 ~]# cat /etc/passwd | grep -n root
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin

[root@server1 ~]# ps -f | grep -v ps
UID          PID    PPID  C STIME TTY          TIME CMD
root        8642    8637  0 11:53 pts/0    00:00:01 -bash
root       64358    8642  0 15:43 pts/0    00:00:00 sleep 1000

[root@server1 ~]# ps -ef | grep bash | grep -v grep
root         986       1  0 09:26 ?        00:00:00 /bin/bash /usr/sbin/ksmtuned
root        8642    8637  0 11:53 pts/0    00:00:01 -bash

[root@server1 ~]# cat /etc/ssh/sshd_config | grep -i permit
PermitRootLogin yes
#PermitEmptyPasswords no
# the setting of "PermitRootLogin without-password".
#PermitTTY yes
#PermitUserEnvironment no
#PermitTunnel no
#       PermitTTY no

[root@server1 ~]# grep -lr ens33 /etc
/etc/sysconfig/network-scripts/ifcfg-ens33

[root@server1 ~]# cat /etc/passwd | grep -E '(root|team01)'
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
team01:x:1000:1000:team01:/home/team01:/bin/bash
[1]+  Done                    sleep 1000

[root@server1 ~]# cat file1 | grep -F ^root
^root

7교시 16:50 ~ 17:50>
find 디렉토리안에서 원하는 파일을 찾고자 할 때 사용하는 명령어
(형식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 {} \;

[root@server1 test]# locate green
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory
[root@server1 test]# updatedb
[root@server1 test]# locate green
/test/green
/usr/share/doc/source-highlight/sh_greenlcd.css
/usr/share/source-highlight/sh_greenlcd.css
/usr/src/kernels/4.18.0-348.7.1.el8_5.x86_64/include/config/hid/greenasia.h
[root@server1 test]# touch green2
[root@server1 test]# locate green
/test/green
/usr/share/doc/source-highlight/sh_greenlcd.css
/usr/share/source-highlight/sh_greenlcd.css
/usr/src/kernels/4.18.0-348.7.1.el8_5.x86_64/include/config/hid/greenasia.h
[root@server1 test]# updatedb
[root@server1 test]# locate green
/test/green
/test/green2
/usr/share/doc/source-highlight/sh_greenlcd.css
/usr/share/source-highlight/sh_greenlcd.css
/usr/src/kernels/4.18.0-348.7.1.el8_5.x86_64/include/config/hid/greenasia.h
[root@server1 test]#

[root@server1 test]# alias locate='updatedb ; locate $1'
[root@server1 test]# touch green3
[root@server1 test]# locate green
/test/green
/test/green2
/test/green3
/usr/share/doc/source-highlight/sh_greenlcd.css
/usr/share/source-highlight/sh_greenlcd.css
/usr/src/kernels/4.18.0-348.7.1.el8_5.x86_64/include/config/hid/greenasia.h
[root@server1 test]#

find의 perm 옵션은
-perm ### : ###에 정확히 해당하는 권한을 검색
-perm -#00 : 권한에 -가 붙게 되면 해당 권한을 포함하는 권한을 검색
숫자에 0으로 입력되면 0은 0~7까지 모든 권한이라는 뜻을 가진다.

[root@server1 test]# find . -perm 500 -ls
 69901881      0 -r-x------   1  root     root            0 Jan  8 16:59 ./file5

[root@server1 test]# find . -perm -400 -ls
 69901824      0 drwxr-xr-x   2  root     root          110 Jan  8 16:59 .
 69901879      0 -r--------   1  root     root            0 Jan  8 16:59 ./file4
 69901881      0 -r-x------   1  root     root            0 Jan  8 16:59 ./file5
 69901882      0 -rw-------   1  root     root            0 Jan  8 16:59 ./file6
 69901884      0 -rwx------   1  root     root            0 Jan  8 16:59 ./file7
 69902915      0 -rw-r--r--   1  root     root            0 Jan  8 16:59 ./file8

[root@server1 test]# find . -perm -100
.
./file1
./file3
./file5
./file7

8교시 18:00 ~ 18:30>
[root@server1 ~]# find . -name "file1[0-9][0-9]" -exec rm -v {} \;
removed './file100'
removed './file101'
removed './file102'
removed './file103'
removed './file104'
removed './file105'
removed './file106'
removed './file107'
removed './file108'
removed './file109'
removed './file110'
[root@server1 ~]#

[root@server1 ~]# find . -name "file1" -ok -rm -v {} \;
< -rm ... ./file1 > ? 

명령어는 재검증이 필요함
[root@server1 test]# find . -name "doc*" -user team01 -type f | wc -l
10148
[root@server1 test]# find . -name "doc*" -not -user team01 -type f | wc -l
89851
[root@server1 test]# find . -name "doc*" -not -user team01 -type f -exec chown team01 {} \;

반응형

'CentOS > Study' 카테고리의 다른 글

[리눅스]01. 22 교육 정리  (0) 2022.01.23
[리눅스]01. 15 교육 정리  (0) 2022.01.16
[리눅스]12.18 교육 정리 2  (0) 2021.12.19
[리눅스]12.18 교육 정리 1  (0) 2021.12.19
[리눅스]12.04 교육 정리  (0) 2021.12.04