1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[覚書] linux便利コマンド

Last updated at Posted at 2019-09-12
  • 検索に使えるコマンド
    (sudo) grep "文字列" ディレクトリパス | tail

  • 複数あるファイルの中から特定の文字列を検索したい。ファイル名だけ取り出す
    find ./ -type f -print | xargs grep 'hoge'
    grep [検索したい文字列] -rl [検索対象フォルダのパス]

  • さらにその文字列を置換する
    grep '置換対象文字列' -rl ./ | xargs sed -i.bak -e 's/置換対象文字列/置換文字列/g'
    ※-i でバックアップを取る。iの後ろに拡張子をつけてあげられる。

  • サブディレクトリまでgrep
    grep -r ディレクトリパス/*

  • 探したいファイルがいるの
    find ./ -name ファイル名

  • 探したいファイル。部分一致でもいいの
    find ./ | grep 名前

  • ファイル数知りたい
    find ./ -type f | wc -l

  • ディレクトリ内のファイル数をカウント
    ls -U1 | wc -l

  • ゴミは表示しない
    2>/dev/null

  • wget
    wget -q -O 設置するディレクトリパス http://取得元URL

  • 整形ファイルいい感じにチェック
    cat ファイル名 | awk -F"\t" '{print $37}' | sort | uniq

  • MySQL系の確認
    (rootUser) rpm -qa | grep mysql

  • インストールされているかどうか確認
    yum list installed 2>/dev/null | grep 検索対象名

  • インストール
    sudo yum install -y インストール対象

  • ファイル内文字列検索
    grep '文字列' ディレクトリパス

  • プロセス確認
    ps aux | grep 検索対象名 | grep -v grep

  • プロセスkill
    sudo kill -9 プロセスID

  • ファイルに共通行があれば抜き出す
    grep -x -f

  • 詳細に出すdiff
    diff -c

  • 行番号指定してcat
    cat -n ディレクトリパス | head -n 39 | tail -n 6

  • 文字エンコードえくすぽーとする。
    export LANG=ja_JP.UTF-8

  • 「2017/05/11」等の日付を抽出
    "^[0-9]\{4\}/[0-9]\{2\}/[0-9]\{2\}"

  • ファイルをsortしてからdiffをとる
    diff <(sort a.txt) <(sort b.txt)

  • 確認しながら置換 (vim)
    %s/hoge/fuga/gc

  • どこのサーバーが繋がっているか確認
    netstat -tan | grep "IPアドレス" | awk '{print $5}' | awk -F':' '{print $1}' | sort | uniq

  • エラーログ調査に役立ちそう。
    egrep "^ERROR" ディレクトリパス | grep "文字列"

  • ファイル比較
    md5sum ファイルパス ファイルパス

  • 文字検出
    egrep -n "ERROR" ディレクトリパス

  • ファイル内文字列置換をfor文で
    for ((i=105; i<116; i++)); do echo ./ファイル名 | xargs sed -i.bak -e 's/置換対象文字列/置換後文字列-'$i'/g'; done
    ※'$i'で回す分の数字を反映

  • ファイルコピー。名前連番で変えつつ
    for ((i=104; i<=115; i++)); do cp コピー元ファイル名 コピー後ファイル名-$i; done
    ※"$i"とダブルクォーテーションで囲むこと

  • ファイル名一括置換
    rename 置換対象文字列 置換後文字列 ファイルパス
    ※ワイルドカード使用可
    ex) rename hoge fuga ./*.txt => 拡張子「.txt」のファイル名の「hoge」部分を「fuga」に変える

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?