【cat】Linuxコマンド_ファイル内のデータを表示・作成をする

Linuxでテキストファイル内のデータを表示するには「cat」コマンドを使用します。




↓↓↓ITエンジニアのおすすめ学習・開発環境の詳細へ↓↓↓

「cat」コマンドの使い方(Linux)

catコマンドはLinuxOS内のテキストファイルの中身を確認するために使用することができますが、ユーザーのホームディレクトリには、まだ中身を確認するようなファイルがありません。

まずは、事前にファイルを作成します。

$ vi test01.txt

※viコマンドについては別途まとめます。

viコマンドでファイルを作成すると以下のように編集画面になるので、キーボードの「i」を押して編集を実施し、適当にテキスト入力します。

test_file
test
test
~
~
"test01.txt" 3L, 20C

入力し終わった後は、「Esc」キーを押し、「:wq!」とキーボードを押します。
これば、入力したテキスト内容を保存して、終了するという意味になります。

ファイルを作成したら、lsコマンドでファイルが作成されていることを確認します。

$ ls -F
test01.txt

ファイルが作成されていることを確認したら「cat」コマンドでファイルの中身を確認します。

$ cat test01.txt
test01_file
test
test

複数のファイルを表示する

catコマンドでは引数を使って複数のファイルを指定し、表示することができます。

複数表示させるには以下のようにスペースで区切り、表示したいファイルを追加します。

$ cat test01.txt test02.txt
test01_file
test
test
test02_file
test
test

表示する内容に行番号を付ける

catで表示された内容に行番号を付けます。
オプションは「-n」となります。

$ cat -n test01.txt
     1  test01_file
     2  test
     3  test
     4
     5  test_1
     6  test_2
     7  test_3
     8
     9  test_4
    10  test_5

また、「-b」オプションでは行番号を付けますが、空白行はカウントしないようにします。

$ cat -b test01.txt
     1  test01_file
     2  test
     3  test

     4  test_1
     5  test_2
     6  test_3

     7  test_4
     8  test_5

すべての制御文字を表示する

「-A」オプションを使用することにより、すべての制御文字を表示します。
以下では改行の区切りに「$」コードが使用されていることが確認できます。

$ cat -A test01.txt
test01_file$
test$
test$
$
test_1$
test_2$
test_3$
$
test_4$
test_5$

複数のファイルを連携して新規ファイルに出力(保存)する

上記の「test01.txt」と「test02.txt」を連結して別の「test03.txt」という新規ファイルに保存する方法は「>」オプションを使用します。

$ cat test01.txt test02.txt > test03.txt

保存された「test03.txt」の内容を確認すると、「test01.txt」と「test02.txt」の中身のデータが連結されて保存されていることがわかります。

$ cat test03.txt
test01_file
test
test

test_1
test_2
test_3

test_4
test_5
test02_file
test
test

入力した内容をファイルに書き込む(ファイル作成)

catコマンドでは標準入力したデータをファイルに書き込むことができます。

以下のコマンドを実行することにより、書き込みの編集入力ができ、「Ctrl+D」で終了すると入力したデータが保存されたファイルが作成されます。

$ cat > test_10.txt
test_file10
test
test
↑書き込みたい内容を入力
終了する場合は「Ctrl+D」とキーボード入力します。

以下で新規に「test_10.txt」が作成されたことが確認できます。

$ ls -la
total 52
-rw-r--r-- 1 root root   59 Jul  5 04:42 test01.txt
-rw-r--r-- 1 root root   22 Jul  5 04:35 test02.txt
-rw-r--r-- 1 root root   81 Jul  5 04:50 test03.txt
-rw-r--r-- 1 root root   22 Jul  5 04:54 test_10.txt

catコマンドで中身を確認すると上記で入力した内容が書き込まれていることが確認できます。

$ cat test_10.txt
test_file10
test
test

「cat」コマンドオプション

「cat」コマンドのオプションは以下の通りとなります。

$ 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.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report cat translation bugs to <https://translationproject.org/team/>
Full documentation at: <https://www.gnu.org/software/coreutils/cat>
or available locally via: info '(coreutils) cat invocation'

「cat」コマンドの使い方は以上となります。

エンジニアのオンライン学習

エンジニアにおすすめのオンライン教材比較
ITエンジニアが自宅で学習ができるオンラインスクール比較

エンジニアのおすすめ学習「Progate」と「Udemy」比較

VPS_比較
最新情報をチェックしよう!