본문 바로가기

SK네트웍스 Family AI캠프 10기/Daily 회고

69일차. Linux

더보기

 

69일 차 회고.

 

 오늘은 리눅스와 쉘에 대해서 배웠는데 대부분은 이미 대학교에서 배운 내용이라 다소 쉬운 느낌이 있었다. 그래도 좀 새로운 내용도 있어서 괜찮았던 것 같다. 다시 정리할 수 있는 시간이 되었다. 그리고 빅데이터분석기사 필기에 합격을 해서 실기 준비를 할 수 있게 되었다. 떨어질까 봐 걱정했는데 안전하게 합격이 되어서 다행이었다.  

 

 

 

 

1. Linux

 

 

1-1. Linux

 

Linux

  • 리눅스 커널에 기반한 오픈소스 유닉스 계열 운영체제

 

Linux 종류

  • Ubuntu Linux
    • 가장 대중적인 리눅스
    • GUI 환경 제공
    • 무료 및 상업적 사용 가능
  • CentOS
    • Red Hat 기반
    • 가볍고 안정적인 서버 운영 가능
  • Kali Linux
    • 보안 및 해킹 테스트에 특화된 도구 포함

 

Linux 구조

  • Application
    • 웹 브라우저, 편집기 등 사용자 프로그램
  • Shell
    • 명령어 인터페이스
    • ex. Bash 등
  • Kernel
    • OS 핵심
    • 하드웨어와 소통
  •  Hardware
    • ex. CPU, RAM, Storage 등

 

Linux 디렉터리

  • / (root)
    • 루트 디렉터리
    • 모든 디렉터리의 최상위
  • /home
    • 사용자 디렉터리
  • /bin, /sbin
    • 실행 파일 저장
  • /etc
    • 설정 파일
  • /var
    • 로그, 캐시, 이메일 등 가변 데이터 저장소

 

Linux 다운로드

docker search ubuntu
docker pull ubuntu

docker images
"""
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
ubuntu       latest    602eb6fb314b   9 days ago   78.1MB
"""

docker run -it --name ubuntu-container ubuntu /bin/bash

 

 

1-2. Linux 기본명령어

 

whoami

  • 현재 로그인한 사용자 이름 출력
whoami
# root

 

apt-get

  • 우분투 계열에서 사용하는 패키지 관리 명령어
apt-get update					# 업데이트할 패키지 목록 갱신

apt-get upgrade					# 설치된 패키지 업데이트

apt-get dist-upgrade				# 의존성까지 포함한 전체 패키지 업데이트

# apt-get install -y <Library Name>
apt-get install -y sudo vim			# 패키지 설치

 

pwd

  • 현재 위치한 디렉터리 경로 확인
pwd

 

ls

  • 디렉터리 내 파일 및 폴더 목록 출력
ls
# bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

ls -l						# 파일 상세정보 출력
#   권한  |  링크수  |   소유자  |  그룹  |  크기  |  날짜  |  이름
"""
total 60
lrwxrwxrwx   1 root root    7 Apr 22  2024 bin -> usr/bin
drwxr-xr-x   2 root root 4096 Apr 22  2024 boot
drwxr-xr-x   5 root root  360 Apr 18 09:34 dev
drwxr-xr-x   1 root root 4096 Apr 18 09:48 etc
drwxr-xr-x   3 root root 4096 Apr  4 11:09 home
lrwxrwxrwx   1 root root    7 Apr 22  2024 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Apr 22  2024 lib64 -> usr/lib64
drwxr-xr-x   2 root root 4096 Apr  4 11:03 media
drwxr-xr-x   2 root root 4096 Apr  4 11:03 mnt
drwxr-xr-x   2 root root 4096 Apr  4 11:03 opt
dr-xr-xr-x 260 root root    0 Apr 18 09:34 proc
drwx------   2 root root 4096 Apr  4 11:09 root
drwxr-xr-x   4 root root 4096 Apr  4 11:09 run
lrwxrwxrwx   1 root root    8 Apr 22  2024 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Apr  4 11:03 srv
dr-xr-xr-x  11 root root    0 Apr 18 09:34 sys
drwxrwxrwt   1 root root 4096 Apr 18 09:47 tmp
drwxr-xr-x   1 root root 4096 Apr  4 11:03 usr
drwxr-xr-x   1 root root 4096 Apr  4 11:09 var
"""

ls -a						# 숨김 파일 포함 전체 목록 출력
"""
.   .dockerenv  boot  etc   lib    media  opt   root  sbin  sys  usr
..  bin         dev   home  lib64  mnt    proc  run   srv   tmp  var
"""

 

