Skip to content

Instantly share code, notes, and snippets.

@yszk0123
Last active September 24, 2016 04:05
Show Gist options
  • Save yszk0123/98ebb3c5ea0b8053d279 to your computer and use it in GitHub Desktop.
Save yszk0123/98ebb3c5ea0b8053d279 to your computer and use it in GitHub Desktop.
よく使うコマンド覚え書き

2015-07-16T18:00:00+09:00

diff

前後のコンテキストも表示するには-cコマンドラインスイッチを使う git diffの場合は-Uコマンドラインスイッチを使う

比較アルゴリズムにおいて,行末の空白文字などを無視する場合は-bを,あらゆるホワイトスペースを無視する場合は-wを使う

grep

  • $ grep dir */*.c => 2階層のgrep

  • $ find . -name '*.c' -print | xargs grep 'dir' => 2階層のgrep (応用)

  • $ find . -name '*.c' -print0 | xargs -0 grep 'dir' => ファイル名に改行が入っている場合など

  • $ grep -n hoge *.md => 行番号が必要な場合

  • $ grep -e '-nanika' *.c => '-'で始まる正規表現

    • 正規表現がコマンドラインスイッチと認識されないように -e コマンドラインスイッチを使う

awk

  • $ cat input.txt | awk '{print $1 * $2;}' => スペース区切りの文字列を2つずつ取り出して掛け合わせる
  • $ cat input.txt | awk -F, '{print $2;}' => カンマ区切り
  • `$ cat input.txt | awk '{print NR " " $2;}' => 行頭に行番号追加
  • $ cat input.txt | awk 'NR % 2 == 0 {print $0;}' => 偶数行のみ出力

cf. awk コマンド | コマンドの使い方(Linux) | hydroculのメモ

Ruby/Rails

  • ActiveSupport::Deprecation.silenced = true => Suppress ActiveSupport warnings
  • $ RUBYOPT=W0 bundle exec rspec path/to/your/spec => Suppress Ruby warnings
  • $ ruby -W0 -Ispec path/to/your/spec => Suppress Ruby warnings -W[level] (level: 0=silence, 1=medium, 2=verbose (default))

cf. ActiveSupport::Deprecation.silenced = true

Git

  • $ git remote prune origin => リモートの使われていないブランチを削除
  • $ git fetch --prune => fetch 時にリモートの使われていないブランチを削除
  • $ git pull --prune => pull 時にリモートの使われていないブランチを削除

cf. Git - リモートで消されたブランチが手元で残ってしまう件を解消する - Qiita

  • $ git branch -a => リモートのブランチも列挙

  • $ git branch -r => リモートのブランチだけ列挙

  • $ git branch --trach <branch> => リモートのブランチを追跡

  • $ git fetch --all == $ git remote update => すべてのリモートブランチをfetch

  • $ git pull --all => すべてのリモートブランチをpull

cf. branch - How to fetch all git branches - Stack Overflow

  • $ git commit --allow-empty -m "make pull request" => 空コミットを作成

  • $ git shortlog => 著者のアルファベット順でソート

  • $ git shortlog -e => メールアドレス表示

  • $ git shortlog -n => コミット数でソート

  • $ git shortlog -s => コミット数だけ表示

  • $ git shortlog -w40,4,8 => 一行の文字数40, 一行目のインデント4, 次の行のインデント8 で表示

cf. transitive.info - git shortlog 使い方

  • $ git log --follow => リネームしたファイルの履歴までたどる

  • $ git worktree => 作業ディレクトリを追加して、同一レポジトリの複数のブランチを同時に編集できるようにする

Git merge

  • $ git mergetool => (conflict 時) デフォルトのマージツールでマージ
  • $ git mergetool -t <your favorite merge tool> => (conflict 時) 指定したマージツールでマージ

cf. Fugitive.vim - resolving merge conflicts with vimdiff

  • $ git log --merge => conflictに影響を与えたコミットのみを表示する

Git diff

  • $ git diff <...> --diff-filter=[ACDMRTUXB*] => 変更の種類に応じてフィルタリング
    • A: Added
    • C: Copied
    • D: Deleted
    • M: Modified
    • R: Renamed
    • T: have their type (mode) changed
    • U: Unmerged
    • X: Unknown
    • B: have had their pairing Broken
    • *: All-or-none

cf. Filter git diff by type of change - Stack Overflow

  • $ git diff <比較元>...<比較先> => 「比較元と比較先の共通祖先」と「比較先」の差分を表示。..とは違うことに注意 e.g. 次のようなブランチ構造の場合
    a -> b -> c
     \-> d
    
    $ git diff c...d => aとdの差分 $ git diff c..d => cとdの差分

sed

  • 一つのファイルを対象にする場合
    • $ grep -l '置換対象の文字列' 置換対象のファイル | xargs sed -i '' 's/置換対象の文字列/置換後の文字列/g'
  • カレントディレクトリのファイル全てを対象にする場合
    • grep -l '置換対象の文字列' ./ | xargs sed -i '' 's/置換対象の文字列/置換後の文字列/g'
  • ファイル名に正規表現を使う場合
    • grep -l '置換対象の文字列' ./hoge* | xargs sed -i '' 's/置換対象の文字列/置換後の文字列/g'

cf. 複数のファイル内の文字列をまとめて置換するLinuxコマンド - Qiita

curl

  • $ curl -H "Accept: application/json" -H "Content-type: application/json" -X <GET | PUT | POST | DELETE | PATCH> <url>
  • $ curl -H "Accept: application/json" -H "Content-type: application/json" -d '{"your":"json","data":true}' -X GET <url>
  • $ curl -H "Accept: application/json" -H "Content-type: application/json" -d your=json -d data=true -X GET <url>

cf. Node.js + Express 4.x + MongoDB(Mongoose)でRESTfulなjsonAPIサーバの作成を丁寧に解説する.+ テストクライアントを用いたAPIテストまで - Qiita

cp

  • echo dir1 dir2 dir3 | xargs -n 1 cp file1 => コピー元ではなく、コピー先を複数指定

cf. linux - How to copy a file to multiple directories using the gnu cp command - Stack Overflow

cron, at

at コマンドでキューにジョブを登録し atq でキュー一覧を確認 at は shebang が使えない。sh のみ

  • $ at <time> -f <file> => 指定時刻に実行ファイルを実行 time = "HH:mm DD.MM.YY" (e.g. 2020-11-30T18:00:00 の場合 "18:00 30.11.20")
  • $ at -c <job number> => 登録されたジョブの確認

cf. atコマンドの使い方(指定時間にコマンドを実行する)でスケジュール実行。 - それマグで!

shell

  • $ command 2> error.log => redirect the standard error to a file
  • $ command 2> error.log => redirect stderr and stdout to a file
  • $ command > stdout.log 2> stderr.log
  • $ command 2>&1 | next-command => redirect stderr to stdout and pipe into next-command
Handle Name Description
0 stdin Standard input
1 stdout Standard output
2 stderr Standard error

cf. BASH Shell: How To Redirect stderr To stdout ( redirect stderr to a File )

  • echo $(pwd) => シェルスクリプトを実行したディレクトリ (絶対パス)
  • echo $(dirname $0) => シェルスクリプトの置かれたディレクトリ (相対パス)
  • echo $(cd $(dirname $0) && pwd) => シェルスクリプトの置かれたディレクトリ (絶対パス)

cf. シェルスクリプトで相対パスと絶対パスを取得する - TASK NOTES

Vim

正規表現

  • '\l' pattern = 直後の文字を小文字に変換
  • '\u' pattern = 直後の文字を大文字に変換
  • :%s/\v([A-Z])/-\l\1/g => Convert camelCase to kebab-case (e.g. 'fooBarBaz' => 'foo-bar-baz')
  • :%s/\v([a-z])/\u\1/g => Convert kebab-case to camelCase (e.g. 'foo-bar-baz' => 'fooBarBaz')

cf. Converting variables to or from camel case - Vim Tips Wiki - Wikia

  • /foo-\(bar\)\@= => foo-bar の foo- にマッチ
  • /foo-\(bar\)\@! => foo-hoge の foo- にマッチ (bar が続かない)
  • /\(foo\)\@<=-bar => foo-bar の -bar にマッチ
  • /\(foo\)\@<!-bar => hoge-bar の -bar にマッチ (foo が先にこない)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment