5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

簡単なシェルコマンドの備忘録

5
Last updated at Posted at 2016-10-11

下記のtest.txtのabで始まるレコード数をカウントする

test.txt
abc
bcd
efg
abc
abcd
ab
1abc
1ab
cat test.txt|grep '^ab'|wc -l

# =>   4

上記のtest.txtのabで始まるレコードを、レコード内容でまとめて出現数をカウントする

cat test.txt |grep '^ab'|uniq -c

# =>   2 abc
# =>   1 abcd
# =>   1 ab

カレントディレクトリ以下のファイルをgrepしてabで始まるレコードを検索する(通常のgrepでファイル検索するよりも高速)

find . -type f -print0|xargs -0 grep '^ab'

# => ./test.txt:abc
# => ./test.txt:abc
# => ./test.txt:abcd
# => ./test.txt:ab

特定のプロセスをまとめて停止させる

ps ax|grep -i xxxx|grep -v grep|awk '{print $1}'|xargs kill -9

カレントディレクトリ以下のファイルを条件検索して、該当ファイルを50ファイルづつまとめて消去する

# 9日前のファイルを消す
find . -type f  -mtime 9 | xargs -n50 rm -f

# 9日前を範囲指定する場合は9ではなく -10 と +8 で指定する
find . -type f  -mtime -10 -mtime +8 | xargs -n50 rm -f

# 上記条件に ファイル名がtestで始まるものという条件を追加する
find . -type f  -mtime -10 -mtime +8 -name "test*" | xargs -n50 rm -f

xargsで標準入力を操作するコマンドでないコマンドを使用する

xargsに -I オプションを使い、置換用の文字を指定する

# カレントディレクトリ以下のファイル名がtで始まるtxtファイルを .tmpという拡張子をつける
find . -type f -name "t*.txt" -print0 | xargs -0 -I% mv % %.tmp

xargsで実行するコマンドを複数続ける

コマンドを直接渡すのではなく、sh -c にスクリプトとして記載するとよい

# カレントディレクトリ以下のファイル名がtで始まるtxtファイルの内容をoutというファイルにバックアップして ファイル名に.tmpという拡張子をつける
find . -type f -name "t*.txt"  -print0 | xargs -0 -I% sh -c "cat % >>out;  mv % %.tmp"

ssh 公開鍵/秘密鍵の作成

セキュリティが高いed25519形式で作る

# 作成
ssh-keygen -t ed25519 -C "コメント" -f "ファイル名"

# 公開鍵/秘密鍵の形式確認
ssh-keygen -l -f "ファイル名"
# => 256 xxxxxxxxxxxx (ED25519)

SSL関連

key/csrファイルの発行

Symantecのcsr発行手順を参照

  • keyの作成
# ssl private keyの作成
openssl genrsa -des3 -out server1.key 2048

# keyよりパスフレーズを解除する
openssl rsa -in server1.key -out server1.key 
  • csrの作成
    シマンテックの説明ページでは、CAにsha-2署名crt証明書発行依頼でも、csrはsha-1署名のままでOKとなっているが、二つの署名のアルゴリズムが異なっているのは気持ち悪いのでここの説明ではsha−2(sha256)で作成する
# csr
openssl req -sha256 -new -key server1.key -out server1.csr

ここの説明用に自己署名証明書を発行する

# 自己署名証明書
openssl x509 -sha256 -days 365 -req -signkey server1.key  < server1.csr >server1.crt

各ファイルの確認コマンド

  • keyファイル
# text 形式での確認
openssl rsa -noout -text -in server1.key

  • csrファイル
# text 形式での確認
openssl req -noout -text -in server1.csr
  • crtファイル
# text 形式での確認
 openssl x509 -noout -text -in server1.crt
  • csrファイルがそのkeyファイルを使用しているのかを確認する
# diffで確認、出力がない場合には、該当keyを使用している
diff <(openssl req -noout -modulus -in server1.csr) <(openssl rsa -noout -modulus -in server1.key)
  • crtファイルがそのkeyファイルを使用しているのかを確認する
# diffで確認、出力がない場合には、該当keyを使用している
diff <(openssl x509 -noout -modulus -in server1.crt) <(openssl rsa -noout -modulus -in server1.key)
5
3
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
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?