LoginSignup
16
16

More than 5 years have passed since last update.

よく使うシェルコマンドとか

Last updated at Posted at 2014-05-23

よく使うシェルコマンド

# プロセスツリー一覧
# 多重起動してる場合に見やすい
$ pstree -Ua
$ pstree -Uahnup
$ ps aux --forest
$ ps axjf

# プロセス一覧
$ ps aux

# top
$ top
[c] 表示変更
[n][30] 30行表示
[1] マルチCPU別々表示

ls

# 更新日順直近10個リスト(-t) 逆順にする場合は(-r)
$ ls -alFtr --color | tail -n 10

ヒストリーコマンド

$ history
# tarコマンドの上下3行を付けてgrep
$ history | grep -n3 tar

圧縮(tarコマンド)

$ tar cvzf ~/sore20160808.tar.gz ~/sore

検索

# カレントディレクトリ配下のファイルに文字列が含まれているファイルをピックアップ
# brew install ag > the_silver_searcher-0.26.0.yosemite.bottle.tar.gz
$ ag 'hogehoge' ./
$ ack 'hogehoge' ./
$ grep -r 'hogehoge' ./
$ find ./ -name '*' | xargs grep hogehoge
$ ack --nocolor --nogroup -i -C 5 'hoge' ./ (emacsのgrep-findで使う場合)
### ファイル名 ua.js を検索してそれぞれの更新日付・サイズを確認する
$ for l in `find ./ -name 'ua.js' | sort`; do ls -l $l; done
### 削除
$ for l in `find ./ -name 'hogehoge.txt' | sort`; do echo $l; done
$ for l in `find ./ -name 'hogehoge.txt' | sort`; do rm $l; done

# ファイル名そのものを検索
$ find ./ -name 'hogehoge'
$ # シングルクォートは不要だがワイドキャラクタ(漢字)の場合やスペースを含む場合は必要
$ find ./ -name 'hogehoge' [-type d|-type f|-type l]

# 更新日など詳細が見えなくてよいときは -print を指定するか、省略する。
# 更新日が最近の10日以内のファイルを見つける。場所はカレントディレクトリ「.」以下から。詳細に(-ls)
$ find . -mtime -10 -ls
# 更新日が最近の10日以上前のファイルを見つける(こう書くと古い方を見てしまう)
$ find . -mtime +10
# 更新日がちょうど○日のファイルを見つける
$ find . -mtime 10

agについて The Silver Searcher

ackより早い検索コマンド

$ ag hoge
# 大きいjsを除外しつつ検索 (~/.agignore に登録していいかも)
$ ag useragent --ignore *min.js --ignore jquery*

ackについて

ackコマンド - Web就活日記
http://yut.hatenablog.com/entry/20110523/1306111855

$ ack -a hoge
全ファイル対象の検索

$ ack --nocolor --nogroup -i -C 5 'ほげ' ~/source/ソースパス/
# --nogroup 1行毎にファイルパスが付く
# -i 大文字小文字を区別しない
# -C 5 ヒットの前後5行を付けて表示

# 拡張子 .inc が検索対象とならない場合のrcファイル
$ cat >> .ackrc
--type-set=inc=.inc
[ctrl]+[d]

# emacsへの設定
;; M-x grep-findでPerlのackコマンドを使うように変更
(setq grep-find-command "ack --nocolor --nogroup -i -C 5 'ほげ' ~/source/ソースパス/ ")

headとtail

# 一括tail
$ tail -n 100 *.log
# 一括tailと監視
$ tail -n 100 *.log -f

# 部分cat 100行目から200行目
$ head hoge.log -n 200 | tail -n 100

# サブフォルダ配下のログを一括Tail監視
$ tail -f -n 10 `find \`ls -1\` | grep \.log$`
$ # tailオプション
$ # -f 監視
$ # -n 10 tail 10行表示

svn

# 差分カラー表示
$ svn diff | vim -R -

hanhan's blog » svn diff に色を付ける方法まとめ http://hanhans.net/blog/hanhan/2011/02/25/svn-diff-%E3%81%AB%E8%89%B2%E3%82%92%E4%BB%98%E3%81%91%E3%82%8B%E6%96%B9%E6%B3%95%E3%81%BE%E3%81%A8%E3%82%81/

時々使いそうなシェルコマンド

バイナリダンプ

$ hexdump -C 7000/dump.rdb
# まとめてダンプ
$ find ./ -type f -name 'dump.rdb' -exec ls -l {} \; -exec hexdump -C {} \;

osxに入ってるコマンドでバイナリ比較 - Qiita http://qiita.com/polikeiji/items/7110dd595a5fb3819464

ディレクトリごとの容量

