LoginSignup
1
0

More than 1 year has passed since last update.

【zsh】ディレクトリ構造の差分をツリー形式で表示する

Posted at

やりたいこと

tree コマンドと colordiff を利用して、以下のようなディレクトリ構造の差分をツリー形式で表示すること:

 .
-├── app
-│   └── app.js
 ├── css
-│   ├── reset.css
 │   └── styles.css
 ├── img
 │   ├── image1.png
-│   ├── image2.png
+│   ├── image3.png
 │   └── image4.png
 └── index.html

導入方法

macOS で Homebrew を利用している場合は、以下のコマンドで tree コマンドと colordiff コマンドをインストールします:

$ brew install tree colordiff

.zshrc に以下の関数を追加します:

.zshrc
function treediff () {
  local sed_command=$(echo -ne 's/\u2514/\u251c/g')
  colordiff <(cd $1; tree . | sed -e $sed_command) <(cd $2; tree . | sed -e $sed_command) -U10000
}
sed コマンドの処理内容 tree コマンドが出力する罫線 (\u2514) を (\u251c) に置き換えることで、罫線が差分とならないようにしています。

シェルを再起動します:

$ exec $SHELL -l

利用方法

treediff directory1/ directory2/ のように比較したい 2 つのディレクトリのパスを指定してコマンドを実行すると、以下のようにディレクトリ構造の差分がツリー形式で出力されます:

--- /dev/fd/11  2022-11-21 10:00:00.000000000 +0900
+++ /dev/fd/12  2022-11-21 10:00:00.000000000 +0900
@@ -1,13 +1,10 @@
 .
-├── app
-│   ├── app.js
 ├── css
-│   ├── reset.css
 │   ├── styles.css
 ├── img
 │   ├── image1.png
-│   ├── image2.png
+│   ├── image3.png
 │   ├── image4.png
 ├── index.html
 
-3 directories, 7 files
+2 directories, 5 files

参考にしたページ

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