A Linux Command - user000422/0 GitHub Wiki

どんなにベテランになってもコマンド1つ1つ見直して実行する癖は忘れないこと

WinSCPなどのGUIツールで処理を行わないこと(作成や削除)

キー 意味
vi コマンド履歴確認
esc ディレクトリ名候補表示(入力途中でも可能)
pwd 今自分がいる階層を表示
ディレクトリ 説明
/home/sample_user ユーザ用ディレクトリ 作業用などに使用
/usr/local システム管理者用ライブラリ

■システム

# ssh接続
ssh [email protected] # rootユーザで接続

# ユーザ切り替え
su - oracle # Oracleユーザに切り替え
sudo su # rootユーザに切り替え(パスワードは現在ユーザのパスワードでよい)
sudo vi sample.txt # rootユーザでコマンド実行

# 現在のユーザ名
whoami

# 自身以外のユーザでコマンド実行
sudo -u sample_user cd /tmp

# OSバージョン確認(Redhat)
cat /etc/redhat-release

# OSビット確認
getconf LONG_BIT

# コマンド履歴
history
history > /tmp/sample.txt # 結果をファイルに出力

# ログイン履歴
last # RHEL

# 環境変数設定ファイル確認
cat ~/.bash_profile

# OS bit確認
getconf LONG_BIT

# ユーザ一覧確認 ※スペルは「passwd」で合っている
cat /etc/passwd

# ユーザ追加
useradd -u 200 sampleuser              # -u UIDを指定
useradd -g samplegroup sampleuser      # -g グループを指定
useradd -d /home/sampleuser sampleuser # -u UIDを指定

# UIDを変更
usermod -u 999 sampleuser

# shell
sh sample.sh # シェル実行
sh sample.sh 2021 # 引数を指定し、シェル実行

# メモリ キャッシュクリア
sync; echo 1 > /proc/sys/vm/drop_caches # ページキャッシュのみ
sync; echo 3 > /proc/sys/vm/drop_caches # ページキャッシュとdentry、inodes

# 再起動(RHEL)
reboot

■ファイル ディレクトリ操作

# ls(list segments)ファイル、ディレクトリの情報表示
ls # 現在いるディレクトリのファイル、フォルダを表示
ls /home # 指定ディレクトリのファイル、フォルダを表示

ls *sample.txt # ファイル名条件指定
ls -l # 詳細表示
ls -t # 更新時間順に並べる
ls -tl # 更新時間順に詳細表示し並べる
ls -I "*sample.txt" # 特定のファイルを除外

ll # 「ls -l」の省略記述

# cd(change directory)ディレクトリ移動
cd /work/2021/ # ディレクトリ移動
cd / # ルートディレクトリへ移動
cd .. # 1つ上のディレクトリへ移動

# cat(concatenate) ファイル閲覧
cat sample.txt # ファイル内容を出力(閲覧)
cat sample.txt | grep 'word' # 応用 grepと組み合わせ

# touch ファイル作成
touch sample.txt
touch -d "2023-01-01 23:59" sample.txt # -d タイムスタンプ変更

# cp コピー
# rとRは同じ
cp sample.txt newdir
cp sample/sample.txt . # 現在位置にコピー
cp -f sample.txt newdir # -f 上書きを常に許可 ※-fが機能しないシステムも存在する
cp -r sampleDir newDir # -r ディレクトリごとコピー
cp -p sample/sample.txt newdir # -p ディレクトリ構造ごとコピー(パーミッション属性保持)
cp -rp sample/sample.txt newdir # rとp

# mv(move)ファイルやディレクトリ移動
mv fromDir/sample.txt toDir/sample.txt

# rm ファイル削除
# 「r」オプションが本当に必要か考えること ディレクトリ対象のオプションですよ!
rm -i sample.txt # 確認して削除
rm -f sample.txt # 強制削除
rm -r /sample    # ディレクトリごと削除

# mkdir ディレクトリ作成
mkdir /tmp/sample
mkdir -p /tmp/sample # -p 必要に応じて親ディレクトリも作成
mkdir -m 777 /tmp/sample # パーミッション指定(基本型)
mkdir -m 777 `date '+%Y%m%d'`sample # dateコマンドで日付利用したディレクトリを作成

# find ファイル検索
find /home/abc.dat # ディレクトリを指定しファイルを検索
find -name "*abc.dat" # ワイルドカードを使用し、ファイル名で検索
find -name "*.dat" -o -name "*.sh" # or検索
find /home -type l # type l ディレクトリ配下のシンボリックリンクを検索
# 実践(なんか↑の全部変じゃない?)
find /home -name 'sample.php' # /home配下でファイルを検索

# grep 文字列検索
grep -r sampleword sampleDir # rオプション ディレクトリ内のファイルも検索(なければディレクトリ内を検索できない)
grep -r --include='*.php' sampleword sampleDir # include 検索対象の拡張子を指定
grep -r --exclude='*.html' sampleword sampleDir # exclude 指定拡張子のファイルを検索対象から除外
grep -r --exclude='*.html' --exclude='*.css' sampleword sampleDir # exclude 指定拡張子のファイルを検索対象から除外(複数)
grep -n sampleword sampleDir # nオプション 行番号を表示

# tail ファイル内末尾閲覧
tail sample.txt
tail -n 10 sample.txt # -n 指定行数末尾から閲覧
tail -f sample.txt # -f ファイル終端監視(ctrl + nで抜けるまで監視)

# zip
# 出力先はカレントディレクトリ(第1引数にパスを指定することで出力先を変更)
zip -r sample.zip sampleDir # r 再帰(ディレクトリの中のディレクトリやファイルも対象にする)

