Computer Science/Linux (Ubuntu)

10 Pipe, Filter, Redirection

Dowon Kang 2024. 1. 28. 09:43

 

νŒŒμ΄ν”„ (|)

νŒŒμ΄ν”„λŠ” 두 개의 λͺ…λ Ήμ–΄λ₯Ό μ—°κ²°ν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€. ν•œ λͺ…λ Ήμ–΄μ˜ 좜λ ₯이 λ‹€λ₯Έ λͺ…λ Ήμ–΄μ˜ μž…λ ₯으둜 μ „λ‹¬λ©λ‹ˆλ‹€.
예λ₯Ό λ“€μ–΄, λ‹€μŒκ³Ό κ°™μ΄ μ‚¬μš©ν•  μˆ˜ μžˆμŠ΅λ‹ˆλ‹€.

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.