(備忘録)たまに使うけど書き方をよく忘れてしまうコマンド


はじめに

自分の備忘録用です
忘れて調べ直してしまうのでメモです。。。
自分用なので、分かりにくい点や情報、技術が古いかもしれませんがご了承ください
他にも思いついたら追記していこうと思います。。。

参考資料

参考にさせて頂きました

環境 ※以下のVerでなくても動くと思いますが、古いのでご注意下さい

Ubuntuバージョン
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.4 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.4 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic
シェル
$ echo $SHELL
/bin/bash

ファイル検索(find)

$ find -name *Study*
./app_tensor/mkdbAndStudy.py
$ find -regex '.*Study.*'
./app_tensor/mkdbAndStudy.py

文字列検索(grep)

$ grep -r 雲がゆくのは ./*
./app_tensor/inputFile/ans_studyInput_fork.txt:6,T,雲がゆくのは,映画|武田鉄矢|切ない|知らない誰かの幸せ願う
./app_tensor/exeWhatMusic.py:LABELS = ["TSUNAMI", "雲がゆくのは", "空も飛べるはず", "糸", "おなじ話"

# ファイル名指定
$ grep -E '.*ゆくのは.*' ./app_tensor/mkdbAndStudy.py 
labelToCode = {"TSUNAMI":0, "雲がゆくのは":1, "空も飛べるはず":2

# syslog を日付と"error"でgrep
$ grep --regex '.*20:02:26.*error.*' /var/log/syslog
May 20 20:02:26 user-PC gnome-shell[907]: Error setting property 'Powered' on interface 
May 20 20:02:26 user-PC gnome-shell[2049]: Error setting property 'Powered' on interface 

区切られたテキストを加工(awk)

$ cat awk_test.txt 
A1,A2,A3,A4,A5
B1,B2,B3,B4,B5
C1,C2,C3,C4,C5
D1,D2,D3,D4,D5

# ファイルをcatして、「,」区切りで左から3番目、5番目抽出して加工
$ cat awk_test.txt | awk -F "," '{print $3 " + " $5}'
A3 + A5
B3 + B5
C3 + C5
D3 + D5

# 又はこちらでも同じ
$ awk -F "," '{print $3 " + " $5}' awk_test.txt 
A3 + A5
B3 + B5
C3 + C5
D3 + D5

複数のスペースを一つにする(sed)

$ cat sed_test.txt 
aaa bbb ccc      # sp x 1
aaa  bbb ccc     # sp x 2
aaa   bbb   ccc  # sp x 3

# 2文字のスペースを一つのスペースに変換 → (space)(space)(space)*
$ cat sed_test.txt | sed -e 's/   */ /g'
aaa bbb ccc # sp x 1
aaa bbb ccc # sp x 2
aaa bbb ccc # sp x 3

テキストファイルをキー指定で結合する(join)

$ cat join1.txt 
1,join1-1-2,join1-1-3
2,join1-2-2,join1-2-3
3,join1-3-2,join1-3-3
4,join1-4-2,join1-4-3

$ cat join2.txt 
1,join2-1-2,join2-1-3
2,join2-2-2,join2-2-3
3,join2-3-2,join2-3-3
4,join2-4-2,join2-4-3

# INNER JOIN
$ join -t "," -1 1 -2 1 join1.txt join2.txt
1,join1-1-2,join1-1-3,join2-1-2,join2-1-3
2,join1-2-2,join1-2-3,join2-2-2,join2-2-3
3,join1-3-2,join1-3-3,join2-3-2,join2-3-3
4,join1-4-2,join1-4-3,join2-4-2,join2-4-3

$ cat join3.txt 
1,join3-1-2,join3-1-3
3,join3-3-2,join3-3-3
5,join3-5-2,join3-5-3

# LEFT OUTER JOIN
$ join -t "," -a 1 -1 1 -2 1 join1.txt join3.txt
1,join1-1-2,join1-1-3,join3-1-2,join3-1-3
2,join1-2-2,join1-2-3
3,join1-3-2,join1-3-3,join3-3-2,join3-3-3
4,join1-4-2,join1-4-3

テキストファイルから指定した文字より後ろを抽出(substr, index)

$ cat substr_test.txt 
A1,A2,A3=row1,A4,A5
B1,B2,B3=row2,B4,B5
C1,C2,C3=row3,C4,C5
D1,D2,D3=row4,D4,D5

# awkで3列目を抜き出して、"="より後ろの文字列を取り出す
$ awk -F ',' '{print substr($3, index($3, "=")+1)}' substr_test.txt 
row1
row2
row3
row4

標準出力に表示をしながらファイルに保存(tee)

