์ง„๋ฐฉ์ด 2025. 4. 11. 09:00

Regular Expression ์ •๊ทœ ํ‘œํ˜„์‹

A syntactical set or phrase that represents a pattern of text or strings

๋ฌธ์ž์—ด ๋‚ด์—์„œ ํŠน์ • ํŒจํ„ด ์ฐพ๊ฑฐ๋‚˜ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

Enables you to represent a varying array of characters with a much smaller set of predefined characters

๋ฌธ์ž์—ด์—์„œ ์›ํ•˜๋Š” ํ…์ŠคํŠธ ์ฐพ์Œ / ๊ฐ„๋‹จํ•œ ์‹๋งŒ์œผ๋กœ ๋‹ค์–‘ํ•œ ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด์— ๋Œ€์‘ ๊ฐ€๋Šฅ

Often include metacharacters

Metacharacters - characters that represent another set or group of characters or commands

ํŠน๋ณ„ํ•œ ์˜๋ฏธ, ๋™์ž‘ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฌธ์ž / ๋‹ค๋ฅธ ๋ฌธ์ž ์ง‘ํ•ฉ, ๋ช…๋ น์–ด ๊ทธ๋ฃน ๋Œ€์‹ ํ•จ


Meta characters

. (dot, period) - a character ์ž„์˜์˜ ํ•œ ๋ฌธ์ž์™€ ์ผ์น˜

[] (square brackets) - any one of inside in it  ๋Œ€๊ด„ํ˜ธ ์•ˆ์— ์—ด๊ฑฐ๋œ ๋ฌธ์ž๋“ค ์ค‘ ํ•˜๋‚˜์˜ ๋ฌธ์ž์™€ ์ผ์น˜

* (asterisk, splat) - zero or more characters ๋ฐ”๋กœ ์•ž ๋ฌธ์ž, ๊ทธ๋ฃน์ด 0๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณต ๊ฐ€๋Šฅ

[^.*] (caret) - match at the beginning ํŒจํ„ด ๋งจ ์•ž์— ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ๋ฌธ์ž์—ด, ์ค„์˜ ์‹œ์ž‘ / ๋Œ€๊ด„ํ˜ธ ์•ˆ์˜ ๋งจ ์ฒ˜์Œ์— ์œ„์น˜ํ•˜๋ฉด ๋’ค์— ๋‚˜์—ด๋œ ๋ฌธ์ž๋ฅผ ์ œ์™ธํ•œ ์–ด๋–ค ๋ฌธ์ž์™€๋„ ์ผ์น˜ / . (dot)”์™€ “* (asterisk)”๋ฅผ ์ œ์™ธํ•œ ํ•œ ๋ฌธ์ž์™€ ์ผ์น˜

^.*[.*] - match at the beginning and match inside in it

$ (dollar) - match at the end of line ๋ฌธ์ž์—ด์ด๋‚˜ ์ค„์˜ ๋ ๋‚˜ํƒ€๋ƒ„

\ (backslash) - remove special meaning of the following character ๋‹ค์Œ์— ์˜ค๋Š” ๋ฌธ์ž์˜ ํŠน๋ณ„ํ•œ ์˜๋ฏธ ์ œ๊ธฐํ•˜์—ฌ ๊ทธ ๋ฌธ์ž๋ฅผ ์ผ๋ฐ˜ ๋ฌธ์ž๋กœ ์ทจ๊ธ‰

? (question mark) - zero or one character ์•ž์˜ ๋ฌธ์ž, ๊ทธ๋ฃน์ด 0๋ฒˆ ๋˜๋Š” 1๋ฒˆ ๋‚˜ํƒ€๋‚จ์„ ์˜๋ฏธ

[a - z] - match all occurrences of any single letter


Regular Expression

[0-9] [0-9] [0-9]\.[0-9][0-9] [0-9]\.[0-9][0-9][0-9]

-> U.S. Telephone number

0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ํ•œ ์ž๋ฆฌ ์ˆซ์ž

ํ ๋ƒ๋ง


FTP (file transfer protocol)

๋กœ์ปฌ์—์„œ FTP ํด๋ผ์ด์–ธํŠธ ์‚ฌ์šฉํ•ด ์›๊ฒฉ ์„œ๋ฒ„(10.10.54.75)์— ์ ‘์†ํ•˜์—ฌ ์—ฌ๋Ÿฌ ์ž‘์—… ์ˆ˜ํ–‰ํ•˜๋Š” ๊ณผ์ • ๋ณด์—ฌ์คŒ

