๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Computer Science/Linux (Ubuntu)

10 Pipe, Filter, Redirection

by Dowon Kang 2024. 1. 28.

 

ํŒŒ์ดํ”„ (|)

ํŒŒ์ดํ”„๋Š” ๋‘ ๊ฐœ์˜ ๋ช…๋ น์–ด๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ํ•œ ๋ช…๋ น์–ด์˜ ์ถœ๋ ฅ์ด ๋‹ค๋ฅธ ๋ช…๋ น์–ด์˜ ์ž…๋ ฅ์œผ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

command1์˜ ์ถœ๋ ฅ์ด command2์˜ ์ž…๋ ฅ์œผ๋กœ ์ „๋‹ฌ๋˜์–ด ๊ฒฐ๊ณผ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค.

 

 

ํ•„ํ„ฐ (grep)

grep์€ ํŠน์ • ํŒจํ„ด์ด๋‚˜ ํ…์ŠคํŠธ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” ํ‘œ์ค€ ์ž…๋ ฅ์—์„œ ํ…์ŠคํŠธ๋ฅผ ์ฝ์–ด ํŒจํ„ด์— ๋งž๋Š” ๋ผ์ธ๋งŒ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ๋ช…๋ น์€ "filename" ํŒŒ์ผ์—์„œ "pattern"์ด๋ผ๋Š” ๋ฌธ์ž์—ด์„ ์ฐพ์•„ ํ•ด๋‹น ๋ผ์ธ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

 

๋ฆฌ๋””๋ ‰์…˜

๋ฆฌ๋””๋ ‰์…˜์€ ๋ช…๋ น์˜ ์ž…์ถœ๋ ฅ์„ ์กฐ์ ˆํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

  1. >๋Š” ์ถœ๋ ฅ์„ ํŒŒ์ผ๋กœ ๋ฆฌ๋””๋ ‰์…˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋ฉฐ, >>๋Š” ๊ธฐ์กด ํŒŒ์ผ์— ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  2. <๋Š” ํŒŒ์ผ์„ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

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

๋ช…๋ น์˜ ์ถœ๋ ฅ์„ "output.txt" ํŒŒ์ผ๋กœ ๋ฆฌ๋””๋ ‰์…˜ํ•ฉ๋‹ˆ๋‹ค.

 

ํŒŒ์ดํ”„์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

command1 ์˜ ์ถœ๋ ฅ์„ command2 ๋กœ ์ „๋‹ฌํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ "output.txt" ํŒŒ์ผ๋กœ ๋ฆฌ๋””๋ ‰์…˜ํ•ฉ๋‹ˆ๋‹ค.

 

 

 

Example

 

 

 

Foreground & Background

ํฌ๊ทธ๋ผ์šด๋“œ(Foreground)์™€ ๋ฐฑ๊ทธ๋ผ์šด๋“œ(Background)๋Š” ์ปดํ“จํ„ฐ ์šด์˜์ฒด์ œ์™€ ํ”„๋กœ์„ธ์Šค ์‹คํ–‰ ๊ด€๋ จ ์šฉ์–ด๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

 

ํฌ๊ทธ๋ผ์šด๋“œ (Foreground)

  • ํฌ๊ทธ๋ผ์šด๋“œ๋Š” ์‚ฌ์šฉ์ž์™€ ์ง์ ‘ ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š” ํ”„๋กœ์„ธ์Šค๋‚˜ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
  • ์‚ฌ์šฉ์ž๊ฐ€ ์‹คํ–‰ํ•œ ํ”„๋กœ๊ทธ๋žจ์ด๋‚˜ ํ˜„์žฌ ํ™œ์„ฑํ™”๋œ ์ฐฝ์ด ํฌ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰ ์ค‘์ผ ๋•Œ, ํ•ด๋‹น ํ”„๋กœ๊ทธ๋žจ์€ ์‚ฌ์šฉ์ž์™€ ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ์— ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ผ๋ฐ˜์ ์œผ๋กœ, ์‚ฌ์šฉ์ž๊ฐ€ ํ‚ค๋ณด๋“œ ๋˜๋Š” ๋งˆ์šฐ์Šค๋ฅผ ํ†ตํ•ด ํ”„๋กœ๊ทธ๋žจ๊ณผ ์ƒํ˜ธ์ž‘์šฉํ•  ๋•Œ, ํ•ด๋‹น ํ”„๋กœ๊ทธ๋žจ์€ ํฌ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