$ grep --regex '.*Jun 14 00:33.*error.*' /var/log/syslog | tee error.txt
Jun 14 00:33:06 xxx systemd-resolved[538]: Server returned error NXDOMAIN
Jun 14 00:33:06 xxx systemd-resolved[538]: message: [ Server returned error NXDOMAIN

$ cat error.txt 
Jun 14 00:33:06 xxx systemd-resolved[538]: Server returned error NXDOMAIN
Jun 14 00:33:06 xxx systemd-resolved[538]: message: [ Server returned error NXDOMAIN

計算(expr)

$ expr 1 + 1
2

ファイルとかディレクトリ情報(ls)

# 日時の古い順で並び替えてサイズ見やすく表示
$ ls -ltrh
合計 16K
drwxr-xr-x 2 okubo okubo 4.0K  5月 16 09:09 web_nginx
-rw-rw-r-- 1 okubo okubo 1.3K  5月 16 10:39 docker-compose.yml
-rw-r--r-- 1 okubo okubo  910  5月 16 13:18 README.md
drwxr-xr-x 9 okubo okubo 4.0K  5月 19 22:25 app_tensor

# サイズの昇順に並び替え(-k 5 でサイズ(左から5番目)を指定)
$ ls -lth | sort -k 5
合計 16K
-rw-r--r-- 1 okubo okubo  910  5月 16 13:18 README.md
-rw-rw-r-- 1 okubo okubo 1.3K  5月 16 10:39 docker-compose.yml
drwxr-xr-x 2 okubo okubo 4.0K  5月 16 09:09 web_nginx
drwxr-xr-x 9 okubo okubo 4.0K  5月 19 22:25 app_tensor

ディスク容量表示(df、du)

# ディスク容量表示
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            3.7G     0  3.7G   0% /dev
/dev/sda1       229G   49G  168G  23% /
tmpfs           3.7G  654M  3.1G  18% /dev/shm
/dev/loop1      2.5M  2.5M     0 100% /snap/gnome-calculator/730

# 指定ディレクトリのディスク使用量表示
$ du -sh web_nginx/
12K web_nginx/

# ディレクトリ配下のディスク使用量表示
$ du -sh ./*
4.0K    ./README.md
2.9M    ./app_tensor
4.0K    ./docker-compose.yml
12K ./web_nginx

コマンドの実行結果を引数に別のコマンドを実行(xargs)

# dateコマンドの実行結果を引数にecho実行
$ date "+%Y/%m/%d" | xargs -I {} echo "[" {} "]"
[ 2020/05/20 ]

# dateコマンドの実行結果を引数にgrep実行
$ date "+%Y/%m/%d" | xargs -I {} grep --regex '{} 13:.*' abc.log
2020/05/20 13:11:10

権限変更など

# 読み書きが自分、グループメンバ、他人できる
chmod 666 ファイル名
# ファイルのユーザーとグループを変更 
chown ユーザ:グループ ファイル名
# ファイルの所有者のみ実行権限与える
chmod u+x ファイル名

ログの後尾をリアルタイム表示

$ tail -f /var/log/syslog
May 20 20:28:36 user-PC kernel: [78648.697825] audit: type=1400 audit(1589974116.974:77): 
May 20 20:28:37 user-PC kernel: [78649.409555] audit: type=1400 audit(1589974117.686:78):  

cpu使用率

# MB表示、閉じる時「cntr+c」
$ vmstat -t --unit M 2
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- -----timestamp-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st                 JST
 2  0   1640    138     79   1261    0    0   139   269  128  214 32  7 61  0  0 2020-05-20 20:41:22
 0  0   1640    136     79   1261    0    0    10    68 1724 3402 24  2 74  0  0 2020-05-20 20:41:24
 0  0   1640    134     79   1261    0    0     0     0 1545 2933 22  1 77  0  0 2020-05-20 20:41:26
 0  0   1649    159     79   1243    0    5   316 10200 1529 2977 29 10 61  0  0 2020-05-20 20:41:28

参考

$ top
# 閉じる時「q」
top - 20:51:21 up 4 days, 21:20,  1 user,  load average: 0.67, 0.76, 0.73
Tasks: 363 total,   1 running, 316 sleeping,   1 stopped,   0 zombie
%Cpu(s):  6.6 us,  1.9 sy,  0.0 ni, 91.6 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  7682812 total,   189004 free,  6088124 used,  1405684 buff/cache
KiB Swap:  2097148 total,   394564 free,  1702584 used.   750084 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 2049 user      20   0 3566012 243964  31780 S   4.0  3.2  53:28.05 gnome-shell
 1906 user      20   0  725820  57600  28296 S   3.0  0.7  35:17.79 Xorg                     
 4395 user      20   0  853616  29596  15248 S   2.6  0.4   1:02.87 gnome-terminal-

参考

プロセス

# プロセス確認
$ ps -ef | grep 確認したい文字

# プロセス削除 ※注意
kill プロセスID

マニュアル見る

man みたいコマンド

ファイルの圧縮

# gzip ファイル名
$ gzip test.py

# tar zcvf 圧縮後ファイル.tar 圧縮ファイル1.txt 圧縮ファイル2.txt
$ tar zcvf tar_test.tar test2.py test3.py test4.py 

ファイルの解凍

# gunzip 解凍したいzipファイル
$ gunzip test.py.gz

# tar zxvf 解凍したいtarファイル
$ tar zxvf tar_test.tar

テキスト バックアップ

$ tail -500 syslog > /home/user/backup_syslog

$ grep --regex '.*20:02:26.*error.*' /var/log/syslog > /home/user/backup_errlog

Java コンパイル〜実行

# コンパイル
$ javac Test.java
# 実行 ※ヒープの最大サイズを指定する場合 java -Xmx512m Test
$ java Test
開始時刻:1589982488981 ms
終了時刻:1589982489643 ms
処理時間:662 ms

# バックグラウンドで実行する場合は「&」つける
$ java Test &