$ du -sh ./**
$ du -sh ./_samples/**
# node_modulesディレクトリがあるディレクトリを探す
$ find ./ -type d -name 'node_modules' -prune > node_modules.txt

よく使わないシェルコマンド

Open AndroidStudio project from command line on OSX

Android Studio を起動したメニューからショートカットスクリプトを作れる
新しいwindowで開くかどうか聞かれるかと思いきや、駄目だった。

Menu --> Tools --> Create command line launcher.

$ ll /usr/local/bin/studio
-rwxr--r-- 1 aaa staff 3207  5  2 04:45 /usr/local/bin/studio*
$ studio --help
Usage:
  /usr/local/bin/studio -h | -? | --help
  /usr/local/bin/studio [project_dir]
  /usr/local/bin/studio [-l|--line line] [project_dir|--temp-project] file[:line]
  /usr/local/bin/studio diff <left> <right>
  /usr/local/bin/studio merge <local> <remote> [base] <merged>
$ studio `pwd`

秘密鍵のパスワード忘れて試行錯誤する時のコマンド

Mavericks で ssh 接続できない | アンドロイドな日々 http://android.ohwada.jp/archives/5307

ssh ほげほげ -v -- debugモードで接続ね

自らハックはこちら

ekaneko@kfc ~ $ cp .ssh/id_rsa .ssh/id_isa.orig
ekaneko@kfc ~ $ ssh-keygen -f id_rsa -p
Enter old passphrase:
Bad passphrase.
ekaneko@kfc ~ $ ssh-keygen -f id_rsa -p
Enter old passphrase:
Bad passphrase.
ekaneko@kfc ~ $ # そして日は暮れ野には獣が…
ekaneko@kfc ~ $ ssh-keygen -f id_rsa -p
Enter old passphrase:
Key has comment 'xxxxxxxx'
Enter new passphrase (empty for no passphrase):xxxxxxxx
Enter same passphrase again:xxxxxxxx
Your identification has been saved with the new passphrase.
ekaneko@kfc ~ $ # Fuck!

ターミナルソフトでコピペしてファイル作る

ekaneko@bibian ~ % cat >~/create_character.sql
[ここでターミナルソフトからペースト]
use `mytest`;
CREATE TABLE `character` (
  `player_id` bigint(20) NOT NULL DEFAULT '0',
  `character_id` BIGINT(20) NOT NULL DEFAULT '0',
  `level` bigint(20) DEFAULT '0',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`player_id`, `character_id`),
  KEY `player_status_unique_IX1` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='てすてす';
[ctrl]+[d]
ekaneko@bibian ~ % ll
合計 8
-rw-r--r-- 1 ekaneko ekaneko  387  6月  2 12:04 create_character.sql
drwxr-xr-x 6 ekaneko ekaneko 4096  5月 29 15:00 work
ekaneko@bibian ~ % mysql -uroot -p < ~/create_character.sql
Enter password: 
ekaneko@bibian ~ % mysql -uroot -p --database=mytest -e'show tables'
Enter password: 
+------------------+
| Tables_in_mytest |
+------------------+
| character        |
+------------------+
ekaneko@bibian ~ % 

csvファイルをutf-8に変換して行頭から3つだけに再編集する

$ nkf -wLu hoge.csv | awk -F , '{for(i=1;i<3;i++)s=s $i ",";print s;s="";}'

ワンライナーとか

ちょっとだけMySQLあります

# カレントディレクトリのファイル拡張子リネーム
$ for l in `ls *.sql -X1 | sort`; do n=${l%sql}"done"; mv $l $n; done
# カレントディレクトリ配下の CRLFファイル、BOMファイルを検索
$ find ./ -type f -exec file {} \; | grep "with CRLF"
$ find ./ -type f -exec file {} \; | grep "(with BOM)"
# カレントディレクトリ配下のhogehogeファイルを削除
$ for l in `find ./ -name 'hogehoge.txt' | sort`; do echo $l; done
$ for l in `find ./ -name 'hogehoge.txt' | sort`; do rm $l; done
# カレントディレクトリのファイル一覧とサイズだけをリストする
$ ls -AGUfl | sed '/^d/d' | awk '{print $5 "," "\"" substr($0, index($0, $9)) "\""}'
,"total 5449360"
22532,".DS_Store"
17507,"electric.el"
5789,"electric.el.gz"
# ディレクトリ配下のファイル全ての場合
$ find ./ -type f -ls | awk '{print $7 "," "\"" substr($0, index($0, $11)) "\""}'

# mysql プロセス監視
$ while :; do clear 1>&2; mysql -uscott -p --table -Be 'show processlist;'; sleep 5; done

svn st を日付順に見たいとおっしゃられたので・・・

svn st | awk '{print $2}' | xargs ls -altr | awk '{for(i=5;i<NF+1;i++)printf "%8s ",$i;print "";}'

制御構文とか

# if文書くのむずい
$ if [ "MacMini" = `hostname -s` ]; then echo "hit"; fi
$ if [ "MacMini" = `hostname -s` ]; then echo "hit"; else echo "fail"; fi
16
16
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
16
16