cd

  • 디렉터리 이동
# cd <Directory>
cd usr						# usr 디렉토리로 이동

cd ..						# 상위 디렉토리로 이동

cd /						# 루트 디렉토리로 이동

cd ~						# 홈 디렉토리로 이동

 

clear

  • 현재 터미널 화면을 깨끗하게 지운다.
clear

 

cat

  • 파일 내용 출력
# cat <File Name>
cat .profile
"""
# ~/.profile: executed by Bourne-compatible login shells.

if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi

mesg n 2> /dev/null || true
"""

 

mkdir

  • 디렉터리 생성
# mkdir <Directory Name>
mkdir sample					# sample 디렉토리 생성

# mkdir -p <Path>
mkdir -p tutorial/test				# 중첩 디렉토리 생성

 

touch

  • 빈 파일 생성
# touch <File Name>
touch test.txt

 

mv

  • 파일 이동 및 이름 변경
# mv <Source> <Destination>
mv test.txt sample/test.txt			# 파일 이동

mv sample/test.txt sample/sample.txt		# 파일 이름 변경

 

cp

  • 파일 및 디렉터리 복사
# cp <Source> <Destination>
cp ./sample/sample.txt ./sample.txt		# 파일 복사

# cp -r <Source> <Destination>
cp -r ./tutorial/test ./tutorial2		# 디렉토리 복사

 

rm

  • 파일 및 디렉터리 삭제
# rm <File Name>
rm sample.txt					# 파일 삭제

# rm -r <Directory Name>
rm -r sample					# 디렉토리 삭제

 

 

1-3. Permission

 

Permission

  • 파일 타입
기호 의미
- Normal File
d Directory
l Link
p Named Pipe
s Socket
c Character Device
b Block Device
  • User 권한
    • r (4) : 읽기 권한
    • w (2) : 쓰기 권한
    • x (1) : 실행 권한
  • Group 권한
    • r (4) : 읽기 권한
    • w (2) : 쓰기 권한
    • x (1) : 실행 권한
  • Other 권한
    • r (4) : 읽기 권한
    • w (2) : 쓰기 권한
    • x (1) : 실행 권한

 

Permission 변경

  • chmod
    • 파일 및 디렉터리 권한 설정 및 변경
# chmod <Permission> <File or Directory Name>
chmod go+w test.txt				# 그룹과 기타 사용자에게 쓰기 권한 추가

ls -l
"""
total 0
-rw-rw-rw- 1 root root 0 Apr 18 11:34 test.txt
"""

chmod go-rw test.txt				# 그룹과 기타 사용자에서 읽기/쓰기 권한 제거

ls -l
"""
total 0
-rw------- 1 root root 0 Apr 18 11:34 test.txt
"""
chmod 777 test.txt				# 모든 사용자에게 읽기/쓰기/실행 권한 부여

ls -l
"""
total 0
-rwxrwxrwx 1 root root 0 Apr 18 11:34 test.txt
"""

chmod 664 test.txt				# 소유자 : rw- / 그룹 : rw- / 기타 : r--

ls -l
"""
total 0
-rw-rw-r-- 1 root root 0 Apr 18 11:34 test.txt
"""

 

 

1-4. Account

 

Account

  • root
    • 시스템 전체를 제어할 수 있는 최고 권한의 관리자 계정
  • 일반 사용자
    • root가 useradd 명령어로 생성한 제한 권한 계정

 

Account 명령어

  • 현재 로그인한 계정 확인
whoami
  • 비밀번호 변경
# sudo passwd <Account Name>
sudo passwd root
  • 계정 생성
# sudo adduser <New Account ID>
sudo adduser student

# adduser 명령어가 없는 경우 설치
apt-get install -y adduser
  • 계정 전환
# su - <Account Name>
su - student

whoami
# student

pwd
# /home/student
  • [student] 파일 생성 및 확인
touch test.txt

echo "Hello World" > test.txt

cat test.txt
# Hello World
  • [student / root] 계정 생성
adduser tutorial
# fatal: Only root may add a user or group to the system.

sudo adduser tutorial
"""
student is not in the sudoers file.
"""

su - root

sudo adduser tutorial
  • [tutorial] 디렉터리 이동
su - tutorial

cd /home/student
# -bash: cd: /home/student: Permission denied

ls /home/student
# ls: cannot open directory '/home/student': Permission denied

cat /home/student/test.txt
# cat: /home/student/test.txt: Permission denied
  • [student] 권한 부여
su - student

chmod 755 .
  • [tutorial] 디렉터리 이동
su - tutorial

cd /home/student

ls -l
# -rw-rw-r-- 1 student student 12 Apr 18 12:11 test.txt

