1. インフラ系
①スケジュール的に起動(crontab)
⇓ crontabファイルを編集
vi /etc/crontab
⇓ 下記のレコードを追加(今回は毎日3時に/home/autostart.shを実行)
0 3 * * * root /home/autostart.sh
⇓ 実行対象スクリプトの実行権限を付与
chmod 777 /home/autostart.sh
⇓ crontabファイルに定義ルールの記載はあります。
2. ネットワーク系
①ネットワーク接続情報を表示(netstat -tunlp)
netstat -tunlp
netstat -tunlp | grep 3306
//指定のポートが使用中かを確認
以下は、各オプションの意味です:
- “-t” : TCP接続を表示します。
- “-u” : UDP接続を表示します。
- “-n” : ポート番号とIPアドレスを数値で表示します。
- “-l” : ローカル接続のみを表示します。
- “-p” : ポート番号とプロセス名を表示します。
[root@localhost demo]# netstat -tunlp | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 16884/docker-proxy
tcp6 0 0 :::3306 :::* LISTEN 16890/docker-proxy
[root@localhost demo]#
[root@localhost demo]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19f3dfc905fe mysql:5.7 "docker-entrypoint.s…" 44 hours ago Up 44 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
② プロセスを強制終了(kill -9 PID)
kill -9 PID
例:kill -9 16884 16890
//ポート3306を使うプロセスを強制終了。-9
は強制終了のこと
③firewalldサービス
systemctl status firewalld
systemctl start firewalld
systemctl stop firewalld
3. 検索系(find,grep)
①カレントディレクトリに存在する特定のファイルの全てについて、最終行を出力
find *demo*20230313* -exec tail -1 {} \;
find *demo*20230313* -exec tail -1 {} \; | wc -l
具体的には、以下のように解釈できます。
find
: ファイルを検索するコマンド*demo*20230313*
: ファイル名に “demo” および “20230313” の文字列が含まれるものを検索する-exec
: 検索された各ファイルに対して、指定されたコマンドを実行するオプションtail -1 {} ./
: 検索されたファイルに対して、tailコマンドを実行して最後の行を表示する。 {} はファイル名(findの結果で置換され)を表す\;
はfindコマンドに対する-execオプションの引数として、tailコマンドを使用する場合、\;
は-exec
オプションの終了を示す。wc -l
: 出力された行数を数えるコマンド。
\;
がない場合
[root@localhost demo]# find *demo*20230313* -exec tail -1 {}
find: `-exec' に引数がありません
②全サーバで指定ファイルを検索
find / -name ファイル名
⇒ この際は*
などのワイルドカードは使用不可
[root@localhost demo]# find / -name demo01-20230313.log
/home/demo/demo01-20230313.log
[root@localhost demo]#
③指定ファイルの指定キーワードを検索
find *demo*20230313* | xargs grep 6
grep 6 *demo*20230313*
具体的には、"find"コマンドはファイルを検索し、"xargs"コマンドは検索されたファイルに対して、指定されたコマンド(ここでは “grep”)を実行するために使用されます。
[root@localhost demo]# ll
合計 16
-rw-r--r--. 1 root root 34 3月 13 19:43 demo01-20230313.log
-rw-r--r--. 1 root root 34 3月 13 19:44 demo02-20230313.log
-rw-r--r--. 1 root root 34 3月 13 19:45 demo03-20230313.log
-rw-r--r--. 1 root root 34 3月 13 19:45 demo04-20230312.log
[root@localhost demo]# find *demo*20230313* | xargs grep 6
demo01-20230313.log:601
demo02-20230313.log:602
demo03-20230313.log:603
[root@localhost demo]#
[root@localhost demo]# grep 6 *demo*20230313*
demo01-20230313.log:601
demo02-20230313.log:602
demo03-20230313.log:603
[root@localhost demo]#
④空白ファイル一括削除
find . -size 0 | xargs rm
find ./ -size 0 -exec rm {} \;
⑤grepコマンド
詳細について、👇下記ページをご参照ください
https://www.mina-isho.com/archives/2303151800
4. ディレクトリ・ファイル総合系
①圧縮(tar -cvf,compress)と解凍(tar -xvf,uncompress)
tar [-cxtzjvfpPN] ファイルまたはディレクトリ
- -c : 新しいアーカイブを作成します。
- -x : アーカイブからファイルを抽出します。
- -t : アーカイブ内のファイルをリスト表示します。
- -z : 圧縮をgzipで実行します。
- -j : 圧縮をbzip2で実行します。
- -v : 処理の詳細を表示します。
- -f : アーカイブのファイル名を指定します。
- -p : 抽出時に、ファイルのパーミッションやオーナーシップを保持します。
- -N : 指定した日付以降に変更されたファイルのみをアーカイブします。
- -P : シンボリックリンクを解決して、そのリンク先のファイルをアーカイブします。
例:
tar -cvf test.tar test.txt
// 「test.txt」を新しいアーカイブに作成
tar -zcvf test.tar.gz test.txt
// tar.gzに圧縮。※解凍する際も-zの指定が必要
tar -tf test.tar
// test.tar内のファイルをリスト表示
compress test.tar
// test.tarを更にtest.tar.Zに圧縮
tar xvf test.tar
// 解凍
②ディレクトリ新規(mkdir)
mkdir test
//testという名前の新規ディレクトリーを作成
mkdir -m 777 test
//testという名前の新規ディレクトリーを作成する際、全ての許可rwxrwxrwxを与える
mkdir -p /home/test
//testという名前の新規ディレクトリーを作成する際、homeというディレクトリーがない場合、併せて作成
5. ファイル加工系
①文字列切り取り(cut)
⇒ テキストファイルや標準入力から指定されたフィールドを切り取り、出力するためのLinuxコマンドです。
参照先:cutコマンド
②ソート(sort)
⇒テキストファイルや標準入力の行をソート(並べ替え)するために使用されます。
参照先:sortコマンド
③置換、削除(sed)
置換(sed ‘s/v1/v2/’)
-
s
: substitute、代替という意味の英語の頭文字 -
/g
:ない場合、最初のみ -
/g
:ある場合、全体置換 -
\
:特殊文字の前に付く -
デモ
[root@localhost demo]# echo fff,ffff,fffff > test.txt
[root@localhost demo]# sed "s/,/ \& /" test.txt
fff & ffff,fffff
[root@localhost demo]# sed "s/,/ \& /g" test.txt
fff & ffff & fffff
[root@localhost demo]# sed "s/fff/ttt/g" test.txt
ttt,tttf,tttff
削除(sed ‘/k/d’)
sed "/キーワード/d"
またはsed '/キーワード/d' test.txt
^キーワード
:先頭部が合致することキーワード$
:末尾部が合致する場合/d
:行を削除/x
:行の内容をクリア、行自体が残る- デモ
[root@localhost demo]# cat test.txt
fff,ffff,fffff
#7654321#
87#65
9876#
[root@localhost demo]# sed "/#/d" test.txt
fff,ffff,fffff
[root@localhost demo]# sed "/^#/d" test.txt
fff,ffff,fffff
87#65
9876#
[root@localhost demo]# sed "/#$/d" test.txt
fff,ffff,fffff
87#65
[root@localhost demo]# sed "/^#/x" test.txt
fff,ffff,fffff
87#65
9876#
[root@localhost demo]#
改行を置換
- CRLF(/r/n) -> LF(/n)
sed -i ‘s/\r//g’ input.txt
sed -i -z ‘s/\r\n/\n/g’ input.txt - LF(/n) -> CRLF(/r/n)
sed -i ‘s/$/\r/g’ input.txt
sed -i -z ‘s/\n/\r\n/g’ input.txt - CRLF(/r/n), LF(/n) を削除
sed -i -z ‘s/\r\n//g’ input.txt
sed -i -z ‘s/\n//g’ input.txt
基本オプション
-i: ファイルを直接操作する
-z: 改行コード(\n) を操作できるようにする
-e:オプションを使うと複数条件指定できます
-n:指定した行を表示できます(例:sed -n 2,5p input.txt)
コメント欄