如何僅在第一層的兩個資料夾中找到相同的子資料夾?

如何僅在第一層的兩個資料夾中找到相同的子資料夾?

設想:

  • 2個主資料夾,可能有同名的子資料夾(我不知道哪些名稱可能重複,這就是我想要找到的)
  • 子資料夾有許多其他檔案和子資料夾,因此具有自動遞歸功能的工具並不是真正的選擇
  • 我只關心兩個主資料夾第一層的重複子資料夾名稱
  • 子資料夾的內容並不重要
  • 文件的內容並不重要

我嘗試使用meldGUI,但這需要無盡的時間才能完成這些結構。

我嘗試使用diff --brief --report-identical-files folder1 folder2,但基本上報告了所有內容,它甚至不包括資料夾,所以我什至不能| grep identical

我使用了錯誤的工具嗎?還是有什麼我沒有學到的技巧diff --help?還是我做錯了什麼?

謝謝

答案1

我會使用一個簡單的find

find "/path/to/main1" "/path/to/main2" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort | uniq -d

或使其以零終止以防止換行符號出現問題:

find "/path/to/main1" "/path/to/main2" -mindepth 1 -maxdepth 1  -type d -printf '%f\0' | sort -z | uniq -zd | xargs -0

答案2

使用zsh, 給定

% tree dir1 dir2
dir1
├── bar
└── foo
    └── baz
dir2
├── bar
│   └── baz
└── baz

6 directories, 0 files

然後

% a=( dir1/*(/ND:t) ) ; b=( dir2/*(/ND:t) )

建立數組t/兩個頂級目錄中的目錄的ails(基本名稱)dir1dir2(帶有D奧特格洛布和ulglob 選項已啟用)。

然後我們可以使用形式的擴充${name:*arrayname}來僅保留兩個數組中都存在的元素:

% print -rC1 ${a:*b}
bar

相關內容