1.  FTP ์„œ๋ฒ„ ์ ‘์† ๋ฐ ๋กœ๊ทธ์ธ

C:\ > ftp 10.10.54.75

๋กœ์ปฌ ์ปดํ“จํ„ฐ์˜ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ(์—ฌ๊ธฐ์„œ๋Š” C:)์—์„œ FTP ํด๋ผ์ด์–ธํŠธ ์‹คํ–‰ํ•˜์—ฌ IP ์ฃผ์†Œ 10.10.54.75์— ์ ‘์†

Id/passwd

FTP ์„œ๋ฒ„์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ์ž ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅํ•ด ๋กœ๊ทธ์ธ

2. ์›๊ฒฉ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ž‘์—…

> dc public_html

์›๊ฒฉ ์„œ๋ฒ„์—์„œ ์ž‘์—…ํ•  ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ public_html๋กœ ๋ณ€๊ฒฝ

์ผ๋ฐ˜์ ์œผ๋กœ FTP ๋ช…๋ น์–ด๋Š” cd๋ฅผ ์‚ฌ์šฉ / ์ผ๋ถ€ FTP ํด๋ผ์ด์–ธํŠธ์—์„œ๋Š” dc ์‚ฌ์šฉ

> dir (or ls)

ํ˜„์žฌ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋‚ด์˜ ํŒŒ์ผ, ํด๋” ๋ชฉ๋ก ์ถœ๋ ฅ

3. ์ „์†ก ๋ชจ๋“œ ์„ค์ • ๋ฐ ํŒŒ์ผ ์ „์†ก

> asc

FTP ์ „์†ก ๋ชจ๋“œ๋ฅผ ASCII ๋ชจ๋“œ๋กœ ๋ฒฝ๊ฒฝ

ASCII ๋ชจ๋“œ๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ ์ „์†ก ์‹œ ์‚ฌ์šฉ, ํŒŒ์ผ ๋‚ด์˜ ์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž ๋“ฑ์„ ์„œ๋ฒ„ ํ™˜๊ฒฝ์— ๋งž๊ฒŒ ์ž๋™์œผ๋กœ ๋ณ€ํ™˜

> put a.html

๋กœ์ปฌ์˜ a.html ํŒŒ์ผ์„ ์›๊ฒฉ ์„œ๋ฒ„๋กœ ์—…๋กœ๋“œ

> get b.html

์›๊ฒฉ ์„œ๋ฒ„์˜ b.html ํŒŒ์ผ์„ ๋กœ์ปฌ ์ปดํ“จํ„ฐ๋กœ ๋‹ค์šด๋กœ๋“œ

> mput *.html

ํ˜„์žฌ ๋กœ์ปฌ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ํ™•์žฅ์ž๊ฐ€ .html์ธ ๋ชจ๋“  ํŒŒ์ผ์„ ์›๊ฒฉ ์„œ๋ฒ„๋กœ ์ผ๊ด„ ์—…๋กœ๋“œ

mput์€ multiple put์˜ ์•ฝ์ž๋กœ, ์ผ์น˜ํ•˜๋Š” ํŒŒ์ผ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ํ•œ๋ฒˆ์— ์ „์†กํ•  ๋•Œ ์‚ฌ์šฉ

> bi

์ „์†ก ๋ชจ๋“œ๋ฅผ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ชจ๋“œ๋กœ ๋ณ€๊ฒฝ / ํ…์ŠคํŠธ ์ด์™ธ์˜ ํŒŒ์ผ ์ „์†ก ์‹œ ์‚ฌ์šฉ, ํŒŒ์ผ ๋ฐ์ดํ„ฐ๊ฐ€ ์›๋ณธ๊ณผ ๋™์ผํ•˜๊ฒŒ ์ „์†ก

> put my.jpg

๋กœ์ปฌ์˜ my.jpg ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ์›๊ฒฉ ์„œ๋ฒ„๋กœ ์—…๋กœ๋“œ

ํŒŒ์ผ์˜ ๋ฐ์ดํ„ฐ ์†์ƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ชจ๋“œ๋กœ ์ „์†ก

4. FTP ์„ธ์…˜ ์ข…๋ฃŒ

