Data Engineering

기본적인 리눅스 명령어 정리

nijex 2022. 11. 8. 02:10

 

0. 명령어 확인

man 명령어 : 명령어 매뉴얼 확인 가능(manual)

$ man man
```
NAME
       pwd - print name of current/working directory

SYNOPSIS
       pwd [OPTION]...

DESCRIPTION
       Print the full filename of the current working directory.

       -L, --logical
              use PWD from environment, even if it contains symlinks
       
       ...
```

 

 

1. 기본 동작

 

pwd : 현재 자신이 위치한 작업 디렉토리를 확인(print working directory)

$ pwd
/home/ubuntu

 

cd 디렉토리명 : 디렉토리 이동(change directory)

# cd 디렉토리명 : 현재 위치에서 하위 디렉토리로 이동
$ cd Documents
/home/ubuntu/Documents

# cd .. : 상위 디렉토리로 이동
$ cd ..
/home/ubuntu

# cd / : 최상위 디렉토리로 이동
$ cd /
/

# cd : 홈 디렉토리로 이동
$ cd
/home/ubuntu

 

mkdir 디렉토리명 : 디렉토리 생성(make directory)

# mkdir 디렉토리명
$ mkdir dir1
$ ls
dir1

 

touch 파일명 : 빈 파일 생성

# touch 파일명
$ touch file1
$ touch file2
$ ls
dir1 file1 file2

 

ls : 현재 위치한 디렉토리의 내용을 확인

$ ls
dir1 file1 file2

# 옵션
# -l : 보다 상세한 내용 확인 (권한 소유자계정 그룹계정 byte크기 최종수정일 이름)
$ ls - l
total 4
drwxrwxr-x 2 ubuntu ubuntu 4096 Nov 11 22:04 dir1
-rw-rw-r-- 1 ubuntu ubuntu    0 Nov 11 22:06 file1
-rw-rw-r-- 1 ubuntu ubuntu    0 Nov 11 22:08 file2

# -a : 숨겨진 파일을 모두 출력
$ ls -al
total 12
drwxrwxr-x  3 ubuntu ubuntu 4096 Nov 11 22:08 .
drwxr-xr-x 21 ubuntu ubuntu 4096 Nov 11 22:04 ..
drwxrwxr-x  2 ubuntu ubuntu 4096 Nov 11 22:04 dir1
-rw-rw-r--  1 ubuntu ubuntu    0 Nov 11 22:06 file1
-rw-rw-r--  1 ubuntu ubuntu    0 Nov 11 22:08 file2

# -i : inode 번호 확인하여 참조하고 있는 파일 확인 가능
$ ls -i
662490 dir1  671810 file1  674202 file2

 

 

2. 파일 및 디렉토리 관련

 

cp 원위치 복사위치 : 파일 복사, 복사하며 파일 이름 변경도 가능

cp -r 원위치 복사위치 : 디렉토리 복사, 복사하며 디렉토리 이름 변경도 가능

# file1을 복사해서 dir디렉토리에 file3를 생성
$ cp file1 dir1/file3
$ ls dir1
file3

# dir1을 복사해서 현재 위치에 dir2 생성
$ cp -r dir1 dir2
$ ls
dir1  dir2  file1  file2

 

mv 원위치 이동위치 : 파일 또는 디렉토리 이동, 이동하며 파일 이름 변경도 가능

$ mv dir2 dir1
$ ls
dir1  file1  file2
$ ls dir1
dir2  file3

 

rm 파일명 : 파일 삭제

rm -r 디렉토리명 : 디렉토리 삭제

$ ls
dir1  file1  file2

# 파일 삭제
$ rm file1
$ ls
dir1  file2

# 디렉토리 삭제
$ rm -r dir1
$ ls
file2

 

tar cvfz target.tar.gz 디렉토리 파일 파일... : 압축

tar xvfz target.tar.gz : 압축해제

  • x : 압축해제(c : 압축) / v : 처리정보출력 / z : gzip 사용 / f : 여러 개의 파일

 

[파일 및 디렉토리 권한]

  • 첫 글자 : File(-) / Directory(d) / Link(|)
  • 3글자씩 : 소유자(owner) / 그룹(group) / 그외(other)
  • 권한 종류 : read(r) 읽기 / write(w) 쓰기 / execute(x) 실행
  • (예시)  -rwxr-xr-- : 파일에 대해 소유자는 전체 권한, 같은 그룹은 읽기와 실행만 가능, 타그룹은 읽기만 가능

 

