定時帰る術-Linuxファイルさがす（find、grepコマンド）

find、grepの使い方やオプションなど結構多いと思いますが、仕事でもよく使われていて、試してみなら、覚えていった方がいいかもしれません。


# find



###「/」配下のconfファイル数を表示させる
```
find / -name "*.conf" | wc -l
370
```

### 「/」配下のconfファイルを探し、ファイルの詳細を表示させる
```
find / -name "*.conf" -exec ls -l {} \;
-rw-r--r--. 1 root root 336  9月 26 16:49 /boot/loader/entries/fd6ae0502aa249a3a2bf951e394c361a-4.18.0-305.el8.x86_64.conf
-rw-r--r--. 1 root root 408  9月 26 16:49 /boot/loader/entries/fd6ae0502aa249a3a2bf951e394c361a-0-rescue.conf
-rw-r--r-- 1 root root 78 10月 16 09:00 /run/NetworkManager/resolv.conf
-rw-r--r-- 1 root root 78 10月 16 09:00 /run/NetworkManager/no-stub-resolv.conf
-rw-r--r-- 1 root root 562 10月 16 09:00 /run/tmpfiles.d/kmod.conf
-rw-r--r-- 1 root root 78 10月 16 09:00 /etc/resolv.conf
-rw-r--r--. 1 root root 108 11月  9  2020 /etc/dnf/dnf.conf
-rw-r--r--. 1 root root 351 10月  7  2020 /etc/dnf/plugins/copr.conf
-rw-r--r--. 1 root root 30 10月  7  2020 /etc/dnf/plugins/debuginfo-install.conf
-rw-r--r--. 1 root root 24  1月 12  2021 /etc/dnf/plugins/kpatch.conf
-rw-r--r--. 1 root root 17  3月 16  2021 /etc/dnf/plugins/product-id.conf
-rw-r--r--. 1 root root 213  3月 16  2021 /etc/dnf/plugins/subscription-manager.conf
-rw-r--r--. 1 root root 4 11月  9  2020 /etc/dnf/protected.d/dnf.conf
-rw-r--r--. 1 root root 15  3月 31  2021 /etc/dnf/protected.d/redhat-release.conf
-rw-r--r--. 1 root root 6  4月 15  2020 /etc/dnf/protected.d/setup.conf
-rw-r--r--. 1 root root 21  3月 11  2021 /etc/dnf/protected.d/systemd.conf
-rw-r--r--. 1 root root 5  2月  4  2021 /etc/dnf/protected.d/sudo.conf
-rw-r--r--. 1 root root 4 11月  9  2020 /etc/dnf/protected.d/yum.conf
lrwxrwxrwx. 1 root root 70 12月 10  2020 /etc/fonts/conf.d/20-unhint-small-dejavu-sans-mono.conf -> /usr/share/fontconfig/conf.avail/20-unhint-small-dejavu-sans-mono.conf
lrwxrwxrwx. 1 root root 57 12月 10  2020 /etc/fonts/conf.d/57-dejavu-sans-mono.conf -> /usr/share/fontconfig/conf.avail/57-dejavu-sans-mono.conf
lrwxrwxrwx. 1 root root 50 12月 23  2020 /etc/fonts/conf.d/31-cantarell.conf -> /usr/share/fontconfig/conf.avail/31-cantarell.conf
```




### 「/」配下のconfファイルを探し、/var/tmp/conf.zipに圧縮する
```
 find / -name "*.conf" -exec zip /var/tmp/conf.zip {} \;
(rm,chmodもよく使う)
[root@test ~]#  find / -name "*.conf" -exec zip /var/tmp/conf.zip {} \;
updating: boot/loader/entries/fd6ae0502aa249a3a2bf951e394c361a-4.18.0-305.el8.x86_64.conf (deflated 41%)
updating: boot/loader/entries/fd6ae0502aa249a3a2bf951e394c361a-0-rescue.conf (deflated 51%)
updating: run/NetworkManager/resolv.conf (deflated 14%)
updating: run/NetworkManager/no-stub-resolv.conf (deflated 14%)
updating: run/tmpfiles.d/kmod.conf (deflated 67%)
updating: etc/resolv.conf (deflated 14%)
updating: etc/dnf/dnf.conf (deflated 14%)
updating: etc/dnf/plugins/copr.conf (deflated 42%)
updating: etc/dnf/plugins/debuginfo-install.conf (stored 0%)
updating: etc/dnf/plugins/kpatch.conf (stored 0%)
updating: etc/dnf/plugins/product-id.conf (stored 0%)
updating: etc/dnf/plugins/subscription-manager.conf (deflated 28%)
updating: etc/dnf/protected.d/dnf.conf (stored 0%)
updating: etc/dnf/protected.d/redhat-release.conf (stored 0%)
updating: etc/dnf/protected.d/setup.conf (stored 0%)
updating: etc/dnf/protected.d/systemd.conf (deflated 14%)
updating: etc/dnf/protected.d/sudo.conf (stored 0%)
updating: etc/dnf/protected.d/yum.conf (stored 0%)
 ```
