Linux中兩個目錄的區別[關閉]

2020-08-08 09:08:00

本文翻譯自:Difference between two directories in Linux [closed]

I'm trying to find the files existing in one directory but not in the other, I tried to use this command: 我正在嘗試查詢存在於一個目錄中但不存在於另一個目錄中的檔案,我嘗試使用此命令:

diff -q dir1 dir2

The problem with the above command that it finds both the files in dir1 but not in dir2 as well as the files in dir2 but not in dir1 , 上面命令的問題是,它在dir1找到了檔案,但在dir2找不到檔案,在dir2找不到檔案但在dir1找不到,

I am trying to find the files in dir1 but not in dir2 only. 我試圖在dir1找到檔案但不在dir2找到。

Here's a small sample of what my data looks like 這是我的數據的一個小樣本

dir1    dir2    dir3
1.txt   1.txt   1.txt
2.txt   3.txt   3.txt
5.txt   4.txt   5.txt
6.txt   7.txt   8.txt

Another question on my mind is how can I find the files in dir1 but not in dir2 or dir3 in a single command? 我想到的另一個問題是如何在單個命令中找到dir1的檔案而不是dir2dir3中的檔案?


#1樓

參考:https://stackoom.com/question/18riQ/Linux中兩個目錄的區別-關閉


#2樓

diff -r dir1 dir2 | grep dir1 | awk '{print $4}' > difference1.txt

Explanation: 說明:

  • diff -r dir1 dir2 shows which files are only in dir1 and those only in dir2 and also the changes of the files present in both directories if any. diff -r dir1 dir2顯示哪些檔案僅在dir1中,哪些檔案僅在dir2中,以及兩個目錄中存在的檔案的更改(如果有)。

  • diff -r dir1 dir2 | grep dir1 diff -r dir1 dir2 | grep dir1 shows which files are only in dir1 diff -r dir1 dir2 | grep dir1顯示哪些檔案僅在dir1中

  • awk to print only filename. awk只列印檔名。


#3樓

comm -23 <(ls dir1 |sort) <(ls dir2|sort)

This command will give you files those are in dir1 and not in dir2. 此命令將爲您提供dir1中的檔案,而不是 dir2中的檔案。

About <( ) sign, you can google it as 'process substitution'. 關於<( )符號,您可以將其視爲「流程替換」。


#4樓

vim's DirDiff plugin is another very useful tool for comparing directories. vim的DirDiff外掛是另一個用於比較目錄的非常有用的工具。

vim -c "DirDiff dir1 dir2"

It not only lists which files are different between the directories, but also allows you to inspect/modify with vimdiff the files that are different. 它不僅列出了目錄中哪些檔案不同,而且還允許您使用vimdiff檢查/修改不同的檔案。


#5樓

This should do the job: 這應該做的工作:

diff -rq dir1 dir2

Options explained (via diff(1) man page ): 解釋選項(通過diff(1) 手冊頁 ):

  • -r - Recursively compare any subdirectories found. -r - 遞回比較找到的所有子目錄。
  • -q - Output only whether files differ. -q - 僅輸出檔案是否不同。

#6樓

This is a bit late but may help someone. 這有點晚了但可能對某人有幫助。 Not sure if diff or rsync spit out just filenames in a bare format like this. 不確定diff或rsync是否只是以像這樣的裸格式吐出檔名。 Thanks to plhn for giving that nice solution which I expanded upon below. 感謝plhn提供了我在下面 下麪展開的優秀解決方案。

If you want just the filenames so it's easy to just copy the files you need in a clean format, you can use the find command. 如果只需要檔名,那麼只需以乾淨的格式複製所需的檔案就很容易,可以使用find命令。

comm -23 <(find dir1 | sed 's/dir1/\//'| sort) <(find dir2 | sed 's/dir2/\//'| sort) | sed 's/^\//dir1/'

This assumes that both dir1 and dir2 are in the same parent folder. 這假設dir1和dir2都在同一個父資料夾中。 sed just removes the parent folder so you can compare apples with apples. sed只刪除父資料夾,以便您可以將蘋果與蘋果進行比較。 The last sed just puts the dir1 name back. 最後一個sed只是將dir1名稱放回去。

If you just want files: 如果你只想要檔案:

comm -23 <(find dir1 -type f | sed 's/dir1/\//'| sort) <(find dir2 -type f | sed 's/dir2/\//'| sort) | sed 's/^\//dir1/'

Similarly for directories: 同樣對於目錄:

comm -23 <(find dir1 -type d | sed 's/dir1/\//'| sort) <(find dir2 -type d | sed 's/dir2/\//'| sort) | sed 's/^\//dir1/'