Linux basic command

๊ธฐ๋ณธ ๋ช…๋ น์–ด

๊ฒฝ๋กœ ๐Ÿ”ฅ

CLI ์ƒ์—์„œ ๋‚˜์˜ ์œ„์น˜๋ฅผ ์•Œ๋ ค๋ฉด?

$ pwd

์ ˆ๋Œ€๊ฒฝ๋กœ

  • ํŠน์ •ํ•œ ์œ„์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฒฝ๋กœ๋ฅผ ์„ค์ •

  • ์ตœ์ƒ์œ„ ๋ฃจํŠธ (root): /

  • ์‚ฌ์šฉ์ž์˜ ํ™ˆ ๋””๋ ‰ํ† ๋ฆฌ (user): ~/

์ƒ๋Œ€๊ฒฝ๋กœ

  • ํ˜„์žฌ ๋‚˜์˜ ์œ„์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฒฝ๋กœ๋ฅผ ์„ค์ •

  • ํ˜„์žฌ ์œ„์น˜: ./

  • ์ƒ์œ„ ๋””๋ ‰ํ† ๋ฆฌ: ../

  • ์ƒ์œ„์˜ ์ƒ์œ„ ๋””๋ ‰ํ† ๋ฆฌ: ../../

์ด๋™

๋””๋ ‰ํ† ๋ฆฌ ์ด๋™

$ cd /var/log

์‚ฌ์šฉ์ž(๋ณธ์ธ) ํ™ˆ ๋””๋ ‰ํ† ๋ฆฌ

$ cd ~/

ํŠน์ • ์‚ฌ์šฉ์ž ํ™ˆ ๋””๋ ‰ํ† ๋ฆฌ

$ cd /Users/<ํŠน์ •์‚ฌ์šฉ์ž์•„์ด๋””> (macOS)

๋””๋ ‰ํ† ๋ฆฌ์™€ ํŒŒ์ผ์˜ ๋ชฉ๋ก์„ ์ถœ๋ ฅ

$ ls -1

$ ls -l

$ ls -alF

$ ls /

$ ls /Users/<์‚ฌ์šฉ์ž์•„์ด๋””>

$ ll (alias ls -al) โ†’ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ฒŒ๋  ๋ช…๋ น์–ด ๐Ÿ˜

Glob pattern ๐Ÿ”ฅ

์ž„์˜์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๋Š” ๋ฌธ์ž์—ด: * ์ž„์˜์˜ ํ•œ ๊ธ€์ž: ? ๊ธ€์ž์˜ ์ง‘ํ•ฉ: [abcw-z]

์˜ˆ์‹œ

$ ls *

$ ls .*

$ ls [a-f]*.txt

$ ls *.tx?

ํ™”๋ฉด ์ฒญ์†Œ

$ clear

Ctrl + L

Command + K (terminal clear buffer)

์ง€๋‚œ ๋ช…๋ น์–ด ํ™œ์šฉ

์กฐํšŒ

$ history

12๋ฒˆ ๋ช…๋ น ์žฌ์‹คํ–‰

$ !12

์ปค๋งจ๋“œ์˜ prefix ๋กœ ์žฌ์‹คํ–‰

$ !ls

๋ฐ”๋กœ ์ง์ „ ๋ช…๋ น ์žฌ์‹คํ–‰

$ !!

๋ฐ”๋กœ ์ง์ „ ๋ช…๋ น์˜ argument

$ ls -alF โ†’ $ ls !$

์ง์ „ ๋ช…๋ น ํŽธ์ง‘

$ echo "hello world"

^world^java

โ†’ $ echo "hello java"

๋ช…๋ น์–ด ํŽธ์ง‘ ๐Ÿ”ฅ

๋ช…๋ น์–ด ๋งจ ์•ž ๋˜๋Š” ๋์œผ๋กœ ์ด๋™

Ctrl + A

Ctrl + E

๋‹จ์–ด ๋‹จ์œ„ ์ด๋™

Option + ๋ฐฉํ–ฅํ‚ค

์„œ๋ฒ„ ์ ‘์† ๋ฐ ์ข…๋ฃŒ ๐Ÿ”ฅ

๊ฒŒ์ดํŠธ์›จ์ด ์ ‘์† ๋ฐฉ๋ฒ•

$ ssh <์•„์ด๋””>@hcon.nhnent.com

