[]
Linuxのほとんどの設定ファイル、ログファイル、そしてシェルスクリプトはテキストファイル形式を使用しているため、Linuxにはさまざまなテキストエディタが用意されていますが、これらのファイルの中身を見るだけなら、シンプルなコマンド-catを使用します。
猫のマニュアルにはこう書かれています:
catコマンドはファイルの内容を読み取り、標準デバイスに出力します。
catはlinuxの組み込みコマンドです。 ほとんどのLinuxディストリビューションに組み込まれています。次に使い方を学びましょう。
1.文書内容の表示
一番簡単な方法は、「cat file_name」と入力することです。
# cat /etc/issue
CentOS release 5.10 (Final)
Kernel
on an \m
2.行番号の同時表示
内容の多い設定ファイルを読み込む場合、行番号を同時に表示する方が簡単で、これは -n パラメータを追加することで実現できます。
# cat -n /etc/ntp.conf
1 # Permit time synchronization our time resource but do not
2 # permit the source to query or modify the service on this system
3 restrict default kod nomodify notrap nopeer noquery
4 restrict -6 default kod nomodify notrap nopeer noquery
5
6 # Permit all access over the loopback interface. This could be
7 # tightened as well, but to do so would effect some of the
8 # administration functions
9 restrict .1
10 restrict -6 ::1
3.スペース行以外の行頭の行番号表示
nパラメータと同様に、-bでも行番号を表示することができます。違いは、-bは空白でない行の前にのみ行番号を表示することです。
#cat -b /etc/ntp.conf
1 # Permit time synchronization our time resource but do not
2 # permit the source to query or modify the service on this system
3 restrict default kod nomodify notrap nopeer noquery
4 restrict -6 default kod nomodify notrap nopeer noquery
5 # Permit all access over the loopback interface. This could be
6 # tightened as well, but to do so would effect some of the
7 # administration functions
8 restrict .1
9 restrict -6 ::1
4.タブ表示
テキストにタブを表示したい場合。 Tパラメータを使用してください。 入力結果に ^I と表示されます。
# cat -T /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
.1^I^Ilocalhost.localdomain localhost
::1^I^Ilocalhost6.localdomain6 localhost6
5.改行の表示
Eパラメータは、改行文字を示すために各行の末尾に$を使用します。以下に示すように
# cat -E /etc/hosts
# Do not remove the following line, or various programs$
# that require network functionality will fail.$
.1 localhost.localdomain localhost$
::1 localhost6.localdomain6 localhost6$
6.タブと改行の同時表示
Aパラメータは、-Tと-Eの効果を同時に得たい場合に使用します。
# cat -A /etc/hosts
# Do not remove the following line, or various programs$
# that require network functionality will fail.$
.1^I^Ilocalhost.localdomain localhost$
::1^I^Ilocalhost6.localdomain6 localhost6$
7.分割画面表示
ファイルが画面の大きさを超える場合は、catコマンドと他のコマンドを組み合わせて、ファイルを別の画面に表示することができます。接続にはパイプ文字を使います。
# cat /proc/meminfo | less
# cat /proc/meminfo | more
lessとmoreの違いは、lessパラメータはページアップとページダウンができることです。一方、more はスペースを使ってスクロールダウンすることしかできません。
8.2つの文書の内容の同時表示
rootフォルダにはlinuxとdesktopという2つのファイルがあり、それぞれに以下の内容が含まれています。
Linux:ubuntu、centos、redhat、mint、slackware
デスクトップ : gnome kde, xfce, enlightenment, and cinnamon
2つのファイルの内容を同時に見たいときは、次のようにします。
# cat /root/linux /root/desktop
ubuntu
centos
redhat
mint
slackware
gnome
kde
xfce
enlightment
cinnamon
9.並べ替え表示
同様に、catコマンドを他のコマンドと組み合わせて出力をカスタマイズすることもできます。 また、catコマンドを他のコマンドと組み合わせて、出力をカスタマイズすることもできます。 例えば、sort を他のコマンドと組み合わせることで、パイプラインによってソートされた順序で内容を表示することができます。例
# cat /root/linux | sort
centos
mint
redhat
slackware
Ubuntu
10.入力リダイレクト
表示出力をスクリーンまたは別のファイルにリダイレクトすることもできます。 シンボル > を使用するだけで、別のファイルへの出力を生成できます。
# cat /root/linux > /root/linuxdistro
上記のコマンドは、/root/linuxと全く同じ内容のlinuxdistroというファイルを生成します。
11.新しいドキュメント
Linuxで新しいファイルを作成する方法はいくつかあります。そのひとつがcatを使う方法です。
# cat > operating_system
Unix
Linux
Windows
MacOS
cat > operatingsystemと入力すると、operatingsystemのファイルが生成されます。そしてその下に空白の行が現れます。この時点で何か入力することができます。例えば、Unix、Linux、Windows、MacOSと入力し、入力が終わったら、Ctrl-Dを押してファイルを保存し、catを終了してください。 カレントフォルダに、今入力した内容を含むoperating_systemというファイルが見つかるでしょう。
12.ドキュメントへの追記
文字を2つ使用すると、1つ目のファイルの内容が2つ目のファイルの末尾に追加されます。 例
# cat /root/linux >> /root/desktop
# cat /root/desktop
これは、/root/linuxの内容を/root/desktopファイルの最後に追加します。
つ目のファイルの内容は次のようになります。
gnome
kde
xfce
enlightment
cinnamon
ubuntu
centos
redhat
mint
slackware
13.入力のリダイレクション
< コマンドでファイルを cat に入力できます。
# cat < /root/linux
上記のコマンドは、/root/linuxの内容をcatの入力として示しています。画面には次のように表示されます。
ubuntu
centos
redhat
mint
slackware
その意味をより明確に示すには、以下のコマンドを使用します:
# cat < /root/linux | sort > linux-sort
コマンドは次のように動作します:/root/linuxから読み込み、ソートし、結果を出力し、新しいlinux-sortファイルを生成します。
次にlinux-sortの中身を見てください:
centos
mint
redhat
slackware
ubuntu
以上が、キャット・コマンドの日常的な基本的な使い方です。 キャットコマンドのマニュアルでもっと詳しく学ぶことができます。





