9
4

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 5 years have passed since last update.

身近で使えそうなワンライン

Last updated at Posted at 2017-09-10

ファイル探索

カレントディレクトリからシンボリックリングを抽出。

ls -la | grep '^l'

逆に、パターン不一致のものを得るには、vオプションを使います。
シンボリックリンク以外の一覧を得るには、次のようにします。

ls -la | grep -vE '^l'

文字列探索

あるディレクトリ以下の全.mdファイルから、特定の文字列とその周辺の行を探索
@jiro4989 さんからコメントして頂いたワンラインを、各ファイルの境界がわかるように少し加工してみました。
(表示されるパスが長いので、ファイル名のみ表示するように変更しました。)

find target/dir | grep -E ".md$" | xargs -J % grep "tips" % -3 | sed 's/.*\///g' | sed 's/\(\.md\)[-:]/\1  /g' | less -N

文字列置換

以下は、カレントディレクトリ内の全texファイルの句読点をカンマとピリオドに置換します。

for file in *.tex; do sed -ie 's/\。/./g' $file; sed -ie 's/、/,/g' $file; done

逆の置換を行う際は、小数点が句点に置換されないように注意する必要があります。

不要ファイル削除

以下は.texファイルと生成物のpdf以外をすべて削除します。
ディレクトリは対象外にしてあります。

find . -type f | grep -vE 'tex|pdf' | xargs rm

.DS_Storeを再帰的に削除

find . -name \.DS_Store | xargs rm

rbenvでrubyの最新バージョンをインストール

rbenv install -l | grep -v - | tail -1 | xargs rbenv install

pythonのモジュールを一括アップデート

pip freeze | cut -d = -f 1 | xargs pip install -U

brewでパッケージ一括アップデート

brew list | xargs brew upgrade

htmlタグの除去

cat target.html | sed -e 's/<[^>]*>//g'

gitで必要なブランチ以外を一掃する

masterブランチ以外のブランチを削除する例です。

git branch | grep -v master | xargs git branch -d

カレントディレクトリに存在する全ファイルを拡張子ごとに分類

joを使ってファイル一覧のJSONをつくる
JSON形式にするために、joというコマンドを使用しています。

for type in $(ls -F | grep -v / | grep -oE '\..*' | sort | uniq);do echo $type=$(ls *$type | awk '{print $0,"=",$0}' | jo);done | jo -p

Yahoo!天気予報をシェルで

Linuxコマンドで天気予報を取得&表示するワンライン(on Mac)

curl -s https://weather.yahoo.co.jp/weather/jp/40/8210/40133/8100001.html | tr '\n' ' ' | sed -e 's/<[^>]*>//g' | grep -oE ' - [0-9].*明日' | tr ' ' '\n' | grep -v '^$' | head -47 | tr '\n' '\t' | awk '{sub("時刻", "\n時刻 ");sub("天気", "\n天気 ");sub("気温(℃)", "\n気温(℃)");sub("湿度(%)", "\n湿度(%)");sub("降水量(mm/h)", "\n降水量(mm/h)");print}'

おわりに

macOSでのみ実行確認をしています。
他に便利そうなワンラインやリクエストがあったらコメントください。

9
4
2

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
9
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?