본문 바로가기
LINUX/AWS-MAVEN

[LINUX] cat 파일 내용 출력하기

by w1z 2023. 6. 16.

개요

cat 명령어는 리눅스에서 파일의 내용을 출력하는 기능을 수행합니다.

 

사용법

리눅스에서 사용되는 명령어는 모두 대소문자를 구분하니 주의해주세요.

cat 명령어에 대한 자세한 설명은 cat --help 명령어를 통해 볼 수 있습니다.

 

Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s), or standard input, to standard output.

  -A, --show-all           equivalent to -vET
  -b, --number-nonblank    number nonempty output lines, overrides -n
  -e                       equivalent to -vE
  -E, --show-ends          display $ at end of each line
  -n, --number             number all output lines
  -s, --squeeze-blank      suppress repeated empty output lines
  -t                       equivalent to -vT
  -T, --show-tabs          display TAB characters as ^I
  -u                       (ignored)
  -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB
      --help     display this help and exit
      --version  output version information and exit

With no FILE, or when FILE is -, read standard input.

Examples:
  cat f - g  Output f's contents, then standard input, then g's contents.
  cat        Copy standard input to standard output.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'cat invocation'


기본적인 사용법은 cat 명령어 뒤에 파일명을 입력하면 해당 파일의 내용을 표준출력으로 출력합니다.

 

cat filename

[root@peterdev test]# cat test1.txt
abc
def
qwe
poi
abc
abcdef
abcd

 

cat이나 tail, head 등의 명령어는 파이프(pipe) 를 통해 다른 명령어듣과 연계하거나 재지향(redirect)을 이용하여 다른 파일에 저장할 수 있습니다.

 

단독으로도 쓰임새가 많지만 이렇게 연계할 경우 매우 강력한 기능이 되니 아래 글들을 참고하셔서 꼭 기억해두시기 바랍니다.

[Linux] 파이프(pipe)에 대한 이해

[Linux] 재지향(Redirection)에 대한 이해

 

 

 

예제

ex) 리눅스 시스템 계정 목록 출력

[root@peterdev ~]# cat /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologinsystemd-network:x:192:192:systemd Network Management:/:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinpolkitd:x:999:997:User for polkitd:/:/sbin/nologinntp:x:38:38::/etc/ntp:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologinchrony:x:998:996::/var/lib/chrony:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

 

ex) 줄번호와 같이 출력

[root@peterdev test]# cat -n arr.c     1  #include<stdio.h>     2     3  int main(void)     4  {     5    int score[5];     6    scanf("%d", &score[0]);     7    scanf("%d", &score[1]);     8    scanf("%d", &score[2]);     9    scanf("%d", &score[3]);    10    scanf("%d", &score[4]);    11    12    printf("%dst score is %d\n", 1, score[0]);    13    printf("%dst score is %d\n", 2, score[1]);    14    printf("%dst score is %d\n", 3, score[2]);    15    printf("%dst score is %d\n", 4, score[3]);    16    printf("%dst score is %d\n", 5, score[4]);    17    18    return 0;    19  }

-n 옵션을 사용하면 줄번호와 같이 출력할 수 있습니다.

 

ex) 한페이지 씩 출력

[root@peterdev ~]# cat /var/log/messages | moreJun 17 03:27:02 peterdev rsyslogd: [origin software="rsyslogd" swVersion="8.24.0" x-pid="432" x-info="http://www.rsyslog.com"] rsyslogd was HUPedJun 17 04:01:01 peterdev systemd: Started Session 1462 of user root.Jun 17 04:01:01 peterdev systemd: Starting Session 1462 of user root.Jun 17 05:01:01 peterdev systemd: Started Session 1463 of user root.Jun 17 05:01:01 peterdev systemd: Starting Session 1463 of user root.Jun 17 05:48:22 peterdev chronyd[456]: Selected source 122.215.240.52Jun 17 06:01:01 peterdev systemd: Started Session 1464 of user root.Jun 17 06:01:01 peterdev systemd: Starting Session 1464 of user root.Jun 17 07:01:01 peterdev systemd: Started Session 1465 of user root.Jun 17 07:01:01 peterdev systemd: Starting Session 1465 of user root.Jun 17 08:01:01 peterdev systemd: Started Session 1466 of user root.Jun 17 08:01:01 peterdev systemd: Starting Session 1466 of user root.Jun 17 09:01:01 peterdev systemd: Started Session 1467 of user root.Jun 17 09:01:01 peterdev systemd: Starting Session 1467 of user root.Jun 17 10:01:01 peterdev systemd: Started Session 1468 of user root.Jun 17 10:01:01 peterdev systemd: Starting Session 1468 of user root.Jun 17 11:01:01 peterdev systemd: Started Session 1469 of user root.Jun 17 11:01:01 peterdev systemd: Starting Session 1469 of user root.Jun 17 12:01:01 peterdev systemd: Started Session 1470 of user root.Jun 17 12:01:01 peterdev systemd: Starting Session 1470 of user root.Jun 17 12:39:28 peterdev dhclient[757]: DHCPREQUEST on eth0 to 169.254.169.254 port 67 (xid=0x5f254c90)--More--
more 명령어를 파이프로 연결해서 사용하면 한페이지 씩 출력할 수 있습니다.
 

ex) 파일 복사

[root@peterdev test]# cat test.c > copy.c

cat 명령어의 출력을 다른 파일로 재지향하면 파일을 복사할 수 있습니다.



 

-Peter의 우아한 프로그래밍

여러분의 댓글은 저에게 크나큰 힘이 됩니다. 오류 및 의견 남겨주시면 감사하겠습니다.