GNU/Linux >> Linux の 問題 >  >> Linux

14 Linux での便利な「cat」コマンドの例

はじめに

Unix を学ぶときは、最初に cat コマンドを学ぶのが一般的です。ペットに優しい人なら覚えやすい略語です。このコマンドは、指定した要素を 1 つのパッケージに連結します。ファイルを調べる最も簡単な方法は、連結である cat を使用することです コマンド。

この記事では、Linux の cat コマンドの使用方法を学習します。前に説明したように、cat は Concatenate の略です。 cat コマンドは、ファイルの内容の表示、ファイルの作成、ファイルの編集などに使用される Linux のファイル管理コマンドです。これは、Linux で最も頻繁に使用される基本コマンドの 1 つです。 Linux の cat コマンドは、GNU GPLv3 ライセンスの下でリリースされたオープン ソース アプリケーションです。デフォルトでは、Linux ディストリビューションにプリインストールされています。この記事では、14 の最も重要な Linux の cat コマンドを例を挙げて紹介します。

cat コマンドを使用する構文は次のとおりです:

# cat [OPTION]... [FILE]...

1.ファイルの内容を表示

Linux の cat コマンドを使用して、ファイルの内容を表示できます。以下のコマンドを参照してください。

$ cat test1.txt
This is a test file....

2.複数のファイルの内容を表示

一度に複数のファイルの内容を表示したい場合は、cat コマンドを使用して実行できます。ここには、text1.txt と text2.txt という 2 つのファイルがあります。それでは、これら 2 つのファイルの内容を確認してみましょう。

$ cat test1.txt test2.txt
This is a test file...
This is another test file...

3.パイプ (|) を使用した less/more コマンドで Linux の cat コマンドを使用する方法

単一の画面では表示できない長いファイルを表示したい場合は、パイプ (|) を使用してコマンド less で cat コマンドを使用します。以下のコマンドを参照してください。

$ cat /etc/login.defs | less

4.同じ拡張子を持つすべてのファイルのコンテンツを一度に表示

同じ拡張子のファイルが多数あるとします。拡張子が *.txt のテキスト ファイルがいくつかあるとします。次に示すように、Linux の cat コマンドを使用してこれらのファイルの内容を表示します。

$ cat *.txt
This is a test file....
Ubuntu 16.04 Long Term Support
Welcome to thegeekdiary.com

5.新しいファイルを作成

Linux の cat コマンドに記号 > を付けて使用すると、新しいファイルを作成できます。 (より大きい)。コマンド (cat> test.txt) を実行した後、そのファイルに保存するコンテンツを入力する必要があります。テキストを入力し、キーボードで CTRL+D を押して、ファイルを作成して保存します。

$ cat > test.txt
This is a test file...
ctrl+d

6.あるファイルのコンテンツを別のファイルにダンプ

あるコンテンツを含むファイルがあり、そのすべてのコンテンツを新しいファイルにコピーしたいとします。これは、シンボル> (より大きい) を指定した cat コマンドを使用して行うことができます。ここでは、いくつかのコンテンツを含む test1.txt という名前のファイルがあり、そのすべてのコンテンツを myfile.txt という名前の新しいファイルにダンプしたいと考えています。

$ cat test1.txt > myfile.txt

# Output
 
$ cat myfile.txt
This is a test file....

7.複数のファイルのコンテンツを新しいファイルにダンプ

Linux の cat コマンドを使用して、複数のファイルの内容を新しいファイルにダンプすることもできます。ここでは、test1.txt と test2.txt の内容を newfile.txt という名前の新しいファイルにダンプしています。

$ cat test1.txt test2.txt > newfile.txt

# Output

$ cat newfile.txt 
This is a test file....
Welcome to thegeekdiary.com

8.作成済みのファイルにコンテンツを追加する (ファイルの編集)

シンボル>> (2 倍以上) を指定した cat コマンドを使用して、作成済みのファイルにコンテンツ (書き込みコンテンツ) を追加できます。以下のコマンドを実行した後、そのファイルに保存するコンテンツを入力し、キーボードの CTRL+D を押してファイルを保存して閉じる必要があります。以下のコマンドを参照してください。

$ cat >> test1.txt

9.すべての出力行に番号を付ける

引数 -n を指定して Linux の cat コマンドを使用すると、任意のファイルのすべての出力行に番号を付けることができます。 .以下のコマンドを参照してください。