> quit

FTP ์„ธ์…˜์„ ์ข…๋ฃŒํ•˜๊ณ , FTP ํด๋ผ์ด์–ธํŠธ๋ฅผ ๋‹ซ์Œ


grep

ํ…์ŠคํŠธ์—์„œ ํŠน์ • ๋ฌธ์ž์—ด์ด๋‚˜ ํŒจํ„ด์ด ํฌํ•จ๋œ ์ค„๋งŒ์„ ์ฐพ์•„ ์ถœ๋ ฅ

ํŒŒ์ผ ์ด๋ฆ„ ๋’ค์— ๊ฒ€์ƒ‰ํ•  ๋ฌธ์ž์—ด ์ง€์ •ํ•˜๋ฉด ํ•ด๋‹น ํŒŒ์ผ์—์„œ ์ผ์น˜ํ•˜๋Š” ์ค„๋“ค์„ ์ฐพ์Œ

|๋ฅผ ์ด์šฉํ•ด ๋‹ค๋ฅธ ๋ช…๋ น์–ด์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„ ๊ฒ€์ƒ‰

print lines that has the string

$ grep unix30 /etc/passwd

$ get /etc/passwd | grep unix30

๊ฐ€์ ธ์˜จ ๋‚ด์šฉ์„ grep unix30์œผ๋กœ ์ „๋‹ฌํ•˜์—ฌ ๊ฒ€์ƒ‰

$ grep -v // opposite ๋ฐ˜์ „ ๋งค์นญ

$ grep -i // ignore case ๋Œ€์†Œ๋ฌธ์ž ๋ฌด์‹œ


find

์ง€์ •ํ•œ ๊ฒฝ๋กœ์—์„œ ํŒŒ์ผ์ด๋‚˜ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋‹ค์–‘ํ•œ ์กฐ๊ฑด์— ๋”ฐ๋ผ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ

find files in directory structure

$ find / -name core ์ด๋ฆ„ ๊ธฐ์ค€

$ find /export -user unix300 ํŒŒ์ผ ์†Œ์œ ์ž ๊ธฐ์ค€

$ find /export -size +2000k ํŒŒ์ผ ํฌ๊ธฐ ๊ธฐ์ค€

$ find / -perm -4000 ํŒŒ์ผ ๊ถŒํ•œ ๊ธฐ์ค€

$ find / -name core 2> /dev/null ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ๋ฌด์‹œํ•˜๋ฉฐ ๊ฒ€์ƒ‰


sort

ํ‘œ์ค€ ์ž…๋ ฅ, ํŒŒ์ผ ๋‚ด์šฉ ์ •๋ ฌํ•ด ํ‘œ์ค€ ์ถœ๋ ฅ์œผ๋กœ ๋ณด๋ƒ„

$ sort -t: +1 /etc/group

$ head /etc/passwd | sort -r > pass.txt ์ •๋ฆฌ ํ›„ ํŒŒ์ผ์— ์ €์žฅ

-f - ignore case ๋Œ€์†Œ๋ฌธ์ž ๋ฌด์‹œ

sort -f unsorted.txt

-r - reversed order

sort -r unsorted.txt

-t - set delimiter (field separator) ๊ฐ ์ค„์„ ์—ฌ๋Ÿฌ ํ•„๋“œ๋กœ ๊ตฌ๋ถ„ํ•  ๋•Œ ์‚ฌ์šฉํ•  ๊ตฌ๋ถ„์ž ์ง€์ •

$ sort -t: +1 /etc/group

-o - set ouput file ์ถœ๋ ฅ ํŒŒ์ผ ์ง€์ •

sort -r input.txt -o sorted.txt

Tee/script

Tee

ํ‘œ์ค€ ์ถœ๋ ฅ์„ ํ™”๋ฉด์— ํ‘œ์‹œํ•˜๋ฉด์„œ ๋™์‹œ์— ํŒŒ์ผ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์คŒ

ํ•˜๋‚˜์˜ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์„ ๋ถ„ํ• ํ•˜์—ฌ ๋‘ ๊ตฐ๋ฐ์— ๊ธฐ๋ก (ํ„ฐ๋ฏธ๋„ ํ™”๋ฉด(ํ‘œ์ค€์ถœ๋ ฅ)๊ณผ ํŒŒ์ผ)

split output (screen and file)

