諸事情によりコマンドを覚える必要があったためメモ。 とりあえず用途別に。
パス関連
現在のディレクトリの表示
% pwd /Users/myname/commandtest
パス名からファイル・ディレクトリ名のみ抽出する
% basename /Users/myname/commandtest commandtest
コマンドのパスの表示
% which pip /Users/myname/.pyenv/shims/pip
時間関係
カレンダー表示
% cal 12月 2016 日 月 火 水 木 金 土 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
定期的に自動でコマンド実行
crontab -e
で開いた設定ファイルに設定を記述する。
ファイル関係
権限
パーミッションを変更する
chmod g+w,o= test.txt
所有者を変更
-R
でディレクトリ内部の所有権をすべて変更する。
chown -R testdir
ファイルの一部分を切り出す
% cut -c1-3 test.txt aaa
ファイル間の差分を調べる
% diff -w test.txt test2.txt 1a2 > bbb
作成・編集・置換
X行の指定サイズのファイルを作成
% yes "" | head -10 > test.txt
yes xxx
はひたすら xxx
を生成するだけのコマンドなのでこれだと改行のみ10行のファイルを test.txt
に出力したことになる。
一応確認してみると
% wc test.txt 10 0 10 test.txt
となっている。
差分ファイルの作成
bsdiff test.old test.new test.bsdiff
X個の乱数を含んだファイルを生成
0~100の乱数を5こ生成する。
% jot -r 5 68 64 23 52 95
表示を整えるには -w
を指定して
% jot -r -w %05d 5 00081 00044 00080 00015 00008
とする。
ファイルに含まれる半角スペースをタブに置換
% unexpand test.txt
ファイルに含まれる文字列を正規表現でマッチして置換
% sed 's/[a-z0-9]*.txt/XXXX/' < test.txt
指定文字列を置換して別ファイルに出力
% tr a b < test2.txt > test3.txt
結果は
% cat test2.txt aaaaa bbb % cat test3.txt bbbbb bbb
となり a
がb
に痴漢された。
ファイルを1KBごとに分割して出力
split -b 1k test.txt
表示
ファイルの中身を表示
% cat test.txt aaaaa
ファイルの先頭から〜行表示
% head test.txt aaaaa
ファイルの一時的な表示
less test.txt
連続する同じ内容の行をまとめて表示
% uniq test.txt aaaaa
ファイルを行ごとにソートして表示
% cat test.txt | sort -u aaaaa
ファイルサイズ・容量
ディスクの使用容量を確認する
% df Filesystem 512-blocks Used Available Capacity iused ifree %iused Mounted on /dev/disk1 487830528 386078104 101240424 80% 48323761 12655053 79% / devfs 360 360 0 100% 624 0 100% /dev map -hosts 0 0 0 100% 0 0 100% /net map auto_home 0 0 0 100% 0 0 100% /home
カレントディレクトリの容量
% du -h 8.0K
チェックサムのサイズ
% cksum test.txt 2550443804 6 test.txt
行数・単語数・ファイルサイズ・ファイル名の表示
% wc test.txt 1 1 6 test.txt
ネットワーク・送受信
現在のネットワーク接続の状況確認
% netstat Active Internet connections Proto Recv-Q Send-Q Local Address Foreign Address (state) tcp4 0 0 ***.***.***.** ****.****.****.https ESTABLISHED tcp4 0 0 localhost.12443 localhost.58621 ESTABLISHED tcp4 0 0 localhost.58621 localhost.12443 ESTABLISHED
リモート・ローカル間でファイルをコピー
-r
はディレクトリのコピーの際に再帰的な処理を行うために必要。
scp -r username@10.***.***.00:dirname ~/tempdir
プロセス
実行中のプロセスを確認
実行中のプロセスからnotebook
が含まれているものを探して表示。
% ps aux | grep notebook myname 3955 0.0 0.0 2423376 216 s001 R+ 5:32PM 0:00.00 grep notebook
ログアウト後でもバックグラウンドでコマンドを実行
nohup コマンド名 &
です。
% nohup jupyter notebook &
その他
コマンドの実行時間を計測
time
以下に計測したいコマンドを記述する。
% time tr a b < test2.txt > test3.txt tr a b < test2.txt > test3.txt 0.00s user 0.01s system 72% cpu 0.010 total
コマンドの出力結果をコマンドラインに表示しつつファイルに出力
% cat test3.txt | tee test4.txt bbbbb bbb
すると
% cat test4.txt bbbbb bbb
となっていて test4.txt
にコマンドラインに表示された内容が出力される。
ファイルに追記された内容をリアルタイムで確認
% tail -f test.txt
コマンドラインを指定秒数止める
% sleep 10
指定した文字列を行頭に持つ英単語を検索
% look pix pix pixie pixilated pixilation pixy