0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

現役のインフラエンジニアが業務でよく使うコマンド

0
Last updated at Posted at 2025-08-08

初級

su

rootユーザーに切り替えるコマンド

sudo su -

コマンド入力後にパスワード聞かれる
rootユーザーだと簡単に環境破壊できるので注意が必要:warning:

ps

プロセスの生死を確認する

ps aux | grep プロセス

pwd

現在の作業ディレクトリのフルパスを表示するコマンド

pwd

cdしてるとどこいるかわからなくなるのでことあるごとにたたく
mvとかcpにする時に使うイメージ

ls

ディレクトリ情報を一覧で表示するコマンド

ls -ltr

一番使うコマンド
オプションの詳しい説明は割愛
日付順で並べ替えて詳細表示

chmod/chown

ファイル読み書き実行権限を変更する / ファイルの所有者を変更するコマンド

chmod 755 ファイル名
#(例) chmod 755  hoge.sh
chown root:root ファイル名
#(例) chown root:root hoge.sh

新しくファイルもってきたりすると権限なくてスクリプト実行できないとかよくある。
コピーしたいけど権限なくてできないとかの場合に使用する。

df

ディスクの空き容量を表示するためのコマンド

df -h ディレクトリ
#(例) df -h /var/log/messages

ログたまりすぎて圧迫してないかたまに確認する。

wc

ファイル内の行数を表示するコマンド

コマンド | wc -l
#(例)ls | wc -l

ディレクトリ内のファイル数を確認するときによく使う。

tar

ファイルやディレクトリを一つにまとめるコマンド

# 圧縮
tar zcvf hoge.tar.gz 圧縮対象のディレクトリ
#(例) tar zcvf hoge.tar.gz /tmp/test_250101
# 解凍
tar zxvf hoge.tar.gz

zip的なもの
zipもlinuxで意外と使う

中級

grep

特定の文字列を検索するコマンド

コマンド | grep 検索文字列
#(例) ifconfig | grep inet

grepコマンドは基本的にパイプに続けて使用することが多い。
ログから指定した文字を探す場合は以下を使用する。

grep 検索文字 検索対象ログ
#(例) grep "test msg" TEST_*.log

複数ログから指定した文字を含む箇所を表示したい場合の例

awk

コマンド | awk -F: '{print $2}'
コマンド | awk '$3 == "root" {print $0}'

よく使う。
特定の区画だけを抜き出したい時などに使用。
シェルスクリプト内で使用することも多い。
grepと組み合わせて使うことも多い。

root@hoge$ ls -l
lrwxrwxrwx.   1 root root       7  3月 25  2022 bin -> usr/bin
dr-xr-xr-x.   5 root root    4096  7月  5  2024 boot
drwxr-xr-x   21 root root    3360  8月 19 08:40 dev
drwxr-xr-x.  87 root root    8192  7月 18 17:16 etc
drwxr-xr-x.  22 root root    4096  7月  2 09:13 home
lrwxrwxrwx.   1 root root       7  3月 25  2022 lib -> usr/lib
lrwxrwxrwx.   1 root root       9  3月 25  2022 lib64 -> usr/lib64
...
root@hoge$ ls -l / | awk '{print $6}'
3月
7月
8月
7月
7月
3月
3月
...
root@hoge$ ls -l / | awk '$6 == "8月" {print $0}'
#以下でもOK
#root@hoge$ ls -l / | awk '{ if ($6 == "8月") {print $0}}'
drwxr-xr-x   21 root root    3360  8月 19 08:40 dev
dr-xr-xr-x  206 root root       0  8月 19  2025 proc
dr-xr-x---.   7 root root    4096  8月 19 10:34 root
drwxr-xr-x   29 root root     900  8月 19 08:40 run
dr-xr-xr-x   13 root root       0  8月 19  2025 sys
drwxrwxrwt.  11 root root    4096  8月 19 10:45 tmp
root@hoge$

おまけ

for f in TESTLOG_*
do
    diff ${f} TESTLOG_01.txt
done | less

出力したエビデンスたち(TESTLOG_01.txt ~ TESTLOG_10.txt)の中身のdiffをとる場合
TESTLOG_01.txtとそれ以外のファイルで比較する。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?