もた日記

くだらないことを真面目にやる

commandlinefu.comで見つけた便利・面白Linuxコマンド使用例

commandlinefu.com


www.commandlinefu.com

commandlinefu.comという便利なコマンド使用例(ワンライナー)を共有するサイトがあったので気になったコマンドを試してみる。
サイトではユーザが投票できるようになっていてsoted byをvotesにすると人気順に閲覧可能。最近でもコマンド使用例が投稿されているが2009年、2010年頃に投稿されたものが人気なのでその頃に話題だったのだろうか。

f:id:wonder-wall:20170730203010p:plain

コマンド使用例



^foo^bar


直前のコマンドを置換。^が1つだと削除。

$ echo "abc"
abc
$ ^bc^de
ade
$ echo "abc"
abc
$ ^bc
a


mount | column -t


表形式に整形。

$ mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,size=1931660k,nr_inodes=482915,mode=755)
$ mount | column -t
sysfs                    on  /sys                        type  sysfs        (rw,nosuid,nodev,noexec,relatime)
proc                     on  /proc                       type  proc         (rw,nosuid,nodev,noexec,relatime)
devtmpfs                 on  /dev                        type  devtmpfs     (rw,nosuid,size=1931660k,nr_inodes=482915,mode=755)

セパレータを指定することもできるのでcsvファイルも整形可能。

$ column -t -s, < test.csv
1    a
10   ab
100  abc


time read (ctrl-d to stop)


簡易ストップウォッチということらしい。Ctrl-dを押すと停止。

$ time read

real	0m4.534s
user	0m0.000s
sys	0m0.000s


echo "You can simulate on-screen typing just like in the movies" | pv -qL 10


誰かが打っているようにタイピングをシミュレートする。この場合は1秒に10文字ずつ。

$ echo "You can simulate on-screen typing just like in the movies" | pv -qL 10
You can simulate on-screen ty

pvはpipe viewerの略。使えない場合は下記コマンドでインストール(CentOSの場合)。

$ sudo yum install pv

本来は以下のようにパイプの進捗を確認するコマンド。

$ pv access.log | gzip > access.log.gz
 254MiB 0:00:08 [34.9MiB/s] [==============>                                      ] 29% ETA 0:00:18

使い方は下記記事を参照。
wonderwall.hatenablog.com

mv filename.{old,new}


拡張子変更。バックアップファイル作成など。

$ cp file.txt{,.bak}


diff <(sort file1) <(sort file2)


ソートされていないファイルを比較する。テンポラリのファイルを作成しなくてよい。

!*


直前のコマンドのパラメータを再利用する(先頭のみ除く)。

$ vi cd work
$ !*
cd work


!:-


直前のコマンドの最後のパラメータ以外を再利用する。

$ curl -sS -o /tmp/test http://httpbin.org
$ !:- http://www.commandlinefu.com

と実行すると、下記コマンドと同じ。

$ curl -sS -o /tmp/test http://www.commandlinefu.com


fc


直前のコマンドをエディタで編集。

date -d@1234567890


UnixTimeの変換。

$ date -d@1234567890
2009年  2月 14日 土曜日 08:31:30 JST


ps awwfux | less -S


プロセスの親子関係を表示。

root     20502  0.0  0.1 140780  4956 ?        Ss    7月29   0:00  \_ sshd: vagrant [priv]
vagrant  20504  0.0  0.0 142012  3328 ?        S     7月29   0:03      \_ sshd: vagrant@pts/0
vagrant  20505  0.0  0.1 155292  6220 pts/0    Ss    7月29   0:13          \_ -zsh
vagrant  11105  0.0  0.0 115660  2432 pts/0    S    20:39   0:00              \_ /bin/bash
vagrant  11262  0.0  0.0 139632  1708 pts/0    R+   21:03   0:00                  \_ ps awwfux


bind -P


キーバインド表示。

$ bind -P

abort can be found on "\C-g", "\C-x\C-g", "\e\C-g".
accept-line can be found on "\C-j", "\C-m".
alias-expand-line is not bound to any keys


mkdir -p work/{d1,d2}/{src,bin,bak}


ディレクトリツリー作成。

$ mkdir -p work/{d1,d2}/{src,bin,bak}
$ ls -R work/
work/:
d1  d2

work/d1:
bak  bin  src

work/d1/bak:

work/d1/bin:

work/d1/src:

work/d2:
bak  bin  src

work/d2/bak:

work/d2/bin:

work/d2/src:


showkey -a


押したキーのキーコードを表示。

$ showkey -a

Press any keys - Ctrl-D will terminate this program

a 	 97 0141 0x61
1 	 49 0061 0x31
! 	 33 0041 0x21


ccze


ログに色を付ける。

$ tail -f /var/log/messages | ccze -A
$ tail -f /var/log/exim4/mainlog | ccze -A

f:id:wonder-wall:20170730211732p:plain

使えない場合は下記コマンドでインストール(CentOSの場合)。

$ sudo yum install ccze

使い方は下記記事を参照。
wonderwall.hatenablog.com

grep . filename > newfilename


空白行を削除して出力。

grep -Fx -f file1 file2


共通行を表示。

$ cat test1.txt
1
2
3
$ cat test2.txt
3
4
5
$ grep -Fx -f test1.txt test2.txt
3


grep . *


ファイル名付きでcatしたい場合。

$ grep . *
test1.txt:1
test1.txt:2
test1.txt:3
test2.txt:a
test2.txt:b
test2.txt:c


kill -9 $$


コマンド履歴を残さずにexit。$$はログインしているシェルのプロセスID。
詳しくは下記リンク参照。
qiita.com

tail -f file | while read; do echo "$(date +%T.%N) $REPLY"; done


タイムスタンプ付きでtail -f

$ tail -f file | while read; do echo "$(date +%T.%N) $REPLY"; done
21:26:37.785311678 test1
21:26:40.622065180 test2
21:26:43.435610201 test3


set -o vi


コマンド入力をviモードにする。
現在のオプションは下記で確認可能。

$ set -o

元に戻す場合。

$ set -o emacs