chmod 옵션 파일·디렉토리 : 파일 또는 디렉토리의 권한 변경(change modification)

[옵션 1]

  • u : user(owner) / g : group / o : other
  • + : 권한 부여 / - : 권한 삭제
  • r : read / w : write / x : execute
$ ls -l
-rw-rw-r-- 1 ubuntu ubuntu 0 Nov 11 22:08 file1

# file1의 소유자에게 실행 권한 추가
$ chmod u+x file1
$ ls -l
-rwxrw-r-- 1 ubuntu ubuntu 0 Nov 11 22:08 file1

 [옵션 2]

  • r : 4 / w : 2 / x : 1 => r, w, x의 합계를 해당 소유자 또는 그룹의 권한으로 하여, 세자리 숫자로 권한 지정
# user 권한 read(4) + write(2) + execute(1) = 7
# 소유자 그룹 권한 write(2) + execute(1) = 3
# 타그룹 권한 execute(1) = 1
$ chmod 731 file1
$ ls -l
-rwx-wx--x 1 ubuntu ubuntu 0 Nov 11 22:08 file1

 

chown 옵션 소유자:그룹 : 파일 또는 디렉토리의 소유자 및 그룹 변경

# file1의 소유자를 ubuntu, 그룹을 big으로 지정
$ sudo chown ubuntu:big file1
$ ls -l
-rwx-wx--x 1 ubuntu big 0 Nov 11 22:08 file1

 

[link]

  • hard link
    • filesystem에서 같은 inode를 참조하는 link file을 추가적으로 생성
    • 어떤 hard link 파일을 수정하든 참조하고 있는 파일의 수정이 일어남
    • hard link파일이 하나라도 남아있으면 해당 파일이 filesystem에서 삭제되지 않음
    • 파일에만 설정 가능
  • soft(symbolic) link
    • 바로가기
    • soft link 파일은 filesystem의 파일이 아닌 link 파일을 참조함

ln 원파일 생성파일 : hard link 파일 생성

ln -s 원파일 생성파일 : soft link 파일(바로가기) 생성

* ls -i : inode 번호 확인하여 참조하고 있는 파일 확인 가능

$ ln file1 file2
$ ln -s file1 file3

$ ls -li
674202 -rwx-wx--x 2 ubuntu big    0 Nov 11 22:08 file1
674202 -rwx-wx--x 2 ubuntu big    0 Nov 11 22:08 file2
680594 lrwxrwxrwx 1 ubuntu ubuntu 5 Nov 11 23:17 file3 -> file1
# file2는 hard link 파일로, file1과 같은 inode 번호를 가진다

 

 

3. 파일 입출력 

 

cat 파일명 : 표준 입력을 모니터에 표준 출력

  • cat 파일명 | more : 보다 편하게 내용 확인 가능, space 눌러 다음 내용 출력
  • cat 파일1 파일2 ... > 새파일 : 파일 여러 개를 합쳐 새로운 파일 생성하여 출력
  • cat 파일1 >> 파일2 : 파일1의 내용을 파일2의 끝에 붙여넣어 출력

 

head 파일명 : 파일의 시작부터 10번째 줄까지 출력

 

tail 파일명 : 파일의 마지막 10줄을 출력

  • tail -f 파일명 : 파일의 끝에 새로운 내용 추가시 바로 출력, 로그 확인시 많이 사용

 

less 파일명 : 파일 내용 확인, 화면에 출력되는 만큼만 메모리 할당하여 용량이 큰 파일 확인할 때 유용

 

 

 

4. 검색

grep 문자열 파일명 : 특정 문자열이 포함된 파일 검색

$ grep test *
file1:test1
file2:test12
file3:test123

# -w 옵션 : 정확히 일치하는 파일만 검색
$ grep -w test12 *
file2:test12

 

find 경로 표현식 : 해당 경로에서 표현식에 따라 파일 검색

# 현재 경로에서 이름에 file이 들어가는 파일 검색
$ find . -name '*file*'
./file3
./file1
./file2

# 현재 경로에서 .txt 확장자 파일 검색
$ find . -name '*.txt'
./text.txt

 

which 명령어 : 특정 명령어의 위치 검색

