Bash ๋ช…๋ น ์†Œ๊ฐœ ๐Ÿง‘๐Ÿฟโ€๐Ÿ’ป

4897 ๋‹จ์–ด bashcodenewbielinux
๋ฐฉ๊ธˆ ๋ช‡ ๊ฐ€์ง€ ๊ธฐ๋ณธ ํ„ฐ๋ฏธ๋„ ๋ช…๋ น์— ๋Œ€ํ•ด ๋ฐฐ์› ๊ณ  ์ข‹์€ ๊ฒฝํ—˜์ด์—ˆ์Šต๋‹ˆ๋‹ค!

๊ทธ๋ž˜ํ”ฝ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋งŒํผ ๋ณธ๋Šฅ์ ์ด์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์—ฌ์ „ํžˆ ํ„ฐ๋ฏธ๋„์„ ์‚ฌ์šฉํ•˜๊ธฐ ์‹œ์ž‘ํ•ด์•ผ ํ•  ๋ช‡ ๊ฐ€์ง€ ์ข‹์€ ์ด์œ ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ”ท ๊ณ ์„ฑ๋Šฅ

๐Ÿ”ท ํ›„๋“œ ์•„๋ž˜์—์„œ ์ผ์–ด๋‚˜๋Š” ์ผ์— ๋Œ€ํ•œ ์‹ค์ œ ์ œ์–ด

๐Ÿ”ท ์ž‘์—… ์ž๋™ํ™”



์ฐธ๊ณ : Linux/macOS ํ„ฐ๋ฏธ๋„์˜ ๊ธฐ๋ณธ ๋ช…๋ น๋งŒ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์‹œ์ž‘ํ•˜๋‹ค



Linux ๋˜๋Š” macOS๋ฅผ ์šด์˜ ์ฒด์ œ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๊ฐ€์žฅ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ์…ธ์ธ bash ์…ธ์„ ์‚ฌ์šฉํ•  ๊ฐ€๋Šฅ์„ฑ์ด ํฝ๋‹ˆ๋‹ค.

ํ„ฐ๋ฏธ๋„์„ ์—ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œ์‹œ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.



ํ•ญํ•ด



cd(๋””๋ ‰ํ† ๋ฆฌ ๋ณ€๊ฒฝ) ๋ช…๋ น์„ ์‚ฌ์šฉํ•˜๋ฉด ์ปดํ“จํ„ฐ์˜ ํŒŒ์ผ ์‹œ์Šคํ…œ์„ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# Go to target folder
cd /target

# Go to parent folder
cd ..

# Go to home folder
cd ~


๋ชฉ๋ก



ls(๋ชฉ๋ก) ๋ช…๋ น์€ ํŒŒ์ผ๊ณผ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค.

# Print files located in the current directory.
ls

# Print files located in the target directory
ls targetparent/target


์˜ต์…˜



๋ช…๋ น์— -[option]์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋Œ€๋ถ€๋ถ„์˜ ๊ธฐ์กด ๋ช…๋ น์— ๋ช‡ ๊ฐ€์ง€ ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด:

# Print files located in the current directory with more details.
ls -l

# Print every files located in the current directory (hidden files as well).
ls -a


ํŒŒ์ผ ๋˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ ๋งŒ๋“ค๊ธฐ/์ œ๊ฑฐ



mkdir(make directory) ๋ช…๋ น์€ ์ƒˆ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

# Make a new directory in current directory.
mkdir directoryName



touch ๋ช…๋ น์€ ์ƒˆ ํŒŒ์ผ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

# Make a new file in current directory.
touch fileName



rm/rmdir(์ œ๊ฑฐ) ๋ช…๋ น์€ ํŒŒ์ผ/๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

# Delete an empty directory.
rmdir myDirectory

# Delete a file.
rm myFile

# Delete a directory recursively.
rm -r myDirectory



๋‚ด๊ฐ€ ์–ด๋”” ์žˆ์ง€?



pwd(์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ ์ธ์‡„) ๋ช…๋ น์€ ํ˜„์žฌ ์œ„์น˜๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

# Display current location in terminal.
pwd



ํŒŒ์ผ ๊ด€๋ฆฌ



cp(๋ณต์‚ฌ) ๋ช…๋ น์„ ์‚ฌ์šฉํ•˜๋ฉด ํŒŒ์ผ์„ ๋ณต์‚ฌํ•˜์—ฌ ๋ถ™์—ฌ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# Create a copy of a file to target location.
cp myFile target/



mv(์ด๋™) ๋ช…๋ น์€ ํŒŒ์ผ์„ ๋‹ค๋ฅธ ์œ„์น˜๋กœ ์ด๋™ํ•˜๊ฑฐ๋‚˜ ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ์œผ๋ฉด ์ด๋ฆ„์„ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.

# move file to target location.
mv myFile target/

# rename file.
mv myFile newName


ํ„ฐ๋ฏธ๋„์—์„œ ํŒŒ์ผ ์ฝ๊ธฐ/ํŽธ์ง‘



head/tail ๋ช…๋ น์€ ํŒŒ์ผ์˜ ์ผ๋ถ€๋ฅผ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค.

# Print 10 first lines of a file.
head myFile

# Print 10 last lines of a file.
tail myFile

# Print 30 first lines of a file.
head -n 30 myfile



cat(์—ฐ๊ฒฐ) ๋ช…๋ น์€ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ธ์‡„ํ•˜๊ณ , ์—ฌ๋Ÿฌ ํŒŒ์ผ์„ ํ•จ๊ป˜ ์—ฐ๊ฒฐํ•˜๊ณ , ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ํŽธ์ง‘ํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# Print content of a file in terminal.
cat myFile

# Print content of a file with numbers.
cat myFile -n

# Combine multiple files ogether.
cat fileOne fileTwo > target



๊ฒ€์ƒ‰



grep(์ •๊ทœ์‹ ๊ฐ€์ ธ์˜ค๊ธฐ)์€ ์ฃผ์–ด์ง„ ์‹์„ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

# Look for a word in a file.
grep word fileName

# Look for a word in a file without case sensitivity.
grep -i word fileName



๋‹ค์Œ์€ bash ๋ช…๋ น์— ๋Œ€ํ•œ ๊ฐœ์ธ ์น˜ํŠธ ์‹œํŠธ์ž…๋‹ˆ๋‹ค!

์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜‡

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