$ ssh <์•„์ด๋””>@gw-dev.godo.co.kr

์ข…๋ฃŒ

$ logout ๋˜๋Š” exit

์‚ฌ์šฉ์ž ์กฐํšŒ

$ last

์ปค๋งจ๋“œ ์„ค๋ช… ๋ณด๋Š” ๋ฒ• ๐Ÿ”ฅ

$ man โ€”help

$ man ssh

$ man 2 open

$ man 3 opendir

์งˆ๋ฌธ \

  • man ์—์„œ ๋น ์ ธ๋‚˜์˜ค๋Š” ๋ฐฉ๋ฒ•์€?

  • man ๋‹ค์Œ ๋ถ™์€ 2, 3์˜ ์˜๋ฏธ๋Š”?

์„ค๋ช… ๋ณด๋Š” ๋ฒ•

์ •๋‹ต \

  • q

  • man man

๋ช…๋ น์–ด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•

which \

  • which lsof

  • PATH ํ™˜๊ฒฝ ๋ณ€์ˆ˜์— ํฌํ•จ๋˜์–ด์•ผ ํƒ์ƒ‰ ๊ฐ€๋Šฅ

whereis \

  • whereis lsof

๋ฌธ์ž์—ด ์ถœ๋ ฅํ•˜๊ธฐ

echo \

  • $ echo "hello world"

  • $ echo -n "hello world"; echo " java coffee"

ํ˜„์žฌ ์ ‘์†ํ•ด์žˆ๋Š” ์„œ๋ฒ„์˜ ์ด๋ฆ„๊ณผ OS ์ •๋ณด๋ฅผ ํ™•์ธ

  • $ uname

  • $ uname -a

๋น„์Šทํ•œ ๋ช…๋ น

  • $ hostname

๋‚˜์˜ ์‚ฌ์šฉ์ž ์•„์ด๋””๋ฅผ ์•Œ๋ ค๋ฉด?

  • $ id

  • $ whoami

  • echo "$USER"

ํŒŒ์ผ ๋ณด๊ธฐ ๐Ÿ”ฅ

$ echo "title: log.log <Key Input: Return>
content1: hello world <Key Input: Return>
content2: linux command tutorial <Key Input: Return>
eof: end of file" >> log.log

OR

echo "title: log.log\ncontent1: hello world\ncontent2: linux command tutorial\neof: end of file" >> log.log

$ vi log.log

$ cat log.log

$ more log.log

$ grep hello log.log | less -eMR

ํŒŒ์ผ ์ผ๋ถ€๋งŒ ๋ณด๊ธฐ

$ head -1 log.log

$ tail -2 log.log

$ tail -f /var/log/lastlog ๐Ÿ’™

์ฐพ๊ธฐ

$ grep [๋ฌธ์ž์—ด] [ํŒŒ์ผ]

$ grep "tutorial" log.log

