0
0

More than 1 year has passed since last update.

拡張子を複数指定してdiffをとる

Last updated at Posted at 2023-05-17

コマンド

$ ls
dir_1/  dir_2/

のディレクトリ対して

find dir_1 dir_2 -type f -name "*.html" -or -name "*.php" \
| sed -e "s/^dir_1//g" | sed -e "s/^dir_2//g" | sort | uniq \
| xargs -IXXXX diff  --strip-trailing-cr dir_1XXXX dir_2XXXX -u

解説

拡張子で検索

$ find dir_1 dir_2 -type f -name "*.html" -or -name "*.php"
dir_1/index.html
dir_1/index.php
dir_1/sub_dir/index.html
dir_1/sub_dir/index.php
dir_2/index.html
dir_2/index.php
dir_2/sub_dir/index.html
dir_2/sub_dir/index.php

sedで先頭のディレクトリ名を消す

$ find dir_1 dir_2 -type f -name "*.html" -or -name "*.php" \
| sed -e "s/^dir_1//g" | sed -e "s/^dir_2//g"
/index.html
/index.php
/sub_dir/index.html
/sub_dir/index.php
/index.html
/index.php
/sub_dir/index.html
/sub_dir/index.php

重複を排除

$ find dir_1 dir_2 -type f -name "*.html" -or -name "*.php" \
| sed -e "s/^dir_1//g" | sed -e "s/^dir_2//g" | sort | uniq
/index.html
/index.php
/sub_dir/index.html
/sub_dir/index.php

diffをとる

find dir_1 dir_2 -type f -name "*.html" -or -name "*.php" \
| sed -e "s/^dir_1//g" | sed -e "s/^dir_2//g" | sort | uniq \
| xargs -IXXXX diff  --strip-trailing-cr dir_1XXXX dir_2XXXX -u
  • -u : unified形式で差分を表示
  • --strip-trailing-cr : 改行コードを無視※

※正確ではないらしいが割愛

実行結果

--- dir_1/index.html    2023-05-17 12:38:42.451136400 +0900
+++ dir_2/index.html    2023-05-17 12:39:46.760155700 +0900
@@ -7,8 +7,8 @@
     <title>Document</title>
 </head>
 <body>
-    <p>変更前</p>
-    <p>変更前</p>
-    <p>変更前</p>
+    <p>変更後</p>
+    <p>変更後</p>
+    <p>変更後</p>
 </body>
 </html>
\ No newline at end of file
--- dir_1/index.php     2023-05-17 12:38:42.451136400 +0900
+++ dir_2/index.php     2023-05-17 12:39:46.760155700 +0900
@@ -7,8 +7,8 @@
     <title>Document</title>
 </head>
 <body>
-    <p>変更前</p>
-    <p>変更前</p>
-    <p>変更前</p>
+    <p>変更後</p>
+    <p>変更後</p>
+    <p>変更後</p>
 </body>
 </html>
\ No newline at end of file
--- dir_1/sub_dir/index.html    2023-05-17 12:38:42.451136400 +0900
+++ dir_2/sub_dir/index.html    2023-05-17 12:39:46.760155700 +0900
@@ -7,8 +7,8 @@
     <title>Document</title>
 </head>
 <body>
-    <p>変更前</p>
-    <p>変更前</p>
-    <p>変更前</p>
+    <p>変更後</p>
+    <p>変更後</p>
+    <p>変更後</p>
 </body>
 </html>
\ No newline at end of file
--- dir_1/sub_dir/index.php     2023-05-17 12:38:42.451136400 +0900
+++ dir_2/sub_dir/index.php     2023-05-17 12:39:46.763954700 +0900
@@ -7,8 +7,8 @@
     <title>Document</title>
 </head>
 <body>
-    <p>変更前</p>
-    <p>変更前</p>
-    <p>変更前</p>
+    <p>変更後</p>
+    <p>変更後</p>
+    <p>変更後</p>
 </body>
 </html>
\ No newline at end of file

参考記事

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