매우 자주 쓰이는 명령어. concatenate의 약자다.
단순 파일 내용물 확인, 혹은 파일내용물을 서로 붙일 때 사용된다.
cat [OPTION]... [FILE]...
OPTION
: cat
동작을 설정하는데 사용
FILE
: 대상 파일, 혹은 파일들. 없을 경우 사용자 입력값을 가지고 작동한다.
그냥 사용할 경우 파일 내용물을 보여준다.
sycho@DESKTOP-4RPUOID:~$ cat example.txt
sed is a stream editor.
A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).
While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.
But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.
sycho@DESKTOP-4RPUOID:~$ cat example.txt data.txt
sed is a stream editor.
A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).
While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.
But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.
Greg Yoder 833
Sarah Mullins 117
Aimee Lewis 41
Kelli Cooper 475
Shelby Aguilar 208
Eddie Welch 313
Ashley Kelley 173
Isaiah Flores 835
Kathleen Jordan 85
Jessica Morgan 917
\t
, \n
제외 출력이 안되는 문자들도 출력 (-v
)sycho@DESKTOP-4RPUOID:~$ cat -v example.txt
sed is a stream editor.
A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).
While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.
But it is sedM-bM-^@M-^Ys ability to filter text in a pipeline which particularly distinguishes it from other types of editors.
-E
)$
을 붙이도록 설정.sycho@DESKTOP-4RPUOID:~$ cat -E example.txt
sed is a stream editor.$
A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).$
While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.$
But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.$
sycho@DESKTOP-4RPUOID:~$ cat -e example.txt
sed is a stream editor.$
A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).$
While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.$
But it is sedM-bM-^@M-^Ys ability to filter text in a pipeline which particularly distinguishes it from other types of editors.$
*
)$ cat *.txt
(너무 많아서 생략)
-n
)sycho@DESKTOP-4RPUOID:~$ cat -n example.txt
1 sed is a stream editor.
2 A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).
3 While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.
4 But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.
sycho@DESKTOP-4RPUOID:~$ cat -n example.txt data.txt
1 sed is a stream editor.
2 A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).
3 While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.
4 But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.
5 Greg Yoder 833
6 Sarah Mullins 117
7 Aimee Lewis 41
8 Kelli Cooper 475
9 Shelby Aguilar 208
10 Eddie Welch 313
11 Ashley Kelley 173
12 Isaiah Flores 835
13 Kathleen Jordan 85
14 Jessica Morgan 917
-T
)^I
으로 변환해서 출력한다.sycho@DESKTOP-4RPUOID:~$ cat example2.txt
hi world.
I'm using tab right now.
sycho@DESKTOP-4RPUOID:~$ cat -T example2.txt
hi^Iworld.
I'm^Iusing^Itab^Iright^Inow.
-s
)가끔 실수로 파일에 빈 문장이 여러개가 연속으로 나올 때가 있는데 그걸 제거하는 명령어.
그래도 빈 문장 하나는 꼭 남겨놓는다.
sycho@DESKTOP-4RPUOID:~$ cat blank.txt
peek-a-boo
sycho@DESKTOP-4RPUOID:~$ cat -s blank.txt
peek-a-boo
VI
나 VIM
을 사용하지 않고 파일을 만들 때 활용이 가능하다.
방법은 생성할 파일 이름을 지정한 후 redirection을 수행, 이후 입력할 내용물을 적은 다음 Ctrl + D
를 누르면 (stdin 입력 종료) 입력값을 전부 받았다고 command가 판단, 이것을 그대로 출력을 하면서 그 내용물이 방금 지정한 파일 이름에 저장이 된다.
sycho@DESKTOP-4RPUOID:~$ cat > catfile.txt
hello
my name is
sycho
sycho@DESKTOP-4RPUOID:~$ cat catfile.txt
hello
my name is
sycho
sycho@DESKTOP-4RPUOID:~$ cat >> catfile.txt
hello2
my name is
sycho2?
sycho@DESKTOP-4RPUOID:~$ cat catfile.txt
hello
my name is
sycho
hello2
my name is
sycho2?
sycho@DESKTOP-4RPUOID:~$ cat catfile.txt > catfile2.txt
sycho@DESKTOP-4RPUOID:~$ cat catfile2.txt
hello
my name is
sycho
hello2
my name is
sycho2?
sycho@DESKTOP-4RPUOID:~$ cat catfile.txt catfile2.txt > catfile3.txt
sycho@DESKTOP-4RPUOID:~$ cat catfile3.txt
hello
my name is
sycho
hello2
my name is
sycho2?
hello
my name is
sycho
hello2
my name is
sycho2?
sycho@DESKTOP-4RPUOID:~$ cat catfile2.txt >> catfile3.txt
sycho@DESKTOP-4RPUOID:~$ cat -n catfile3.txt
1 hello
2 my name is
3 sycho
4 hello2
5 my name is
6 sycho2?
7 hello
8 my name is
9 sycho
10 hello2
11 my name is
12 sycho2?
13 hello
14 my name is
15 sycho
16 hello2
17 my name is
18 sycho2?
more
, less
를 활용해 결과물 편리하게 보기more
이나 less
를 적용하면 스크롤을 하고, 이래저래 명령어에서 지원하는 단축키를 활용해서 탐색하는 것이 가능하다.$ cat *.txt | more
$ cat *.txt | less
cat
랑 다르게 순서를 반대로 해서 출력하는 tac
command가 존재한다.sycho@DESKTOP-4RPUOID:~$ cat catfile.txt
hello
my name is
sycho
hello2
my name is
sycho2?
$ tac catfile.txt
sycho2?
my name is
hello2
sycho
my name is
hello
xxd
command에 -b
를 활용해가지고 텍스트를 이진 파일 형태로 출력하는게 가능하다.sycho@DESKTOP-4RPUOID:~$ cat catfile.txt | xxd -b
00000000: 01101000 01100101 01101100 01101100 01101111 00001010 hello.
00000006: 01101101 01111001 00100000 01101110 01100001 01101101 my nam
0000000c: 01100101 00100000 01101001 01110011 00001010 01110011 e is.s
00000012: 01111001 01100011 01101000 01101111 00001010 01101000 ycho.h
00000018: 01100101 01101100 01101100 01101111 00110010 00001010 ello2.
0000001e: 01101101 01111001 00100000 01101110 01100001 01101101 my nam
00000024: 01100101 00100000 01101001 01110011 00001010 01110011 e is.s
0000002a: 01111001 01100011 01101000 01101111 00110010 00111111 ycho2?
00000030: 00001010
hexdump
command에 -C
를 활용해가지고 텍스트를 16진수 형태 파일로 출력하는게 가능하다.sycho@DESKTOP-4RPUOID:~$ cat catfile.txt | hexdump -C
00000000 68 65 6c 6c 6f 0a 6d 79 20 6e 61 6d 65 20 69 73 |hello.my name is|
00000010 0a 73 79 63 68 6f 0a 68 65 6c 6c 6f 32 0a 6d 79 |.sycho.hello2.my|
00000020 20 6e 61 6d 65 20 69 73 0a 73 79 63 68 6f 32 3f | name is.sycho2?|
00000030 0a |.|
00000031
sed
)sycho@DESKTOP-4RPUOID:~$ cat catfile.txt | sed -n '1,3p'
hello
my name is
sycho
redirection 및 heredoc redirection을 활용한다. EOF
대신 다른 문자를 입력하면 종료하게 하고 싶으면 그냥 << EOF
를 원하는 것으로 바꾸면 된다.
보통 스크립트 등에서 사용자에게 저장할 파일 내용물을 지정하라고 할 때 활용된다.
sycho@DESKTOP-4RPUOID:~$ cat > list.txt << EOF
> mic
> keyboard
> mouse
> EOF
sycho@DESKTOP-4RPUOID:~$ cat list.txt
mic
keyboard
mouse