cat test.txt
# Hello World

echo "Good Job" > test.txt
# -bash: test.txt: Permission denied
  • 세션 종료
exit

 

 

1-5. Vim

 

Vim

  • 리눅스에서 기본적으로 사용하는 텍스트 편집기

 

Vim 환경 설정

# .vimrc
set number
set hlsearch
set smartcase
set autoindent
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab
set ruler

 

Vim 모드

  • 명령 모드(Normal Mode)
    • 기본 상태
    • 커서 이동, 삭제/복사/붙이기, 탐색 등 조작 가능
    • 입력 모드로 전환하려면 i, a, o 등 사용
  • 입력 모드(Insert Mode)
    • 키보드로 텍스트 입력 가능
    • 명령 모드에서 i, a, o 등으로 진입
    • [Esc] 키로 명령 모드 복귀
  • 마지막 행 모드(Command-Line Model)
    • 명령 모드에서 [:] 키 입력
    • 파일 저장, 종료, 검색, 치환, 설정 등 실행

 

 

1-6. Shell

 

Shell

  • 사용자의 명령어를 받아 커널에 전달하고, 그 결과를 사용자에게 다시 보여주는 명령어 해석기

 

Shell 종류

  • sh
    • Bourne Shell
    • 가장 기본적인 Unix Shell
  • bash
    • GNU 프로젝트에서 개발된 sh의 확장 버전
  • zsh
    • bash의 기능을 확장한 고급 Shell
  • ksh
    • Korn Shell
  • fish
    • 사용자 친화적 기능이 많은 Shell

 

 

1-7. Bash Shell

 

Bash Shell

  • 설치된 Shell 확인
cat /etc/shells
"""
# /etc/shells: valid login shells
/bin/sh
/usr/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/usr/bin/dash
"""
  • 현재 지정된 Shell 확인
echo $SHELL
# /bin/bash

 

일반 변수

  • 변수 선언
# <Variable Name>=<Variable Value>
username=tom
  • 변수 확인
# echo $<Variable Name>
echo $username
# tom
  • 전체 변수 조회
set

set | grep username				# 전체 변수 중 username 조회
# username=tom
  • 변수 삭제
# unset <Variable Name>
unset username

set | grep username

 

환경 변수

  • 환경 변수 선언
# export <Variable Name>=<Variable Value>
export NAME=tom
  • 환경 변수 확인
echo $NAME
# tom
  • 전체 환경 변수 조회
env

env | grep NAME					# 전체 환경 변수 중 NAME 조회
"""
HOSTNAME=e0ad5ee642bd
NAME=tom
"""
  • 환경 변수 삭제
unset NAME

 

Metacharacters

  • 연습용 디렉터리 및 파일 생성
cd ~
mkdir meta
cd meta
touch test1 test2 test3 test11 test12 test13 test21 test22 test23

ls
# test1  test11  test12  test13  test2 test21 test22 test23 test3
  • File Name Extension
    • 파일 이름을 확장하는 데 사용되는 메타문자
# * : 문자열 (0개 이상의 문자)
ls *						# 전체 파일
# test1  test11  test12  test13  test2  test21  test22  test23  test3

ls test1*					# 'test1'로 시작하는 전체 파일
# test1  test11  test12  test13
# ? : 임의의 문자 1개
ls test1?					# 'test1' 다음에 문자 1개가 오는 파일
# test1  test2  test3

ls test?3					# 'test' + 문자 1개 + '3' 형태의 파일
# test13 test23
# {} : 문자열 집합
ls test{11,22}					# 'test11' 또는 'test22'
# test11 test22
# [] : 문자 집합 (한 자리 문자 중에서 선택)
ls test[12]					# 'test1' 또는 'test2'
# test1 test2
  • Command Substitution
date						# 현재 날짜와 시간 출력
# Fri Apr 18 15:40:47 KST 2025

d=`date`					# 명령어 치환
echo $d
# Fri Apr 18 15:40:47 KST 2025
  • Escape Character
    • Shell이 해석하는 것을 원하지 않을 때 사용한다.
# \ : 특수문자의 기능을 무효화하여 문자 그대로 사용하게 함
touch \*					# '*'라는 이름의 파일 생성

ls \*
# '*'
# "" : 문자열을 감싸면서 변수 및 명령어 치환은 허용
echo "Today is `date`."
# Today is Fri Apr 18 15:46:20 KST 2025.

echo "Today is $(date)."
# Today is Fri Apr 18 15:46:20 KST 2025.
# '' : 문자열을 감싸되, 변수 및 명령어 치환을 해석하지 않음
echo 'Today is `date`.'
# Today is `date`.

