Linux diff ツール: 変更されたファイルリストの生成

Linux diff ツール: 変更されたファイルリストの生成

Linuxコマンドラインツールを使用して変更されたファイルのリストをプログラムで作成する方法は?特定のファイル(増分、パッチ)の違いには興味がありません。以前の製品バージョンと比較して、新しいファイルまたは変更されたファイルのリストを取得したいと思います。これにより、新しい製品アップデートをリリースできます。

更新:diff -qr非常に便利な出力を生成しません。出力diff -qrも処理する必要があります。もっと良い方法がありますか?

答え1

あなたはそれを使用することができます違いツール:-qおよび-rオプションリファレンス

-q  --brief
Output only whether files differ.

-r  --recursive
Recursively compare any subdirectories found.

例:

diff -qr dir1 dir2

答え2

簡単な方法があります。 rsync-previewモードを使用してください。

rsync -aHSvn --delete old_dir/ new-dir/

コマンドに「削除」とマークされたファイルは「新しい」ファイルになります。移転される他のコンテンツにも一種の変更がありました。詳しくはrsync-man-pageをご覧ください。

答え3

このdiffutilsパッケージにはlsdiffツールが含まれています。次の出力を渡すだけですdiff -u

diff -u --other-diff-options path1 path2 | lsdiff

答え4

プログラムで新しいファイルを作成したり、ファイルのリストを変更したい場合は、私が考えることができる最善の解決策は次のことです。同期タイプユニーク:

(rsync -rcn --out-format="%n" old/ new/ && rsync -rcn --out-format="%n" new/ old/) | sort | uniq

この例を挙げて説明します。 2つのdokuwikiバージョンを比較して、どのファイルが変更されたか、どのファイルが新しく作成されたかを確認したいと思います。

wgetを使用してtarをインポートしてディレクトリに抽出old/しますnew/

wget http://download.dokuwiki.org/src/dokuwiki/dokuwiki-2014-09-29d.tgz
wget http://download.dokuwiki.org/src/dokuwiki/dokuwiki-2014-09-29.tgz
mkdir old && tar xzf dokuwiki-2014-09-29.tgz -C old --strip-components=1
mkdir new && tar xzf dokuwiki-2014-09-29d.tgz -C new --strip-components=1

rsyncとdiffの比較に示すように、1つの方法でrsyncを実行すると、新しく作成されたファイルが失われる可能性があります。

rsync -rcn --out-format="%n" old/ new/

次の出力が生成されます。

VERSION
doku.php
conf/mime.conf
inc/auth.php
inc/lang/no/lang.php
lib/plugins/acl/remote.php
lib/plugins/authplain/auth.php
lib/plugins/usermanager/admin.php

一方向にのみrsyncを実行すると、新しく作成されたファイルが欠落し、逆に削除されたファイルが欠落します。 diffの出力を比較してください。

diff -qr old/ new/

次の出力が生成されます。

Files old/VERSION and new/VERSION differ
Files old/conf/mime.conf and new/conf/mime.conf differ
Only in new/data/pages: playground
Files old/doku.php and new/doku.php differ
Files old/inc/auth.php and new/inc/auth.php differ
Files old/inc/lang/no/lang.php and new/inc/lang/no/lang.php differ
Files old/lib/plugins/acl/remote.php and new/lib/plugins/acl/remote.php differ
Files old/lib/plugins/authplain/auth.php and new/lib/plugins/authplain/auth.php differ
Files old/lib/plugins/usermanager/admin.php and new/lib/plugins/usermanager/admin.php differ

2 つの方法で rsync を実行し、出力をソートして重複を削除すると、data/pages/playground/最初にディレクトリとファイルが欠落していることがわかります。data/pages/playground/playground.txt

(rsync -rcn --out-format="%n" old/ new/ && rsync -rcn --out-format="%n" new/ old/) | sort | uniq

次の出力が生成されます。

VERSION
conf/mime.conf
data/pages/playground/
data/pages/playground/playground.txt
doku.php
inc/auth.php
inc/lang/no/lang.php
lib/plugins/acl/remote.php
lib/plugins/authplain/auth.php
lib/plugins/usermanager/admin.php

rsync次のパラメータを使用して実行します。

  • -r「ディレクトリに再帰」、
  • -c同じサイズのファイルを比較し、「修正時間とサイズではなくチェックサムベースにスキップする」のみが可能です。
  • -n「変更なく試運転を行う」、
  • --out-format="%n"「指定された形式を使用した更新の出力」に進みます。ここで、 "%n"はファイル名にのみ使用されます。

双方向出力(ファイルリスト)をrsync結合してを使用してソートし、次にこのソートされたsortリストを使用してすべての重複項目を削除して圧縮します。uniq

関連情報