2012-07-11 83 views
2

我在一個目錄中有很多文件A在bash中查找不存在於另一個目錄樹中的平面目錄中的所有文件

其中一些文件與子目錄B/B1B/B2B/B3B/B4 ... 注意某些文件的名稱中有空格的目錄樹存在。

例如:

目錄A

  • 有一個文件名爲A/red file.png
  • 還有另一種名爲A/blue file.png

    ,並在目錄樹B

  • 有一個文件名爲B/small/red file.png

    在這個例子中,我想要一個腳本告訴我文件blue file.png在目錄B中不存在。

我如何寫一個腳本,將列出所有A未在目錄樹B下找到的文件?

回答

5
# A 
# ├── blue file.png 
# └── red file.png 
# B 
# └── small 
#  └── red file.png 

$ comm -23 <(find A -type f -printf '%f\n' | sort | uniq) <(find B -type f -printf '%f\n' | sort | uniq) 
blue file.png 

如果您find缺乏-printf,你可以嘗試:

comm -23 <(find A -type f -exec basename {} \; | sort | uniq) <(find B -type f -exec basename {} \; | sort | uniq) 
+0

感謝。我的「find」似乎沒有-printf,所以我用-print代替。這應該沒有什麼區別,對吧? – 2012-07-11 21:02:23

+0

掛上。有些東西不起作用。我仍然在A和B中都存在結果文件。 – 2012-07-11 21:16:41

+0

好的。也許它是-printf。當我用-print代替時,我得到了整個文件名,包括目錄。如果-printf'%f'給了我文件的基名,我會接受這個答案,但是如何讓這個腳本適應一個沒有-printf的「find」? – 2012-07-11 21:23:07

相關問題