はじめに
私が経験した運用保守の現場ではよく使うファイル関連のコマンドをまとめました。
こういうコマンドがあることを参考にしていただけたら幸いです。
viエディタについて
開いているファイル名を表示
viで開いたときにctrl+gにて開いているファイル名を表示します。
※何のファイルを修正中か把握できます。
行番号を表示する場合
viで開いて行番号を表示する場合
: set nu
:%s/置換前の文字列/置換後の文字列/g
ファイルのコメントアウトと空白以外を表示する
現行と新規のサーバで設定ファイルの差分を取得することはよくあります。
その際にコメントアウトと空白行以外を表示して実際に設定してあるパラメータのみを表示するコマンドです。
cat ファイル名 | grep -v ^$ | grep -v ^#
例)
# cat incron.conf | grep -v ^$ | grep -v ^#
editor = vi
grep -rl
ファイルの「中身」を検索して、マッチしたファイルのファイル名だけを表示したい場合
grep -rl "検索したい文字列" 検索対象ファイル
例)
# grep -rl "AAA" /root/*.txt
/root/test.txt
/root/test1.txt
差分
私が経験した運用保守の現場では設定ファイルを変更前に必ず設定ファイルのバックアップを取得しています。
その後、変更前ファイルと変更後ファイルで差分を取得して画面のスクリーンショットを取得していました。
※補足 画面のスクショを取る理由はエビデンスを変更できないため。
sdiffコマンド
sdiffコマンドは変更前と変更後で比較したら見やすいです。
差異がある個所は|で表示されます。
※見やすいためエビデンスの取得にいよくsdiffコマンドは使用していました。
sdiff -s ファイル変更前 ファイル変更後
例
# sdiff -s /etc/php.ini /etc/php.ini.original
expose_php = Off | expose_php = On
post_max_size = 128M | post_max_size = 8M
upload_max_filesize = 128M | upload_max_filesize = 2M
date.timezone = date.timezone = Asia/Tokyo | ;date.timezone =
diff -uコマンド
よくnagiosの設定ファイルを変更前と変更後で設定ファイルの差分を取得しいた際にどこを変更したのか把握しやすいコマンドです。
# diff -u ファイル変更前 ファイル変更後
例
# diff -u /etc/php.ini /etc/php.ini.original
--- /etc/php.ini 2025-04-26 23:44:12.707732610 +0000
+++ /etc/php.ini.original 2025-04-09 05:43:25.000000000 +0000
@@ -407,7 +407,7 @@
; threat in any way, but it makes it possible to determine whether you use PHP
; on your server or not.
; https://php.net/expose-php
-expose_php = Off
+expose_php = On
;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
@@ -710,7 +710,7 @@
; Its value may be 0 to disable the limit. It is ignored if POST data reading
; is disabled through enable_post_data_reading.
; https://php.net/post-max-size
-post_max_size = 128M
+post_max_size = 8M
; Automatically add files before PHP document.
; https://php.net/auto-prepend-file
@@ -862,7 +862,7 @@
; Maximum allowed size for uploaded files.
; https://php.net/upload-max-filesize
-upload_max_filesize = 128M
+upload_max_filesize = 2M
; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20
@@ -939,7 +939,7 @@
[Date]
; Defines the default timezone used by the date functions
; https://php.net/date.timezone
-date.timezone = date.timezone = Asia/Tokyo
+;date.timezone =
; https://php.net/date.default-latitude
;date.default_latitude = 31.7667