$ ps -ef | tee ps.txt - ์‹คํ–‰ ์ค‘์ธ ํ”„๋กœ์„ธ์Šค ๋ชฉ๋ก ์ถœ๋ ฅ, tee ํ†ตํ•ด ์ด ๊ฒฐ๊ณผ๋ฅผ ํ™”๋ฉด์— ํ‘œ์‹œํ•˜๋ฉด์„œ ps.txt ํŒŒ์ผ์— ์ €์žฅ

$ telnet 10.10.54.75 | tee telnet.txt - telnet์œผ๋กœ ์›๊ฒฉ ํ˜ธ์ŠคํŠธ์— ์ ‘์†ํ•œ ํ›„, ์„ธ์…˜์˜ ์ถœ๋ ฅ๊ณผ ๋™์‹œ์— telnet.txt์— ๊ธฐ๋ก

Script

์‚ฌ์šฉ์ž์˜ ํ„ฐ๋ฏธ๋„ ์„ธ์…˜(์ž…๋ ฅ ๋ช…๋ น์–ด ๋ฐ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ์ „์ฒด)๋ฅผ ํŒŒ์ผ์— ๊ธฐ๋กํ•˜์—ฌ ๋‚˜์ค‘์— ์žฌ์ƒํ•˜๊ฑฐ๋‚˜ ๋ถ„์„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์คŒ

ํ„ฐ๋ฏธ๋„์—์„œ ์ฃผ๊ณ ๋ฐ›์€ ๋ชจ๋“  ์ž…๋ ฅ(ํ‚ค๋ณด๋“œ)์™€ ์ถœ๋ ฅ(ํ™”๋ฉด)์„ ํŒŒ์ผ์— ๊ทธ๋Œ€๋กœ ๊ธฐ๋กํ•ด ์ฃผ๋Š” ๋„๊ตฌ

record interactive login session

$ script [-a filename] - ์„ธ์…˜ ๊ธฐ๋ก ์‹œ์ž‘ / -a ์˜ต์…˜ ์‚ฌ์šฉํ•˜๋ฉด ๊ธฐ์กด ํŒŒ์ผ์— ๋‚ด์šฉ ์ถ”๊ฐ€, ์—†์œผ๋ฉด ์ƒˆ๋กœ์šด ํŒŒ์ผ์— ๊ธฐ๋ก

$ unix commands - ์„ธ์…˜ ๋‚ด ๋ช…๋ น์–ด ์‹คํ–‰ ๋ฐ ๊ธฐ๋ก

...

$ exit // script is ended - ์„ธ์…˜ ์ข…๋ฃŒ

$ script -a mysession.log
Script started, file is mysession.log

Unix utilities Unix ์œ ํ‹ธ๋ฆฌํ‹ฐ ๋ช…๋ น์–ด

wc - word count ์ค„, ๋‹จ์–ด, ๋ฌธ์ž ์ˆ˜ ๊ณ„์‚ฐ (-l ์ค„ ์ˆ˜, -w ๋‹จ์–ด ์ˆ˜, -c ๋ฐ”์ดํŠธ, -m ๋ฌธ์ž ์ˆ˜)

wc -l file.txt    # file.txt์˜ ์ค„ ์ˆ˜
wc -w file.txt    # file.txt์˜ ๋‹จ์–ด ์ˆ˜
wc -c file.txt    # file.txt์˜ ๋ฐ”์ดํŠธ ์ˆ˜

cut - extract character or words ํŠน์ • ์—ด, ๋ฒ”์œ„์˜ ๋ฌธ์ž, ๊ตฌ๋ถ„์ž ๊ธฐ์ค€์œผ๋กœ ํŠน์ • ํ•„๋“œ ์ถ”์ถœ

echo "apple,banana,cherry" | cut -d',' -f2   # banana
cut -c1-5 file.txt                          # ๊ฐ ์ค„์˜ 1~5๋ฒˆ์งธ ๋ฌธ์ž

paste - file merge ์—ฌ๋Ÿฌ ํŒŒ์ผ์˜ ๊ฐ ์ค„์„ ์˜†์œผ๋กœ ๋ถ™์—ฌ ํ•˜๋‚˜์˜ ํ–‰์œผ๋กœ ๋ณ‘ํ•ฉ

