IO Redirection

leeez·2021년 9월 3일
0

Linux

목록 보기
2/2
post-thumbnail

output (>)

  • redirection : 화면으로 출력되는 것이 기본이나, 출력 방향을 다른 방향으로 돌려서 파일로 저장시키는 방식
    -(> | 1>) : Standard Output를 redirection
    -(2>) : Standard Error를 redirection
ls -l > {파일 이름} : ls -l 결과로 출력된 내용을 파일로 저장하고 싶을 때
cat {파일 이름} : 파일 읽기
예)
ls -l > file1.txt
cat file1.txt
-> file1.txt의 내용....
rm file1.txt
rm file1.txt > result.txt 
// file1.txt가 지워진 다음이기 때문에 standard error 발생 및 >(redirection) 불가
rm file1.txt 2> error.log 
// 2>는 standard error를 redirection하기 때문에 error.log standard error가 기입되어 있음

rm file1.txt 1> result.txt 2> error.log 
// file1.txt를 성공적으로 실행되면 출력 결과(standard output)를 result.txt로 redirection, 실패하면 출력 결과(standard error)를 error.log로 redirection

input (<)

cat ~ : 사용자 키보드를 통해서 입력되는 정보(standard input)를 받는다.
cat < hello.txt
// cat은 기본적으로 키보드의 입력을 받지만, redirection을 하게 되면, hello.txt 파일의 내용을 입력으로 받으면서 출력함

head {파일 이름} : 파일 내용을 앞에서 10줄 출력 
head -n{숫자} {파일 이름} : 파일 내용의 앞에서 {숫자}줄 만큼 출력

1. cat(head) {파일 이름} : Command-line Arguments(인자)로 파일을 받아 읽는다.
2. cat(head) < {파일 이름} : Standard Input(표준 입력)으로 파일을 받아 읽는다.



표준 입출력 이용한 예시)
head -n1 < linux.txt > one.txt
// linux.txt이 redirection돼서 head 프로세스에 입력이 되고, 그 출력 결과를 one.txt에 저장한다.
즉, linux.txt라는 파일에 있는 내용 전체에서 첫번째 행만 one.txt로 저장 

append

ls -al **>>** result.txt
// result.txt에 ls -al의 결과를 덧붙임(append)

{명령어} <<{텍스트}
// 다음에 {텍스트}가 나타나면 입력이 끝난다

ls -al > /dev/null
// /dev/null은 리눅스 쓰레기통 역할. 명령어의 결과가 파일에 써지지도, 화면에 출력되지도 않음!

0개의 댓글