$ cat -n /etc/passwd
     1	root:x:0:0:root:/root:/bin/bash
     2	daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
     3	bin:x:2:2:bin:/bin:/usr/sbin/nologin
     4	sys:x:3:3:sys:/dev:/usr/sbin/nologin
     5	sync:x:4:65534:sync:/bin:/bin/sync
     6	games:x:5:60:games:/usr/games:/usr/sbin/nologin
     7	man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
     8	lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
     9	mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
    10	news:x:9:9:news:/var/spool/news:/usr/sbin/nologin

10.各行の終わりを表示

引数 -E を指定した cat コマンド $ を配置します 各出力行の終わりに署名します。このコマンドは、各出力行の終わりを確認する場合に役立ちます。以下のコマンドの例を参照してください。

$ cat -E /etc/passwd
root:x:0:0:root:/root:/bin/bash$
bin:x:1:1:bin:/bin:/sbin/nologin$
daemon:x:2:2:daemon:/sbin:/sbin/nologin$
adm:x:3:4:adm:/var/adm:/sbin/nologin$
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin$
sync:x:5:0:sync:/sbin:/bin/sync$
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown$
halt:x:7:0:halt:/sbin:/sbin/halt$
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin$
operator:x:11:0:operator:/root:/sbin/nologin$
games:x:12:100:games:/usr/games:/sbin/nologin$
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin$
nobody:x:99:99:Nobody:/:/sbin/nologin$
dbus:x:81:81:System message bus:/:/sbin/nologin$
...

11.空でない出力行のみに番号を付ける

以下のコマンドは、空でない行にのみ番号を付け、空白の行には番号を付けません。

$ cat -b /etc/login.defs 
     1	#
     2	# /etc/login.defs - Configuration control definitions for the login package.
     3	#
     4	# Three items must be defined:  MAIL_DIR, ENV_SUPATH, and ENV_PATH.
     5	# If unspecified, some arbitrary (and possibly incorrect) value will
     6	# be assumed.  All other items are optional - if not specified then
     7	# the described action or option will be inhibited.
     8	#
     9	# Comment lines (lines beginning with "#") and blank lines are ignored.
    10	#
    11	# Modified for Linux.  --marekm

    12	# REQUIRED for useradd/userdel/usermod
    13	#   Directory where mailboxes reside, _or_ name of file, relative to the
    14	#   home directory.  If you _do_ define MAIL_DIR and MAIL_FILE,
    15	#   MAIL_DIR takes precedence.

12.タブ文字を表示

引数 -T を指定して Linux の cat コマンドを使用できます タブ文字を表示します。 「タブ」を含むすべての行は、^I として示されます。 .以下のコマンドを参照してください。

$ cat -T /etc/login.defs 
################# OBSOLETED BY PAM ##############
#^I^I^I^I^I^I#
# These options are now handled by PAM. Please^I#
# edit the appropriate file in /etc/pam.d/ to^I#
# enable the equivelants of them.
#
###############

################# OBSOLETED #######################
#^I^I^I^I^I^I  #
# These options are no more handled by shadow.    #
#                                                 #
# Shadow utilities will display a warning if they #
# still appear.                                   #
#                                                 #
###################################################

# CLOSE_SESSIONS
# LOGIN_STRING
# NO_PASSWORD_CONSOLE
# QMAIL_DIR

13.インストールされている cat コマンドのパッケージのバージョンを確認してください

以下のコマンドは、インストールされている cat コマンド パッケージのバージョン、作成者とライセンスの詳細を表示します。

$ cat --version

使用可能なすべての引数を指定したこのコマンドの使用法について詳しくは、以下のコマンドを参照してください。

$ 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: [http://www.gnu.org/software/coreutils/]
Full documentation at: [http://www.gnu.org/software/coreutils/cat]
or available locally via: info '(coreutils) cat invocation'

重要で最も一般的に使用される cat コマンドの例がすべて含まれていることを願っています。基本的で最も広く使用されているコマンドの 1 つであるため、コマンドで使用できる高度な機能とオプションについて知っておくことが重要です。この投稿がお役に立てば幸いです。

cat、more、tail、head、wc コマンドを使用してファイルを表示


Linux
  1. Linuxでの12の便利なWgetコマンドの例

  2. Linuxでの重要なCatコマンドの例

  3. Linuxでのlsコマンド:17の便利な例の説明

  1. Linuxでのmvコマンド:7つの重要な例

  2. LinuxでのSplitコマンドの9つの便利な例

  3. Linuxテールコマンドの例

  1. Linuxでの8つのヘッドコマンドの例

  2. Linux での ln コマンドの例

  3. Linux での file コマンドの例