$ grep -r "bash tutorial" ./*.log

์ž˜๋ผ๋‚ด์„œ ์“ฐ๊ธฐ

$ cut -d [๊ตฌ๋ถ„์ž] -f [ํ•„๋“œ ๋ฒˆํ˜ธ ํŒŒ์ผ]

$ cat log.log | cut -d : -f 1

$ cat log.log | cut -d : -f 2

ํŒŒ์ผ ๊ด€๋ จ ๋ช…๋ น์–ด

ํŒŒ์ผ ์ƒ์„ฑ, ๋ณต์‚ฌ, ์ด๋™, ์‚ญ์ œ \

  • touch

  • cp

  • mv

  • rm

๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ, ์ด๋™, ์‚ญ์ œ \

  • mkdir

  • cp -r

  • mv

  • rmdir

  • rm -r


์ž์ฃผ ์“ฐ๋Š” ๋ช…๋ น์–ด

๋””๋ ‰ํ† ๋ฆฌ ์ด๋ฆ„ ๋‹ค๋ฃจ๊ธฐ

$ dirname /etc/passwd

ํŒŒ์ผ ์ด๋ฆ„ ๊ตฌํ•˜๊ธฐ

$ basename /etc/passwd

์ž…๋ ฅ ๊ฐœ๋ณ„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๐Ÿ”ฅ

xargs ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ผ์ธ ๋‹จ์œ„๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ

$ xargs [๋ช…๋ น]

$ xargs -I % ๋ช…๋ น "%"

$ find /etc/init | xargs wc -l

$ find /etc/init | xargs -I % wc -l "%"

์ •๋ ฌ

์ •๋ ฌ $ sort log.log

์ค‘๋ณต ์ œ๊ฑฐ $ uniq [ํŒŒ์ผ]

๋ฌธ์ž, ๋‹จ์–ด, ๋ผ์ธ ์ˆ˜ ์„ธ๊ธฐ

$ wc [ํŒŒ์ผ]

๋ฌธ์ž: $ wc -m

๋‹จ์–ด: $ wc -w

๋ผ์ธ ์ˆ˜: $ wc -l

ํŒŒ์ผ ํƒ์ƒ‰ ๐Ÿ”ฅ

$ find <๋””๋ ‰ํ† ๋ฆฌ> -name <์ด๋ฆ„ ํŒจํ„ด> -type <ํŒŒ์ผ์œ ํ˜•>

$ find ./log

$ find ./log -name "*.log" -ls

$ find ./log -type d -name "log"

Pattern-directed scanning and processing language

awk

$ cat ./log/log.log | sed 's/world/bash/g'

$ cat ./log/log.log | awk -F ":" '{printf("%s\t%s\n", $1, $2)}'

Stream EDitor ๐Ÿ”ฅ

sed

$ cat ./log/log.log | sed 's/[^log]/k/g' | grep k

ํŒŒ์ผ ๋ฌถ๊ธฐ, ์••์ถ•

  • tar

  • zip / unzip

ํ”„๋กœ์„ธ์Šค

ํ”„๋กœ์„ธ์Šค ๋ชฉ๋ก ์กฐํšŒ

$ ps aux / pstree

ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ

$ kill <ํ”„๋กœ์„ธ์Šค id>

pkill <ํ”„๋กœ์„ธ์Šค๋ช…>

์‹œ์Šคํ…œ

์‹œ์Šคํ…œ ์ž์› ์‚ฌ์šฉ ํ˜„ํ™ฉ ์กฐํšŒ

$ top

$ iostat 1

๋””๋ ‰ํ† ๋ฆฌ ํฌ๊ธฐ ์กฐํšŒ

$ du

๋””๋ ‰ํ† ๋ฆฌ ์ „์ฒด ์‚ฌ์šฉ ํ˜„ํ™ฉ ์กฐํšŒ

$ df

๋„คํŠธ์›Œํฌ ๋ช…๋ น

$ ifconfig

$ ping shopby.co.kr

$ ping 115.88.123.139

$ getent hosts localhost (local machine)

$ getent hosts shopby.co.kr (gateway)

$ traceroute

$ wget -0 log.log https://naver.com

์‹ค์Šต

  • ์‚ฌ์šฉ์ž์˜ ํ™ˆ ๋””๋ ‰ํ† ๋ฆฌ์˜ ์„œ๋ธŒ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋””์Šคํฌ ์‚ฌ์šฉ๋Ÿ‰ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์„œ ๋ณด๋ ค๋ฉด?

  • ./log/log.log ํŒŒ์ผ ๋‚ด์šฉ ์ค‘ โ€œlinux command tutorialโ€ ๋ฅผ โ€œ<์‚ฌ์šฉ์ž์ด๋ฆ„> completed!โ€ ๋ผ๊ณ  ๋ณ€๊ฒฝํ•˜๊ณ  ์ €์žฅํ•˜๊ธฐ

์ •๋‹ต

  • $ du -ks * | sort -rn

  • $ ls | xargs -I % sed -i "s/linux command tutorial/$USER completed\!/g" %


์‰˜ ์Šคํฌ๋ฆฝํŠธ

shebang

  • #! : ์–ด๋–ค ์ธํ„ฐํ”„๋ฆฌํ„ฐ ํ”„๋กœ๊ทธ๋žจ์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ๊ฐ€๋ฅผ ์ง€์ •

  • #!/bin/bash

  • #!/usr/bin/env python (๊ถŒ์žฅ)

ํŒŒ์ผ ์ž‘์„ฑ

#!/bin/bash

echo "hello world"

์‹คํ–‰ $ bash test.sh

$ chmod a+x test.sh

$ ./test.sh


Reference

https://tldp.org/LDP/abs/html/index.html

Last updated