# unzip zipファイルを展開
unzip sample.zip # カレントディレクトリに展開
unzip sample.zip -d targetDir # -d 指定ディレクトリに展開

# du ディスク使用率(ファイルサイズ)
du -sh sampleDir # ディレクトリのサイズ確認
du -sh sampleDir/* # ディレクトリ配下のサイズ確認
du -shm sampleDir # m 結果の表示を(MB)単位に

# df ディスク使用率
df -h sampleDir # ディレクトリのディスク使用率を確認

# sed ファイル内文字操作
# -i ファイルを上書き
sed -i 's/RED/BLUE/g' sample.txt # ファイル内(文字置換 例では「RED」を「BLUE」に置換)
sed -i -e 's/RED/BLUE/g' sample.txt # ファイル内 文字置換
sed -i '$asample_word' sample.txt # ファイル内 行末に追記($aの後ろに追記する文字)

# tree
tree -d -L 2 /sample # 指定ディレクトリを2階層目まで表示

# diff xxx
diff sample.txt after.txt # ファイル内容の差分を確認
diff -s sample.txt after.txt # ファイル内容の差分を確認し一致していたら「同一」と表示

# rsync
# リモートとローカルのファイルとディレクトリの同期
rsync -avz /home/from_dir /home/to_dir # ローカル同期(差分のみ同期できるためcpと差別化

# curl
# ファイルのダウンロード
curl -OL http://sample/sample.zip
# tar
# `v`オプション(メッセージ表示)は必須<br>
# オプションコマンド先頭のハイフンは省略可(タイプ数減少目的に省略推奨)<br>
# ※オプション`-c`と`-C`は意味が違う
# アーカイブ作成(基本型)
tar cvf sample.tar sample.txt # 1つ目がtarファイル、2つ目がアーカイブする対象ファイル
tar cvf sample.tar /tmp sample # tmp下のsampleフォルダごとアーカイブ
# 実務実績有り まとめてアーカイブ作成(「/tmp/sample_1 配下」と「/tmp/sample_2 配下」)
tar -czvf sample.tgz /tmp/sample_1 p/sample_2

# x 展開
# f 展開するファイルを指定
tar xvf /tmp/sample.tar
tar xvzf /tmp/sample.tar.gz -C /tmp/targetDir # -C 指定ディレクトリに展開
tar xvzf /tmp/sample.tgz -C /tmp/targetDir # -C 指定ディレクトリに展開
tar xzf /tmp/sample.tar.gz # tar.gzの展開

# gzip(.gzを扱う)
# -d 展開
gzip -d /tmp/sample.gz

■権限

# chmod 権限変更
chmod 777 sample.txt # 777(パーミッション)

# chown 権限所持者変更
chown sampleuser /tmp/sample.txt
chown -R sampleuser /tmp # -R ディレクトリ配下すべて変更

# chgrp 権限所持グループ変更
chgrp samplegroup /tmp/sample.txt

# 所有者、グループ 同時に変更
chown sample_user:sample_group /tmp/sample.txt

■シンボリックリンク

ln -s targetDir sampleLink # -s シンボリックリンク作成
unlink sampleLink # 削除(参照先は削除しない)

■リダイレクト

ls > sample_log.txt #ファイルに出力
ls >> sample_log.txt #上書きせずに追加

vi … Linuxテキストエディタ

# ----- 通常モード
:q! # 変更を保存せず終了
:wq # ファイルを保存し終了
i # 文字入力モード開始
o #(小文字)現在行の次の行で文字入力モード開始
O #(大文字)現在行の前の行で文字入力モード開始
dd # カーソルのある行を削除
[esc]キー # 文字入力モード終了
1G # ジャンプ機能([任意の数字]+G)

# 文字入力モード

cron ログファイル /var/log/cron ユーザによってcronは異なる

crontab -l # 確認
crontab -e # 編集
crontab -e -u sample_user # 編集 ユーザを指定

# cron
* * * * * sample.sh    # 1分ごとに実行
*/10 * * * * sample.sh # 10分ごとに実行

mount

mount -a # /etc/fstab の全てをマウントする

openssl … xxx

# 秘密鍵
openssl genrsa -out private.pem 2048

■RHEL

# yum
yum install sample    # インストール
yum -y install sample # インストール -y すべて「Yes」
yum search sample # パッケージ検索
yum list installed # インストール済みパッケージ一覧表示
yum list installed | grep sample # インストール済みパッケージ一覧表示 応用

yum deplist zip # deplist パッケージの依存関係一覧を表示

応用

# 特定の拡張子以外のファイルに対してgrep(find + grep)
find targetDir -type f ! -name "*.log" -exec grep -Hn "sample_word" {} \;

■ディレクトリ構成、ファイル一覧を取得し出力

find /target_dir | sort > /tmp/result.txt # こっちのほうがいい 名前順にソート
find /target_dir -print > /tmp/result.txt

■ブラウザからhttpsで接続できない ローカル(サーバ自身)から接続できるか確認するコマンド curl -k https://localhost/

viで日本語入力が文字化けする サーバ環境変数とteratermの漢字入力設定の文字コードを同一にする

■同じ名前のプロセスが大量に作られた、削除したい pgrep sample_process | xargs kill

■swap(RHEL

Swapファイルを作成します。2GB
/bin/dd if=/dev/zero of=/var/swap bs=1M count=2048

Swapファイルとして設定
mkswap /var/swap

Swapファイルを有効に
swapon /var/swap

次回以降の物理サーバの起動時にSwap領域を自動的に有効にするために、Swap領域の設定を追加します。
vi /etc/fstab
/var/swap swap swap defaults 0 0