▼すべてを開く。
▲すべてを閉じる。
$ grep -rLi 'kernel' * cron cups/error_log cups/access_log cups/page_log httpd/error_log httpd/ssl_error_log httpd/access_log httpd/ssl_access_log httpd/ssl_request_log lastlog mail/statistics maillog redhat-config-network samba/smbd.log samba/nmbd.log scrollkeeper.log secure spooler wtmp
grepコマンドで、-rオプションにLiオプションを付け、検索する文字列「'kernel'」、検索対象のファイル「*」(すべてのファイル)を指定すると、文字列「kernel」を含まないファイルをカレントディレクトリにあるすべてのディレクトリの中から検索して表示する。検索する文字列は、引用符「'」で囲むこと。
grepコマンドのオプション | |
-r | ディレクトリを再帰的に検索する |
-L | 指定文字列を含まないファイル |
-i | 大文字小文字の区別をしない |
$ grep -F '^I' boot.log Apr 6 13:44:59 localhost firstboot: ^IBefore reporting any problems, please make sure you are using the most Apr 6 13:44:59 localhost firstboot: ^Irecent XFree86 packages available from Red Hat by checking for updates Apr 6 13:44:59 localhost firstboot: ^Iat http://rhn.redhat.com/errata or by using the Red Hat Network up2date Apr 6 13:44:59 localhost firstboot: ^Itool. If you still encounter problems, please file bug reports in the Apr 6 13:44:59 localhost firstboot: ^IXFree86.org bugzilla at http://bugs.xfree86.org and/or Red Hat Apr 6 13:44:59 localhost firstboot: ^Ibugzilla at http://bugzilla.redhat.com
grepコマンドで、-Fオプションに検索する文字列「'^I'」、検索対象のファイル「boot.log」を指定すると、文字列「^I」を検索して表示する。検索する文字列は、引用符「'」で囲むこと。
grepコマンドのオプション | |
-F | 固定文字列として検索する |
grepコマンドは、デフォルトで検索パターンを正規表現として解釈する。正規表現ではキャレット「^」は行頭を表すため、'^I'は行頭がIで始まる文字列を検索してしまう。これらの正規表現で特別な意味を持つ「^$?*.[]」などのメタキャラクタを単なる文字として検索するには-Fオプションを指定する。以下のコマンドでは目的の文字列は検索できない。
$ grep '^I' boot.log
$ grep '^I' dmesg Initializing CPU#0 Inode cache hash table entries: 8192 (order: 4, 64 KB) Intel machine check architecture supported. Intel machine check reporting enabled on CPU#0. Initializing RT netlink socket Initializing Cryptographic API IP: routing cache hash table of 512 buckets, 4Kbytes Initializing IPsec netlink socket
grepコマンドで、検索する文字列「'^I'」(「^」は正規表現で行頭を意味する)、検索対象のファイル「dmesg」を指定すると、行頭が「I」で始まる行を検索して表示する。検索する文字列は、引用符「'」で囲むこと。
$ grep -i 'kernel' dmesg Kernel command line: ro root=LABEL=/ Memory: 115904k/122816k available (1543k kernel code, 5568k reserved, 1071k data, 164k init, 0k highmem) Freeing unused kernel memory: 164k freed
grepコマンドで、-iオプション、検索する文字列「'kernel'」、検索対象のファイル「dmesg」を指定すると、文字列「kernel」を含む行を大文字小文字の区別なく検索して表示する。検索する文字列は、引用符「'」で囲むこと。
grepコマンドのオプション | |
-i | 大文字小文字の区別をしない |
$ grep -Li 'kernel' * canna cron cups gdm httpd lastlog mail maillog redhat-config-network samba scrollkeeper.log secure spooler squid vbox wtmp
grepコマンドで、-Lオプションにiオプションを付け、検索する文字列「'kernel'」、検索対象のファイル「*」(すべてのファイル)を指定すると、文字列「kernel」を含まないファイルを大文字小文字の区別なく検索して表示する。検索する文字列は、引用符「'」で囲むこと。
grepコマンドのオプション | |
-L | 指定文字列を含まないファイル |
-i | 大文字小文字の区別をしない |
$ grep -li 'kernel' * XFree86.0.log XFree86.0.log.old XFree86.1.log boot.log dmesg ksyms.0 ksyms.1 ksyms.2 ksyms.3 ksyms.4 ksyms.5 ksyms.6 messages up2date
grepコマンドで、-lオプションにiオプションを付け、検索する文字列「'kernel'」、検索対象のファイル「*」(すべてのファイル)を指定すると、文字列「kernel」を大文字小文字の区別なく検索してファイルを表示する。検索する文字列は、引用符「'」で囲むこと。
grepコマンドのオプション | |
-l | 指定文字列を含むファイル |
-i | 大文字小文字の区別をしない |
$ grep 'failed$' boot.log May 9 18:19:36 localhost canna: Stopping Canna server: failed May 9 22:14:32 localhost canna: Stopping Canna server: failed May 10 15:27:13 localhost canna: Stopping Canna server: failed May 10 20:56:54 polotxp ntpd: failed May 10 20:56:54 polotxp ntpd: failed
grepコマンドで、検索する文字列「'failed$'」、検索対象のファイル「boot.log」を指定すると、文字列「failed」が行末にある行を検索して表示する。検索する文字列は、引用符「'」で囲むこと。
正規表現で、「$」は行末を表す。
$ grep -n 'Initializing' dmesg 18:Initializing CPU#0 52:Initializing RT netlink socket 91:Initializing Cryptographic API 96:Initializing IPsec netlink socket
grepコマンドで、-nオプション、検索する文字列「'Initializing'」、検索対象のファイル「dmesg」を指定すると、文字列「Initializing」を含む行を行の番号を付けて表示する。検索する文字列は、引用符「'」で囲むこと。
grepコマンドのオプション | |
-n | 該当行番号を表示する |
$ grep 'kernel' dmesg Memory: 115904k/122816k available (1543k kernel code, 5568k reserved, 1071k data, 164k init, 0k highmem) Freeing unused kernel memory: 164k freed
grepコマンドで、検索する文字列「'kernel'」、検索対象のファイル「dmesg」を指定すると、文字列「kernel」を含む行を表示する。検索する文字列は、引用符「'」で囲むこと。
$ grep -L 'kernel' * XFree86.0.log XFree86.0.log.old XFree86.1.log canna cron cups gdm httpd lastlog mail maillog redhat-config-network samba scrollkeeper.log secure spooler squid vbox wtmp
grepコマンドで、-Lオプションに検索する文字列「'kernel'」、検索対象のファイル「*」(すべてのファイル)を指定すると、文字列「kernel」を含まないファイルを表示する。検索する文字列は、引用符「'」で囲むこと。
grepコマンドのオプション | |
-L | 指定文字列を含まないファイル |
$ less up2date [Tue May 10 16:00:01 2005] up2date updating login info [Tue May 10 16:00:02 2005] up2date logging into up2date server [Tue May 10 16:00:03 2005] up2date successfully retrieved authentication token f rom up2date server [Tue May 10 16:07:29 2005] up2date updating login info [Tue May 10 16:07:29 2005] up2date logging into up2date server [Tue May 10 16:07:31 2005] up2date successfully retrieved authentication token f rom up2date server [Tue May 10 16:07:45 2005] up2date updating login info [Tue May 10 16:07:45 2005] up2date logging into up2date server [Tue May 10 16:07:46 2005] up2date successfully retrieved authentication token f rom up2date server
$ grep -v 'up2date' up2date sys.exit(main() or 0) gui.main() File "gui.py", line 2192, in main gtk.mainloop() File "gui.py", line 1378, in onChannelsPageNext channel = rhnChannel.selected_channels.getByName(channelName) File "rhnChannel.py", line 82, in getByName log.trace_me() x = traceback.extract_stack()
grepコマンドで、-vオプション、検索する文字列「'up2date'」、検索対象のファイル「up2date」を指定すると、文字列「up2date」がない行を検索して表示する。検索する文字列は、引用符「'」で囲むこと。
grepコマンドのオプション | |
-v | 検索文字列を含まない行 |
$ grep -l 'kernel' * boot.log dmesg ksyms.0 ksyms.1 ksyms.2 ksyms.3 ksyms.4 ksyms.5 ksyms.6 messages up2date
grepコマンドで、-lオプション、検索する文字列「'kernel'」、検索対象のファイル「*」(すべてのファイル)を指定すると、カレントディレクトリにある文字列「kernel」を含むファイルを表示する。検索する文字列は、引用符「'」で囲むこと。
grepコマンドのオプション | |
-l | 指定文字列を含むファイル |
grepコマンドは、大文字と小文字を区別するので、次のコマンドの結果は異なるものとなる。
$ grep -l 'Kernel' * XFree86.0.log XFree86.0.log.old XFree86.1.log boot.log dmesg messages
$ locate ping | grep -w 'ping' /usr/share/man/man8/ping.8.gz /usr/share/man/ja/man8/ping.8.gz /usr/share/xemacs/xemacs-packages/etc/sounds/the-ping.au /usr/share/xemacs/xemacs-packages/etc/sounds/the-ping.wav /bin/ping
locateコマンドで「ping」を検索し、その結果をパイプ「|」でgrepコマンドに送り、-wオプションと検索文字列「'ping'」を指定する。
grepコマンドのオプション | |
-w | 独立した単語として検索する |
pingコマンドの所在をlocateコマンドで検索すると、「xxxping」や「xxpingxx」など意図しない検索結果が大量に出る。これらの結果の中から単語としての「ping」を検索する時に-wオプションを利用する。
locateコマンドによるpingの検索結果$ locate ping /etc/gnome-vfs-2.0/modules/mapping-modules.conf /usr/share/doc/pspell-0.12.2/man-html/3_Keeping.html /usr/share/doc/pspell-0.12.2/man-text/3_Keeping.txt /usr/share/doc/freetype-2.1.4/docs/glyphs/clipping.png /usr/share/doc/irda-utils-0.9.15/README.irdaping /usr/share/doc/samba-3.0.9/docs/htmldocs/Samba-HOWTO-Collection/groupmapping.html ・・・・・・・・・・・・・・・・・・・・・・・・・・・・中略・・・・・・・・・・・・・・・・・・・・・・・・・・・・ /usr/sbin/arping /usr/sbin/ping6 ・・・・・・・・・・・・・・・・・・・・・・・・・・・・中略・・・・・・・・・・・・・・・・・・・・・・・・・・・・ /bin/ping /bin/ping6 /sbin/arping