LoginSignup
12
12

More than 5 years have passed since last update.

ディレクトリ間のファイル比較による、ファイル削除・上書きコマンド

Last updated at Posted at 2014-09-19

本コマンドの使いどころ

ディレクトリA・Bがあるとする。

どんなファイル? アクション
Aにはない Bにはある Bのファイルを消す
Aにもある Bにもある Bのファイルを上書き

コマンド

ファイル削除

ディレクトリAにはないがディレクトリBにあるファイルを、ディレクトリBから削除。

diff -qru /path/to/directoryB/ /path/to/directoryA/ | egrep "\/path\/to\/directoryB\/.+だけに発見" | gawk '{ print gensub(/\/path\/to\/directoryB\/(.+)だけに発見: (.+)/, "\\1/\\2", "")}' | xargs -i rm -vr /path/to/directoryB/{}

ファイル上書き

ディレクトリA・B双方に存在するファイルについて、
ディレクトリAのファイルでディレクトリBのファイルを上書き。

diff -qru /path/to/directoryB/ /path/to/directoryA/ | gawk '{ print gensub(/ファイル\/path\/to\/directoryB\/(.+)と.+/, "\\1", "")}' | xargs -i mv -v /path/to/directoryA/{} /path/to/directoryB/{}
12
12
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
12
12