$ which cat
/usr/bin/cat

 

whereis 명령어 : 특정 명령어의 실행파일위치, 소스위치, man페이지파일 위치 검색

$ whereis cat
cat: /usr/bin/cat /usr/share/man/man1/cat.1.gz

 

 

5. 사용자 관련

 

su 사용자명 : 해당 사용자로 변경

sudo 명령 : 관리자 권한으로 실행

id 사용자명 : 사용자 정보 확인

passwd 사용자명 : 사용자 비밀번호 설정

adduser 사용자명 : 사용자 생성

deluser 옵션 사용자명: 사용자 삭제

usermod 옵션 사용자명 : 사용자 정보 수정

# superuser의 권한으로 update 실행
$ sudo apt update
# superuser의 권한으로 upgrade (세부 설정은 모두 yes)
sudo apt upgrade -y

# big이라는 사용자 추가
$ sudo adduser big

# big으로 사용자 변경
$ su big
--- [ big의 터미널 ] ---
$ exit

# big 계정의 권한(그룹) 확인
$ id big
uid=1001(big) gid=1001(big) groups=1001(big)

# sudo 라는 권한을 big 계정에게 줌
$ sudo usermod -aG sudo big
$ id big
uid=1001(big) gid=1001(big) groups=1001(big),27(sudo)

 

 

6. 기타

 

top : cpu 및 memory 상태 확인

$ top
```
top - 23:47:27 up  1:47,  1 user,  load average: 0.10, 0.03, 0.01
Tasks: 291 total,   1 running, 290 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.6 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.1 si,  0.0 st
...

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND 
   1720 ubuntu    20   0 4046660 248920 106512 S   2.0   5.0   0:19.21 gnome-+ 
   1569 ubuntu    20   0  282172  65228  40376 S   1.3   1.3   0:08.63 Xorg   

```

 

ping 도메인주소 : 외부 호스트 서버가 네트워크상으로 접근이 가능한지 확인

$ ping google.com
PING google.com (142.250.199.110) 56(84) bytes of data.
64 bytes from nrt13s52-in-f14.1e100.net (142.250.199.110): icmp_seq=1 ttl=128 time=37.6 ms
64 bytes from nrt13s52-in-f14.1e100.net (142.250.199.110): icmp_seq=2 ttl=128 time=36.6 ms
64 bytes from nrt13s52-in-f14.1e100.net (142.250.199.110): icmp_seq=3 ttl=128 time=45.5 ms

 

nslookup 도메인주소 : 해당 도메인의 ip주소 확인

$ nslookup google.com
Server:		127.0.0.53
Address:	127.0.0.53#53

Non-authoritative answer:
Name:	google.com
Address: 142.251.42.142
Name:	google.com
Address: 2404:6800:4004:821::200e

 

ps : 실행 중인 프로세스 확인

# ps -ef : 리눅스 서버에서 운영 중인 모든 프로세스 확인
$ ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 21:59 ?        00:00:03 /sbin/init auto noprompt
root           2       0  0 21:59 ?        00:00:00 [kthreadd]
root           3       2  0 21:59 ?        00:00:00 [rcu_gp]
root           4       2  0 21:59 ?        00:00:00 [rcu_par_gp]

# 특정 프로세스 확인할 때는 grep과 파이프라인으로 연결하여 프로세스 이름 확인하면 됨
$ ps -ef | grep python
root         855       1  0 22:00 ?        00:00:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
root         977       1  0 22:00 ?        00:00:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
ubuntu      2607    2040  0 23:59 pts/0    00:00:00 grep --color=auto python

 

kill : 프로세스 종료

  • kill -15 프로세스id : 프로세스 정상 종료
  • kill -9 프로세스id : 프로세스 강제 종료

 

uname : 시스템 정보 확인

$ uname
Linux

# 시스템 정보 자세히 확인
$ uname -a
Linux ubuntu 5.15.0-52-generic #58~20.04.1-Ubuntu SMP Thu Oct 13 13:09:46 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

 

reboot : 시스템 재부팅

$ sudo reboot

 

halt : 시스템 완전 종료

# -p 옵션을 주면 전원까지 off
$ sudo halt -p

 

 

 

참고한 영상 :

리눅스 명령어 모음 | 초중급 개발자를 위한 기본 명령어 강좌 - YouTube