### 「/」配下のconfファイルを探し,ipをgrepし、対象ファイル名と行数も表示させる
```
find / -name "*.conf" -exec grep ip {} /dev/null \;
（execでgrep使う場合、andでのgrepができなさそう)
[root@test ~]# find / -name "*.conf" -exec grep ip {} /dev/null \; | more
/etc/dnf/dnf.conf:skip_if_unavailable=False
/etc/dnf/plugins/subscription-manager.conf:# every time subscription-manager plugin is triggered by dnf or yum
/etc/fonts/conf.d/10-hinting-slight.conf:    <its:translateRule translate="no" selector="/fontconfig/*[not(self::description)]"/>
/etc/fonts/conf.d/10-hinting-slight.conf:  <description>Set hintslight to hintstyle</description>
/etc/fonts/conf.d/10-scale-bitmap-fonts.conf:    <its:translateRule translate="no" selector="/fontconfig/*[not(self::description)]"/>
/etc/fonts/conf.d/10-scale-bitmap-fonts.conf:  <description>Bitmap scaling</description>
/etc/fonts/conf.d/10-scale-bitmap-fonts.conf:  For non-scalable bitmap fonts (ie. non-color), skip
/etc/fonts/conf.d/20-unhint-small-vera.conf:    <its:translateRule translate="no" selector="/fontconfig/*[not(self::description)]"/>
/etc/fonts/conf.d/20-unhint-small-vera.conf:  <description>Disable hinting for Bitstream Vera fonts when the size is less than 8ppem</description>
/etc/fonts/conf.d/25-unhint-nonlatin.conf:    <its:translateRule translate="no" selector="/fontconfig/*[not(self::description)]"/>
/etc/fonts/conf.d/25-unhint-nonlatin.conf:  <description>Disable hinting for CJK fonts</description>
/etc/fonts/conf.d/30-metric-aliases.conf:    <its:translateRule translate="no" selector="/fontconfig/*[not(self::description)]"/>
/etc/fonts/conf.d/30-metric-aliases.conf:  <description>Set substitutions for similar/metric-compatible families</description>
/etc/fonts/conf.d/30-metric-aliases.conf:PostScript fonts:       URW fonts:           GUST fonts:        Windows fonts:
/etc/fonts/conf.d/30-metric-aliases.conf:       <!-- PostScript -->
/etc/fonts/conf.d/30-metric-aliases.conf:       <!-- PostScript -->
/etc/fonts/conf.d/30-metric-aliases.conf:       <!-- PostScript -->
/etc/fonts/conf.d/40-nonlatin.conf:    <its:translateRule translate="no" selector="/fontconfig/*[not(self::description)]"/>
/etc/fonts/conf.d/40-nonlatin.conf:  <description>Set substitutions for non-Latin fonts</description>
/etc/fonts/conf.d/45-generic.conf:    <its:translateRule translate="no" selector="/fontconfig/*[not(self::description)]"/>
/etc/fonts/conf.d/45-generic.conf:  <description>Set substitutions for emoji/math fonts</description>

「/」配下に500MB以上のファイルを探して、ファイル詳細を表示させる。
[root@test ~]# find / -size +500M -exec ls -hl {} \; 2>/dev/null
-r-------- 1 root root 128T 10月 16 09:00 /proc/kcore
-r--r--r-- 1 root root 619M  7月  1  2022 /media/images/install.img
```

# grep
```
or：egrep "条件A|条件B|条件C|" 対象ファイル
    grep -e 条件A -e 条件B -e 条件C 対象ファイル

and：grep 条件A 対象ファイル |grep 条件B| grep 条件C
```


### 空白行とコメントアウトされている行を表示させない
```
egrep -v "^?#|^$" /etc/httpd/conf/httpd.conf
(川口流)
```

### /etc/httpd/配下に、logでgrepする。
```
[root@test ~]# grep -r log /etc/httpd/
/etc/httpd/conf.modules.d/00-base.conf:LoadModule log_config_module modules/mod_log_config.so
/etc/httpd/conf.modules.d/00-base.conf:LoadModule logio_module modules/mod_logio.so
/etc/httpd/conf.modules.d/00-optional.conf:#LoadModule log_debug_module modules/mod_log_debug.so
/etc/httpd/conf.modules.d/00-optional.conf:#LoadModule log_forensic_module modules/mod_log_forensic.so
/etc/httpd/conf/httpd.conf:# Configuration and logfile names: If the filenames you specify for many
/etc/httpd/conf/httpd.conf:# with "/", the value of ServerRoot is prepended -- so 'log/access_log'
/etc/httpd/conf/httpd.conf:# server as '/www/log/access_log', where as '/log/access_log' will be
/etc/httpd/conf/httpd.conf:# interpreted as '/log/access_log'.
/etc/httpd/conf/httpd.conf:# configuration, error, and log files are kept.
/etc/httpd/conf/httpd.conf:# ErrorLog: The location of the error log file.
/etc/httpd/conf/httpd.conf:# logged here.  If you *do* define an error logfile for a <VirtualHost>
/etc/httpd/conf/httpd.conf:# container, that host's errors will be logged there and not here.
/etc/httpd/conf/httpd.conf:ErrorLog "logs/error_log"
/etc/httpd/conf/httpd.conf:# LogLevel: Control the number of messages logged to the error_log.
/etc/httpd/conf/httpd.conf:<IfModule log_config_module>
/etc/httpd/conf/httpd.conf:    <IfModule logio_module>
/etc/httpd/conf/httpd.conf:      # You need to enable mod_logio.c to use %I and %O
```