๋ฐฑ๊ทธ๋ผ์šด๋“œ (Background)

  • ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋Š” ์‚ฌ์šฉ์ž์™€ ์ง์ ‘ ์ƒํ˜ธ์ž‘์šฉํ•˜์ง€ ์•Š๋Š” ํ”„๋กœ์„ธ์Šค๋‚˜ ์ž‘์—…์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
  • ์‚ฌ์šฉ์ž๊ฐ€ ์–ด๋–ค ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๊ณ  ๋‚˜์„œ, ํ•ด๋‹น ํ”„๋กœ๊ทธ๋žจ์ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰ ์ค‘์ด๋ฉด ์‚ฌ์šฉ์ž๋Š” ๋‹ค๋ฅธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰ ์ค‘์ธ ํ”„๋กœ์„ธ์Šค๋Š” ๋ณดํ†ต ์‹œ์Šคํ…œ ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, ์‚ฌ์šฉ์ž์˜ ์ฃผ์˜๋ฅผ ๋Œ์ง€ ์•Š๊ณ  ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

 

ํฌ๊ทธ๋ผ์šด๋“œ์™€ ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋Š” ์‚ฌ์šฉ์ž ๊ฒฝํ—˜๊ณผ ์‹œ์Šคํ…œ ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š” ์ค‘์š”ํ•œ ์ปจ์…‰์ž…๋‹ˆ๋‹ค. ํ”„๋กœ์„ธ์Šค๋ฅผ ํฌ๊ทธ๋ผ์šด๋“œ ๋˜๋Š” ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์€ ์‚ฌ์šฉ์ž์˜ ์š”๊ตฌ์— ๋”ฐ๋ผ ์ ์ ˆํ•˜๊ฒŒ ์„ ํƒ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ถ€ ์ž‘์—…์€ ์‚ฌ์šฉ์ž์˜ ์ง์ ‘์ ์ธ ๊ด€์—ฌ๋ฅผ ํ•„์š”๋กœ ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰๋˜๋ฉฐ, ๋‹ค๋ฅธ ์ž‘์—…์€ ์‚ฌ์šฉ์ž์™€์˜ ์ƒํ˜ธ์ž‘์šฉ์ด ํ•„์š”ํ•˜๋ฏ€๋กœ ํฌ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

 

 

์ด๋Ÿฌํ•œ ๋ฆฌ๋ˆ…์Šค ๋ช…๋ น์–ด๋“ค์€ ์œ ๋‹‰์Šค ์ฒด๊ณ„์—์„œ ํŒŒ์ƒ๋œ ๊ฒƒ์œผ๋กœ, ํ„ฐ๋ฏธ๋„์—์„œ ํšจ๊ณผ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ช…๋ น์–ด๋“ค์„ ์กฐํ•ฉํ•˜์—ฌ ๋ณต์žกํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 


ํŒŒ์ดํ”„ (Pipe)
Definition: A command in Unix-like operating systems that connects the output of one command to the input of another, allowing them to work together.


ํ•„ํ„ฐ (grep)
Definition: A command-line utility used to search for a specific pattern or text in a stream of data, often used in conjunction with other commands.

 

๋ฆฌ๋””๋ ‰์…˜ (Redirection)
Definition: The process of adjusting the input or output of a command in Unix-like systems. ">" redirects output to a file, ">>" appends output to a file, and "<" redirects input from a file.

 

ํฌ๊ทธ๋ผ์šด๋“œ (Foreground)
Definition: The active execution state of a program or process that directly interacts with the user, typically receiving input and providing output in real-time.


๋ฐฑ๊ทธ๋ผ์šด๋“œ (Background)
Definition: The execution state of a program or process that runs independently of user interaction, allowing users to perform other tasks while it operates.


'Computer Science > Linux (Ubuntu)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

12 ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํ”„๋กœ๊ทธ๋žจ  (0) 2024.02.08
11 ์‘๊ธ‰๋ณต๊ตฌ (feat, GRUB ๋ถ€ํŠธ๋กœ๋”)  (0) 2024.02.08
9 tar & find (+cron, at)  (0) 2024.01.28
8 dpkg & apt  (1) 2024.01.28
7 Hard Link & Soft(symbolic) Link  (0) 2024.01.28

๋Œ“๊ธ€