paste file1.txt file2.txt   # file1.txt์˜ 1ํ–‰ + ํƒญ + file2.txt์˜ 1ํ–‰ ...
paste -d',' a.txt b.txt     # ๊ตฌ๋ถ„์ž๋ฅผ ์ฝค๋งˆ๋กœ

tr - translate character ์ž…๋ ฅํ•œ ๋ฌธ์ž๋“ค์„ ๋‹ค๋ฅธ ๋ฌธ์ž๋กœ ๋ณ€ํ™˜, ์‚ญ์ œ, ์••์ถ•

echo "aAbB" | tr 'a-z' 'A-Z'    # ์†Œ๋ฌธ์ž → ๋Œ€๋ฌธ์ž
echo "a  b   c" | tr -s ' '     # ์—ฐ์† ๊ณต๋ฐฑ → ํ•˜๋‚˜์˜ ๊ณต๋ฐฑ
echo "hello123" | tr -d '0-9'   # ์ˆซ์ž ์‚ญ์ œ → hello

split - file divide ํฐ ํŒŒ์ผ์„ ์ผ์ •ํ•œ ํฌ๊ธฐ๋‚˜ ์ค„ ์ˆ˜๋งˆ๋‹ค ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž‘์€ ํŒŒ์ผ๋กœ ๋ถ„ํ• 

split -l 1000 large.txt chunk_   # 1000์ค„์”ฉ chunk_aa, chunk_ab ...
split -b 5m backup.tar.gz part_  # 5MB์”ฉ part_00, part_01 ...

uniq - file compare ์ •๋ ฌ๋œ ํŒŒ์ผ์—์„œ ์—ฐ์†๋˜์–ด ๋‚˜์˜ค๋Š” ๋™์ผ ํ–‰ ์ œ๊ฑฐ

sort names.txt \| uniq          # ์ค‘๋ณต ์ œ๊ฑฐ
sort names.txt \| uniq -c       # ๊ฐ ์ด๋ฆ„๋ณ„ ๋“ฑ์žฅ ํšŸ์ˆ˜
sort names.txt \| uniq -d       # ์ค‘๋ณต๋œ ์ด๋ฆ„๋งŒ

cmp - file compare ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ํŒŒ์ผ ๋น„๊ตํ•ด, ์ฐจ์ด ๋‚˜๋Š” ์ฒซ ์œ„์น˜ ์•Œ๋ ค์คŒ

cmp file1.bin file2.bin        # ์ฒซ ์ฐจ์ด ์œ„์น˜ ์ถœ๋ ฅ
cmp -l file1.bin file2.bin     # ๋ชจ๋“  ์ฐจ์ด ๋ฐ”์ดํŠธ ์ถœ๋ ฅ

comm - file compare ๋‘ ๊ฐœ์˜ ์ •๋ ฌ๋œ ํŒŒ์ผ ๋น„๊ตํ•ด, ๊ฐ๊ฐ์˜ ํŒŒ์ผ์—๋งŒ ์กด์žฌํ•˜๋Š” ํ–‰๊ณผ ์–‘์ชฝ์— ๊ณตํ†ต์ ์œผ๋กœ ์กด์žฌํ•˜๋Š” ํ–‰ ์„œ๋กœ ๋‹ค๋ฅธ ์—ด๋กœ ์ถœ๋ ฅ

comm fileA.txt fileB.txt            # ๊ธฐ๋ณธ: 3์—ด ๋ชจ๋‘
comm -12 fileA.txt fileB.txt        # ๊ณตํ†ต ํ–‰(3์—ด)๋งŒ
comm -23 fileA.txt fileB.txt        # fileA์—๋งŒ ์žˆ๋Š” ํ–‰

diff - file compare ๋‘ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ํ–‰ ๋‹จ์œ„๋กœ ๋น„๊ตํ•ด, ์ถ”๊ฐ€ ์‚ญ์ œ ๋ณ€๊ฒฝ๋œ ๋ถ€๋ถ„ ์ƒ์„ธํžˆ ๋ณด์—ฌ์คŒ

diff old.txt new.txt               # ๊ธฐ๋ณธ ๋น„๊ต
diff -u old.txt new.txt > patch.diff  # ํŒจ์น˜ ํŒŒ์ผ ์ƒ์„ฑ
diff -i old.txt new.txt            # ๋Œ€์†Œ๋ฌธ์ž ๋ฌด์‹œ