echo 'Today is $(date).'
# Today is $(date).
  • Job Control Metacharacters
# && : 앞의 명령어가 성공했을 때만 뒤의 명령어 실행
date && pwd
"""
Fri Apr 18 16:05:49 KST 2025
/root/meta
"""

data && pwd
"""
bash: data: command not found
"""
# ; : 앞의 명령어 실행 결과에 상관없이 뒤의 명령어를 실행
date; pwd
"""
Fri Apr 18 16:06:02 KST 2025
/root/meta
"""

data; pwd
"""
bash: data: command not found
/root/meta
"""
# || : 앞의 명령어가 실패했을 때만 뒤의 명령어 실행
date || pwd
"""
Fri Apr 18 16:12:11 KST 2025
"""

data || pwd
"""
bash: data: command not found
/root/meta
"""
  • I/O Redirection & Pipe
# | : pipe (앞 명령의 출력을 뒤 명령의 입력으로 전달)
set | grep OP

set | grep OP | wc -l
# 4
# > : overwrite (파일에 출력 내용을 덮어씀)
echo "Hello World" > test1
cat test1
"""
Hello World
"""
# >> : append (파일에 출력 내용을 추가)
echo "Good Morning" >> test1
cat test1
"""
Hello World
Good Morning
"""
# < : 파일의 내용을 명령의 입력으로 사용
cat < test1
"""
Hello World
Good Morning
"""

cat < test1 | grep Hello | wc -l
# 1

 

Shell Script

  • bin 디렉터리 생성 및 이동
cd ~
mkdir bin
cd bin
  • hello.sh 스크립트 작성
vim hello.sh

---

#!/bin/bash

# file name : hello.sh

echo "Today: $(date +%Y-%m-%d)"
echo "Hello World"
  • 실행 권한 부여
chmod 744 hello.sh

ls -l
"""
total 4
-rwxr--r-- 1 root root 90 Apr 18 16:30 hello.sh
"""
  • 실행
./hello.sh
"""
Today: 2025-04-18
Hello World
"""
  • 환경 변수 PATH 등록
PATH=$PATH:~/bin

hello.sh
"""
Today: 2025-04-18
Hello World
"""

 

Positional Parameters

  • hello.sh 스크립트 작성
vim hello.sh

---

#!/bin/bash

# file name : hello.sh

echo "Today: $(date +%Y-%m-%d)"
echo "Hello World"
echo "----------"

### Arguments

echo "Total Arguments:" $#
echo "All Arguments Values:" $@

echo "----------"

echo "First ->" $1
echo "Second ->" $2

echo "----------"

args=("$@")
echo "First ->" ${args[0]}
echo "Second ->" ${args[1]}
  • 실행
hello.sh a1 b1
"""
Today: 2025-04-18
Hello World
----------
Total Arguments: 2
All Arguments Values: a1 b1
----------
First -> a1
Second -> b1
----------
First -> a1
Second -> b1
"""

hello.sh a1 b1 c1 d1
"""
Today: 2025-04-18
Hello World
----------
Total Arguments: 4
All Arguments Values: a1 b1 c1 d1
----------
First -> a1
Second -> b1
----------
First -> a1
Second -> b1
"""

 

if then fi 조건문

  • hello.sh 스크립트 작성
#!/bin/bash

# file name : hello.sh

num=$1
if [ $num -ge 5 ]
then
    echo "Today: $(date +%Y-%m-%d)"
else
    echo "Hello World"
fi

echo "----------"

### Arguments

echo "Total Arguments:" $#
echo "All Arguments Values:" $@

echo "----------"

echo "First ->" $1
echo "Second ->" $2

echo "----------"

args=("$@")
echo "First ->" ${args[0]}
echo "Second ->" ${args[1]}
  • 실행
hello.sh 5 10
"""
Hello World
----------
Total Arguments: 2
All Arguments Values: 3 10
----------
First -> 3
Second -> 10
----------
First -> 3
Second -> 10
"""

hello.sh 15 10
"""
Today: 2025-04-18
----------
Total Arguments: 2
All Arguments Values: 15 10
----------
First -> 15
Second -> 10
----------
First -> 15
Second -> 10
"""

 

 

'SK네트웍스 Family AI캠프 10기 > Daily 회고' 카테고리의 다른 글

71일차. JavaScript  (0) 2025.04.22
70일차. HTML & CSS  (0) 2025.04.21
68일차. Docker  (0) 2025.04.17
67일차. Docker  (0) 2025.04.16
65-66일차. 단위 프로젝트(LLM(초거대언어모델))  (0) 2025.04.15