CentOSを使用していますが、自分用にLinux系のコマンド・設定をまとめたページにしたいと思います。結構随時追加しています。
検索・ファイル内操作
find ファイル検索
$ find ファイル名 -ls
grep ファイル内検索
$ grep "hoge" *.txt
$ cat hoge.txt | grep "hoge"
cut 区切り文字で分割
$ cut -d " " -f 1 hoge.txt
※半角スペース” “区切りで1番目を取得
sort
$ sort hoge.txt
$ cat hoge.txt | grep "hoge" | sort -r
※-rをつけると降順になる
uniq 集約・集計
$ unic hoge.txt
$ cat hoge.txt | grep "hoge" | sort -r | uniq -c
※-cをつけると集約した件数も表示
head 先頭n文字取得
$ head -10 hoge.txt
$ cat hoge.txt | grep "hoge" | sort -r | uniq -c | head -10
tail 末尾n文字取得
$ tail -10 hoge.txt
$ cat hoge.txt | grep "hoge" | sort -r | uniq -c | tail -f
※-fはリアルタイムで表示(ログを監視する際などに使用)
ディレクトリ内ファイル一覧表示
詳細情報まで表示
$ ls -la
タイムスタンプ新しい順に表示
$ ls -lt
タイムスタンプ古い順に表示
$ ls -ltr
ファイルコピー
cp ファイルコピー
$ cp old.txt new.txt
属性もコピー
$ cp -pold.txt new.txt
ディレクトリごとコピー
$ cp -r old_dir new_dir
ファイル削除
rm ファイル削除
$ rm [ファイル]
ディレクトリごと削除
$ rm -rf [ディレクトリ]
ディレクトリ作成
mkdir ディレクトリ作成
$ mkdir [ディレクトリ]
サブディレクトリごと作成
$ mkdir -p [ディレクトリ]/[サブディレクトリ]
ファイル同期
ディレクトリ同期・バックアップ
rsync -av test-dir/ testdir-bk/
ディレクトリ同期・バックアップ(ssh経由)
rsync -av -e "ssh -p 22" test-dir/ user@xxx.xxx.xxx.xxx:/test-dir/
ディレクトリ同期・バックアップ(ssh経由・削除あり)
同期先だけにあるファイルも削除されます。完全同期をとる場合に使用。
rsync -av --delete -e "ssh -p 22" test-dir/ user@xxx.xxx.xxx.xxx:/test-dir/
圧縮
tar 圧縮してアーカイブ
$ tar -cvzf testdir.gz /testdir
解凍
tar 解凍して展開
$ tar xvzf testdir.gz
シンボリックリンク
ファイル間でシンボリックリンクを貼る
例は1つ上の階層にあるファイルに貼ってみました。
$ ln -s ../test_file.txt test_file.txt
ユーザ・パスワード管理
ユーザ作成
# useradd ユーザ名
ユーザ一覧確認
# cat /etc/passwd
ユーザ削除
# userdel -r ユーザ名
パスワード設定変更
# passwd [ユーザ名]
ユーザグループ
ユーザのグループ確認
# groups [ユーザ名]
ユーザのグループ設定
# usermod -G [グループ名] [ユーザ名]
グループのユーザ確認
# getent group [グループ名]
権限関連
chown ファイル所有者変更
# chown user testfile
chown ディレクトリ一括所有者変更
# chown -R user testdir
chown グループにユーザ追加
# chown -G testgroup user
chmod ファイル権限変更
# chmod 777 testfile
chmod ディレクトリ一括権限変更
# chmod -R 777 testdir/
chmod ディレクトリのみ一括権限変更
# find . -type d -print | xargs chmod 755
※findでディレクトリのみ検索し、xargsでその結果を引数としてchmodに渡す。
chmod ファイルのみ一括権限変更
# find . -type f -print | xargs chmod 755
※findでファイルのみ検索し、xargsでその結果を引数としてchmodに渡す。
ログ確認
tail ログなどのリアルタイム表示
$ tail -f hoge.log
alias
aliasに登録して短縮キー化(例としてls -laをllで登録)
$ alias ll='ls -la'
alias ~/.bashrcに登録
$ vi ~/.bashrc で alias ll='ls -la' などと書き $ source ~/.bashrc で使えるようになる
cron
cron設定確認
$ crontab -l
cron編集
$ crontab -e
cron削除(※禁止! 要注意)
$ crontab -r
elinks
https://normalblog.net/system/elinks/
yum
RedHat系のパッケージ管理システムで、リポジトリからパッケージをダウンロードして、インストールできます。
yumインストール
# yum install [インストール対象]
yumインストール(ログが日本語だとわかりづらい場合)
# LANG=C yuminstall [インストール対象]
yum更新・アップデート
# yum update
yumリスト
今持っているリポジトリにインストール対象があるかの確認時などに使用。
# yum list
phpで絞りたい場合
# yum list | grep php
yum リポジトリ格納場所
ダウンロード元リストの記載があるのでリポジトリを追加する際はこちらに追記する。
/etc/yum.repos.d/
自分のCentOS環境では以下ファイルに記載ありました。
/etc/yum.repos.d/CentOS-Base.repo
rpm
インストールパッケージ確認
# rpm -qa | grep php
上記例ではphpとつくパッケージを全部確認しています。
プロセス
実行中のプロセス確認
$ ps
$ ps aux
スペック確認
CentOS OSバージョン確認
$ cat /etc/redhat-release
CentOSが32bitか64bitか確認
$ uname -a
CentOS CPU確認(何を使用しているか
$ cat /proc/cpuinfo
CentOS メモリ使用率確認
$ /usr/bin/free or $ free
df -h 容量確認
$ df -h
du -h ファイル容量確認
$ du -h testfile
該当ディレクトリの容量確認
$ du [ディレクトリ名] --max-depth=1 -m
mオプションでMB単位
メモリ・CPU負荷状況確認
例.2秒毎にコンソールに表示
$ vmstat 2
ネットワーク関連
IPアドレス確認
$ ifconfig
$ ip addr show
$ ip a
IPアドレス設定(ローカルIP設定)
nmcli connection add type ethernet con-name local-eth1 ifname eth1 ip4 192.168.0.1/24 gw4 192.168.0.254
※eth1に192.168.0.1/24を設定
※nmcli = net work manager command line interface
hostsファイルの場所・編集
$ vi /etc/hosts
※windowsの「C:\Windows\System32\drivers\etc\hosts」に該当
ショートカット
Ctrl + d 終了・ログアウト
https://normalblog.net/system/ctrl-d/
Ctrl + c 実行中のプログラムを強制終了
WHOISコマンド
WHOISインストール
# yum install jwhois
WHOIS確認
[root@localhost ~]# whois yahoo.co.jp [Querying whois.jprs.jp] [whois.jprs.jp] [ JPRS database provides information on network administration. Its use is ] [ restricted to network administration purposes. For further information, ] [ use 'whois -h whois.jprs.jp help'. To suppress Japanese output, add'/e' ] [ at the end of command, e.g. 'whois -h whois.jprs.jp xxx/e'. ] Domain Information: a. [Domain Name] YAHOO.CO.JP g. [Organization] Yahoo Japan Corporation l. [Organization Type] Corporation m. [Administrative Contact] HA14103JP n. [Technical Contact] KM43416JP p. [Name Server] ns01.yahoo.co.jp p. [Name Server] ns02.yahoo.co.jp p. [Name Server] ns11.yahoo.co.jp p. [Name Server] ns12.yahoo.co.jp s. [Signing Key] [State] Connected (2017/11/30) [Registered Date] 2000/11/17 [Connected Date] 2000/11/17 [Last Update] 2017/04/20 10:09:53 (JST)
その他
コンパイル
$ make
コマンド確認
$ which [コマンド]
改行コード変換
$ sed -i 's/\r//' [対象ファイル]
/bin/bash^M: bad interpreter: そのようなファイルやディレクトリはありません (No such file or directory)
というエラーが出てきた時には改行コード変換です。windowsからlinux環境へ転送する際に起きてしまう事があります。(設定による)
CentOS7
CentOS7でコマンドが変わったのでこちらに記載します。

