Linuxでは、catコマンドは最も一般的に使用されるコマンドの1つです。 cat
これは「連結」の略で、ファイルの内容を読み取り、書き込み、および標準出力に連結できます。 catコマンドは通常、1つ以上のテキストファイルの内容を表示し、あるファイルの内容を別のファイルに追加してファイルを結合し、新しいファイルを作成するために使用されます。
このチュートリアルでは、例を使用して、catコマンドを最大限に活用する方法を学習します。
一般的な構文
構文:
$ cat [options] [filename/filenames]
The [options]
コマンドにさまざまな引数を使用できます。
[filename/filenames]
ここで、表示する1つまたは複数のファイルの名前を指定できます。
新しいファイルを作成する
catコマンドを使用すると、新しいファイルを作成してコンテンツを追加できます。
構文:
$ cat > [filename]
この方法でファイルを作成すると、内容を入力できる新しい行にカーソルが置かれます。 希望のコンテンツを書いた後、あなたは使用することができます Ctrl + D 編集を終了して保存します。
例えば:
$ cat > Test1.txt
出力:
kali@kali:~/Desktop$ cat > Test1.txt
This is the first test file.
ファイルの内容を表示する
catコマンドを使用して、catに続けてファイル名を入力するだけで、ファイルの内容を表示できます。
構文:
$ cat [filename]
使用できます more
また less
ファイルをページごとに表示する場合はコマンド。
構文:
$ cat [filename] | more
$ cat [filename] | less
例えば:
$ cat Test1.txt
出力:
kali@kali:~/Desktop$ cat Test1.txt
This is the first test file.
kali@kali:~/Desktop$
複数のファイルの内容を表示する
catコマンドを使用すると、一度に複数のファイルの内容を表示できます。 catコマンドに続いて、スペースで区切られたファイル名のリストを使用できます。
構文:
$ cat [filename1] [filename2] ....
例:
$ cat Test1.txt Test2.txt Test3.txt
出力:
kali@kali:~/Desktop$ cat Test1.txt Test2.txt Test3.txt
This is the first test file.
This is second test file.
This is the third test file.
kali@kali:~/Desktop$
あるファイルから別のファイルにコンテンツをコピーする
catコマンドで(>)演算子を使用すると、あるファイルから別のファイルにコンテンツをコピーできます。
構文:
$ cat [filename1] > [filename2]
の場合 [filename2]
が存在しない場合、catコマンドは自動的に新しいファイルを作成し、のファイルをコピーします。 [filename1]
に [filename2]
。
例えば:
$ cat Test1.txt > Test2.txt
出力:
kali@kali:~/Desktop$ cat Test1.txt > Test2.txt
kali@kali:~/Desktop$ cat Test2.txt
This is the first test file.
kali@kali:~/Desktop$
コマンドはからコンテンツをコピーします Test1.txt
で上書きします Test2.txt
。 上書きする代わりに、(>>)演算子を使用して、ソーステキストファイルを宛先テキストファイルに追加することもできます。
構文:
$ cat [filename1] >> [filename2]
例えば:
$ cat Test1.txt >> Test2.txt
出力:
kali@kali:~/Desktop$ cat Test2.txt
This is the second test file.
kali@kali:~/Desktop$ cat Test1.txt
This is the first test file.
kali@kali:~/Desktop$ cat Test1.txt >> Test2.txt
kali@kali:~/Desktop$ cat Test2.txt
This is the second test file.
This is the first test file.
kali@kali:~/Desktop$
ファイルに行番号を表示する
空でないすべてのファイルは、catコマンドとファイル名とともに-bフラグを使用して表示できます。
構文:
$ cat -b [filename]
にとって example:
$ cat -b Test1.txt
出力:
kali@kali:~/Desktop$ cat -b Test1.txt
1 This is the first test file.
2 This 3 is 4 test 5 file.
kali@kali:~/Desktop$
文字を含まない行も表示する場合は、catコマンドおよびファイル名とともに-nフラグを使用できます。
構文:
$ cat -n [filename]
例えば:
$ cat -n Test1.txt
出力:
kali@kali:~/Desktop$ cat -n Test1.txt
1 This is the first test file.
2
3 This
4 is
5 test
6
7
8 file.
9
kali@kali:~/Desktop$
1つまたは複数のファイルを連結する
複数のファイルの内容を一度に表示する場合は、catコマンドを使用してそれらを連結します。
構文:
$ cat [filename1] [filename2]...
例えば:
$ cat Test1.txt Test2.txt Test3.txt
出力:
kali@kali:~/Desktop$ cat Test1.txt Test2.txt Test3.txt
This is the first test file.
This is the second test file.
This is the first test file.
This is the third test file.
kali@kali:~/Desktop$
(>)演算子を使用して新しいファイルを作成するか、既存のファイルを更新することにより、複数のファイルを1つのファイルに結合することもできます。
構文:
$ cat [filename1] [filename2]... > [filename3]
例えば:
$ cat Test1.txt Test2.txt Test3.txt > Test4.txt
以来 Test4.txt
存在しません、それは作成し、と呼ばれる新しいファイル Test4.txt
の内容を連結します Test1.txt
、 Test2.txt
、 と Test3.txt
の Test4.txt
。
出力:
kali@kali:~/Desktop$ cat Test1.txt Test2.txt Test3.txt > Test4.txt
kali@kali:~/Desktop$ cat Test4.txt
This is the first test file.
This is the second test file.
This is the first test file.
This is the third test file.
kali@kali:~/Desktop$
ファイルの各行の終わりを表示する
catコマンドを使用して、ファイルの行の終わりを判別できます。 行末にスペースなどの隠し文字があり、エラーが発生したり、問題が発見されたりすることがあります。 catコマンドを-Eフラグとともに使用して、行末文字としてドル($)を表示できます。
構文:
$ cat -E [filename]
例えば:
$ cat -E Test4.txt
出力:
kali@kali:~/Desktop$ cat -E Test4.txt
This is the first test file.$
$
$
This is the second test file.$
$
$
This is the first test file.$
$
$
This is the third test file.$
kali@kali:~/Desktop$
空白行を減らす
ファイルの内容を表示するとき、多くの空白行が表示されるのを邪魔する可能性があります。 catコマンドと-sを使用すると、出力から繰り返される空白行を削除できます。 catコマンドの-sオプションは、空白行を1つだけ表示し、繰り返される行を圧縮します。
構文:
$ cat -s [filename]
例えば:
$ cat -s Test4.txt
出力:
kali@kali:~/Desktop$ cat Test4.txt
This is the first test file.
This is the second test file.
This is the first test file.
This is the third test file.
kali@kali:~/Desktop$ cat -s Test4.txt
This is the first test file.
This is the second test file.
This is the first test file.
This is the third test file.
kali@kali:~/Desktop$
最初の出力は-sオプションを使用せず、2番目の出力は-sオプションを使用した後です。
タブを表示
-Tオプションとcatコマンドを併用すると、ファイルの内容とテキスト内のタブスペースが表示されます。
タブスペースは記号^ Iで示されます。
構文:
$ cat -T [filename]
例えば:
$ cat -T Test4.txt
出力:
kali@kali:~/Desktop$ cat Test4.txt
This is the first test file.
This is the second test file.
This is the first test file.
This is the third test file.
kali@kali:~/Desktop$ cat -T Test4.txt
^IThis is the first test file.
This is the second test file.
^IThis is the first test file.
This is the ^Ithird test file.
kali@kali:~/Desktop$
最初の出力は-Tオプションを使用せず、2番目の出力は-Tオプションを使用した後です。
ファイルの内容を逆の順序で表示する
tacコマンドは、catコマンドの逆です。 tacは、テキストファイルの内容とは逆の順序で出力を表示します。
構文:
$ tac [filename]
例えば:
$ tac Test4.txt
出力:
kali@kali:~/Desktop$ cat Test4.txt
This is the first test file.
This is the second test file.
This is the first test file.
This is the third test file.
kali@kali:~/Desktop$ tac Test4.txt
This is the third test file.
This is the first test file.
This is the second test file.
This is the first test file.
最初の出力はcatコマンドを使用して取得され、2番目の出力はtacコマンドを使用して取得されます。
catコマンドについて詳しく知りたい場合、または混乱がある場合は、helpコマンドを使用してください。
$ cat --help
出力:
kali@kali:~$ cat --help
Usage: cat [OPTION]… [FILE]…
Concatenate FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.
-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
Examples:
cat f - g Output f's contents, then standard input, then g's contents.
cat Copy standard input to standard output.
kali@kali:~$
結論
このチュートリアルでは、catコマンド、さまざまなオプションでの使用法、および例について学習しました。 Catは、複数の種類のテキストファイルを作成および表示できる便利なコマンドです。 catコマンドを使用すると、複数のファイルを複数の方法で同時に表示できます。