-
๐ Ch08 Advanced Tools ๐๐ ํ๊ต ๊ณต๋ถ/๐ ์ ๋์ค ์์คํ 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 # ๋์๋ฌธ์ ๋ฌด์
'๐ ํ๊ต ๊ณต๋ถ > ๐ ์ ๋์ค ์์คํ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ Ch11 Running program at specified times ๐ (0) 2025.04.12 ๐ Ch10 Job Control & Process Management ๐ (0) 2025.04.11 ๐ Ch07 vi ๐ (0) 2025.04.03 ๐ Ch06 Unix Commands In-Depth ๐ (0) 2025.04.03 ๐ Ch05 Customizing Working Environment ๐ (0) 2025.04.03