Yuki's Tech Blog

仕事で得た知見や勉強した技術を書きます。

【もう絶対に忘れない Linux コマンド【Linux 100本ノック+名前の由来+丁寧な解説で、長期記憶に焼き付けろ!】セクション5: 調べる・探すコマンドで知らなかったことをざっくりまとめてみた

目次

helpオプション

helpオプションを使用することで、 そのコマンドの簡単な使い方やオプションを見ることができます。 コマンドの後ろにロングオプションで--helpを指定します。

[ec2-user@ip-10-0-10-10 repos]$ cat --help
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s), or standard input, to standard output.

  -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

With no FILE, or when FILE is -, read standard input.

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/>
For complete documentation, run: info coreutils 'cat invocation'
[ec2-user@ip-10-0-10-10 repos]$ 

manコマンド

manコマンドを使うことで、 helpオプションよりそのコマンドの詳細な情報を知ることができます。 manはマニュアルの略です。個人的にはmanコマンドの方が見やすいです。

man コマンド名

特定のファイルやディレクトリを探し出すfindコマンド

findコマンドを実行することで、特定のファイルやディレクトリを探し出すことができます。

以下ではカレントディレクトリ配下の.rbという拡張子のファイルを表示しています。 findコマンドは対象ディレクトリのサブディレクトリも検索対象に含んでいます。

[ec2-user@ip-10-0-10-10 repos]$ find ./ -name "*.rb" -print
./rails/rails-practice/ruby-practice/sample.rb
./react/sample.rb
./react-1/rails/rails-practice/sample.rb
./react-1/rails/rails/rails-practice/sample.rb
./sample.rb
./sample5.rb
[ec2-user@ip-10-0-10-10 repos]$ find ./ -name "*.rb" -print

カレントディレクトリ配下のみを検索対象としたい場合、maxdepthオプションを指定します。depthは深さという意味です。-printは省略可能です。

[ec2-user@ip-10-0-10-10 repos]$ find ./ -maxdepth 1 -name "*.rb" -print
./sample.rb
./sample5.rb

findコマンドは以下の書式で書きます。アクションが-printの場合、アクションを省略できます。

find 検索開始ディレクトリ 検索条件 アクション

(注)パス名展開とワイルドカードは違います。ワイルドカードを使うためには、ダブルクォーテーションで囲みます。記号の意味自体はパス名展開とワイルドカードで違いはありません。

ワイルドカード

記号 意味
* 任意の0文字以上の文字列
? 任意の1文字

ファイル種別で検索する

findコマンドにtypeオプションを指定することで、ファイル種別で検索をかけることもできます。

[ec2-user@ip-10-0-10-10 repos]$ find . -maxdepth 1 -type f
./sample.rb
./sample.py
./sample.ts
./sample5.rb

locateコマンド

findコマンドは検索に時間がかかるという難点があります。locateコマンドではそれを解決しています。findが使えればlocateを使うことはないかなと感じたので、一旦使う時が来たらまた学習しようと思います。

grepコマンド

grepコマンドを実行することで、 指定したファイルの中から特定の文字列を含む行だけを出力します。 grepはgrobal regular expression printの略です。ファイル全体のうち正規表現に一致する行だけを出力するという意味です。grepコマンドは以下のように使用します。

grep 検索対象文字列 ファイル名 or パス
[ec2-user@ip-10-0-10-10 repos]$ cat /etc/crontab -n
     1  SHELL=/bin/bash
     2  PATH=/sbin:/bin:/usr/sbin:/usr/bin
     3  MAILTO=root
     4
     5  # For details see man 4 crontabs
     6
     7  # Example of job definition:
     8  # .---------------- minute (0 - 59)
     9  # |  .------------- hour (0 - 23)
    10  # |  |  .---------- day of month (1 - 31)
    11  # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
    12  # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
    13  # |  |  |  |  |
    14  # *  *  *  *  * user-name  command to be executed
    15
[ec2-user@ip-10-0-10-10 repos]$ grep bin /etc/crontab -n
1:SHELL=/bin/bash
2:PATH=/sbin:/bin:/usr/sbin:/usr/bin

参考記事

もう絶対に忘れない Linux コマンド【Linux 100本ノック+名前の由来+丁寧な解説で、長期記憶に焼き付けろ!】 | Udemy

Linux【ワイルドカードと正規表現】の違いと変換,展開の動作 ~ ls, grep, findでの具体例の解説~ | SEの道標

find コマンドで対象ディレクトリ直下のファイルのみを絞り込みたい - Neo's World