自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在Linux上識別同樣內(nèi)容的文件

系統(tǒng) Linux
有時文件副本相當(dāng)于對硬盤空間的巨大浪費,并會在你想要更新文件時造成困擾。以下是用來識別這些文件的六個命令。

[[267593]]

有時文件副本相當(dāng)于對硬盤空間的巨大浪費,并會在你想要更新文件時造成困擾。以下是用來識別這些文件的六個命令。

在最近的帖子中,我們看了如何識別并定位硬鏈接的文件(即,指向同一硬盤內(nèi)容并共享 inode)。在本文中,我們將查看能找到具有相同內(nèi)容,卻不相鏈接的文件的命令。

硬鏈接很有用是因為它們能夠使文件存放在文件系統(tǒng)內(nèi)的多個地方卻不會占用額外的硬盤空間。另一方面,有時文件副本相當(dāng)于對硬盤空間的巨大浪費,在你想要更新文件時也會有造成困擾之虞。在本文中,我們將看一下多種識別這些文件的方式。

用 diff 命令比較文件

可能比較兩個文件最簡單的方法是使用 diff 命令。輸出會顯示你文件的不同之處。<> 符號代表在當(dāng)參數(shù)傳過來的***個(<)或第二個(>)文件中是否有額外的文字行。在這個例子中,在 backup.html 中有額外的文字行。

  1. $ diff index.html backup.html
  2. 2438a2439,2441
  3. > <pre>
  4. > That's all there is to report.
  5. > </pre>

如果 diff 沒有輸出那代表兩個文件相同。

  1. $ diff home.html index.html
  2. $

diff 的***缺點是它一次只能比較兩個文件并且你必須指定用來比較的文件,這篇帖子中的一些命令可以為你找到多個重復(fù)文件。

使用校驗和

cksum(checksum) 命令計算文件的校驗和。校驗和是一種將文字內(nèi)容轉(zhuǎn)化成一個長數(shù)字(例如2819078353 228029)的數(shù)學(xué)簡化。雖然校驗和并不是完全獨有的,但是文件內(nèi)容不同校驗和卻相同的概率微乎其微。

  1. $ cksum *.html
  2. 2819078353 228029 backup.html
  3. 4073570409 227985 home.html
  4. 4073570409 227985 index.html

在上述示例中,你可以看到產(chǎn)生同樣校驗和的第二個和第三個文件是如何可以被默認(rèn)為相同的。

使用 find 命令

雖然 find 命令并沒有尋找重復(fù)文件的選項,它依然可以被用來通過名字或類型尋找文件并運行 cksum 命令。例如:

  1. $ find . -name "*.html" -exec cksum {} \;
  2. 4073570409 227985 ./home.html
  3. 2819078353 228029 ./backup.html
  4. 4073570409 227985 ./index.html

使用 fslint 命令

fslint 命令可以被特地用來尋找重復(fù)文件。注意我們給了它一個起始位置。如果它需要遍歷相當(dāng)多的文件,這就需要花點時間來完成。注意它是如何列出重復(fù)文件并尋找其它問題的,比如空目錄和壞 ID。

  1. $ fslint .
  2. -----------------------------------file name lint
  3. -------------------------------Invalid utf8 names
  4. -----------------------------------file case lint
  5. ----------------------------------DUPlicate files <==
  6. home.html
  7. index.html
  8. -----------------------------------Dangling links
  9. --------------------redundant characters in links
  10. ------------------------------------suspect links
  11. --------------------------------Empty Directories
  12. ./.gnupg
  13. ----------------------------------Temporary Files
  14. ----------------------duplicate/conflicting Names
  15. ------------------------------------------Bad ids
  16. -------------------------Non Stripped executables

你可能需要在你的系統(tǒng)上安裝 fslint。你可能也需要將它加入你的命令搜索路徑:

  1. $ export PATH=$PATH:/usr/share/fslint/fslint

使用 rdfind 命令

rdfind 命令也會尋找重復(fù)(相同內(nèi)容的)文件。它的名字意即“重復(fù)數(shù)據(jù)搜尋”,并且它能夠基于文件日期判斷哪個文件是原件——這在你選擇刪除副本時很有用因為它會移除較新的文件。

  1. $ rdfind ~
  2. Now scanning "/home/shark", found 12 files.
  3. Now have 12 files in total.
  4. Removed 1 files due to nonunique device and inode.
  5. Total size is 699498 bytes or 683 KiB
  6. Removed 9 files due to unique sizes from list.2 files left.
  7. Now eliminating candidates based on first bytes:removed 0 files from list.2 files left.
  8. Now eliminating candidates based on last bytes:removed 0 files from list.2 files left.
  9. Now eliminating candidates based on sha1 checksum:removed 0 files from list.2 files left.
  10. It seems like you have 2 files that are not unique
  11. Totally, 223 KiB can be reduced.
  12. Now making results file results.txt

你可以在 dryrun 模式中運行這個命令 (換句話說,僅僅匯報可能會另外被做出的改動)。

  1. $ rdfind -dryrun true ~
  2. (DRYRUN MODE) Now scanning "/home/shark", found 12 files.
  3. (DRYRUN MODE) Now have 12 files in total.
  4. (DRYRUN MODE) Removed 1 files due to nonunique device and inode.
  5. (DRYRUN MODE) Total size is 699352 bytes or 683 KiB
  6. Removed 9 files due to unique sizes from list.2 files left.
  7. (DRYRUN MODE) Now eliminating candidates based on first bytes:removed 0 files from list.2 files left.
  8. (DRYRUN MODE) Now eliminating candidates based on last bytes:removed 0 files from list.2 files left.
  9. (DRYRUN MODE) Now eliminating candidates based on sha1 checksum:removed 0 files from list.2 files left.
  10. (DRYRUN MODE) It seems like you have 2 files that are not unique
  11. (DRYRUN MODE) Totally, 223 KiB can be reduced.
  12. (DRYRUN MODE) Now making results file results.txt

rdfind 命令同樣提供了類似忽略空文檔(-ignoreempty)和跟蹤符號鏈接(-followsymlinks)的功能。查看 man 頁面獲取解釋。

  1. -ignoreempty ignore empty files
  2. -minsize ignore files smaller than speficied size
  3. -followsymlinks follow symbolic links
  4. -removeidentinode remove files referring to identical inode
  5. -checksum identify checksum type to be used
  6. -deterministic determiness how to sort files
  7. -makesymlinks turn duplicate files into symbolic links
  8. -makehardlinks replace duplicate files with hard links
  9. -makeresultsfile create a results file in the current directory
  10. -outputname provide name for results file
  11. -deleteduplicates delete/unlink duplicate files
  12. -sleep set sleep time between reading files (milliseconds)
  13. -n, -dryrun display what would have been done, but don't do it

注意 rdfind 命令提供了 -deleteduplicates true 的設(shè)置選項以刪除副本。希望這個命令語法上的小問題不會惹惱你。;-)

  1. $ rdfind -deleteduplicates true .
  2. ...
  3. Deleted 1 files. <==

你將可能需要在你的系統(tǒng)上安裝 rdfind 命令。試驗它以熟悉如何使用它可能是一個好主意。

使用 fdupes 命令

fdupes 命令同樣使得識別重復(fù)文件變得簡單。它同時提供了大量有用的選項——例如用來迭代的 -r。在這個例子中,它像這樣將重復(fù)文件分組到一起:

  1. $ fdupes ~
  2. /home/shs/UPGRADE
  3. /home/shs/mytwin
  4.  
  5. /home/shs/lp.txt
  6. /home/shs/lp.man
  7.  
  8. /home/shs/penguin.png
  9. /home/shs/penguin0.png
  10. /home/shs/hideme.png

這是使用迭代的一個例子,注意許多重復(fù)文件是重要的(用戶的 .bashrc.profile 文件)并且不應(yīng)被刪除。

  1. # fdupes -r /home
  2. /home/shark/home.html
  3. /home/shark/index.html
  4.  
  5. /home/dory/.bashrc
  6. /home/eel/.bashrc
  7.  
  8. /home/nemo/.profile
  9. /home/dory/.profile
  10. /home/shark/.profile
  11.  
  12. /home/nemo/tryme
  13. /home/shs/tryme
  14.  
  15. /home/shs/arrow.png
  16. /home/shs/PNGs/arrow.png
  17.  
  18. /home/shs/11/files_11.zip
  19. /home/shs/ERIC/file_11.zip
  20.  
  21. /home/shs/penguin0.jpg
  22. /home/shs/PNGs/penguin.jpg
  23. /home/shs/PNGs/penguin0.jpg
  24.  
  25. /home/shs/Sandra_rotated.png
  26. /home/shs/PNGs/Sandra_rotated.png

fdupe 命令的許多選項列如下。使用 fdupes -h 命令或者閱讀 man 頁面獲取詳情。

  1. -r --recurse recurse
  2. -R --recurse: recurse through specified directories
  3. -s --symlinks follow symlinked directories
  4. -H --hardlinks treat hard links as duplicates
  5. -n --noempty ignore empty files
  6. -f --omitfirst omit the first file in each set of matches
  7. -A --nohidden ignore hidden files
  8. -1 --sameline list matches on a single line
  9. -S --size show size of duplicate files
  10. -m --summarize summarize duplicate files information
  11. -q --quiet hide progress indicator
  12. -d --delete prompt user for files to preserve
  13. -N --noprompt when used with --delete, preserve the first file in set
  14. -I --immediate delete duplicates as they are encountered
  15. -p --permissions don't soncider files with different owner/group or
  16. permission bits as duplicates
  17. -o --order=WORD order files according to specification
  18. -i --reverse reverse order while sorting
  19. -v --version display fdupes version
  20. -h --help displays help

fdupes 命令是另一個你可能需要安裝并使用一段時間才能熟悉其眾多選項的命令。

總結(jié)

Linux 系統(tǒng)提供能夠定位并(潛在地)能移除重復(fù)文件的一系列的好工具,以及能讓你指定搜索區(qū)域及當(dāng)對你所發(fā)現(xiàn)的重復(fù)文件時的處理方式的選項。

責(zé)任編輯:龐桂玉 來源: Linux中國
相關(guān)推薦

2019-04-25 13:30:14

Linux文件分身

2020-12-28 06:44:45

FedoraLinux RPM文件

2023-12-20 22:04:17

Linux二進(jìn)制文件

2015-03-30 11:34:19

LinuxFSlint

2019-02-25 15:36:52

Linux復(fù)制文件遠(yuǎn)程系統(tǒng)

2020-03-06 08:56:41

Linux運算符文本

2019-01-07 09:50:06

Linuxtarball命令

2019-11-26 16:58:51

Linuxpkgsrc

2019-11-26 09:20:47

LinuxJava

2023-01-17 07:40:59

LinuxAppImage應(yīng)用程序

2019-09-03 08:57:52

Linux命令軟件

2017-05-03 15:30:38

LinuxMeld比較文件夾

2014-12-03 08:53:59

eCryptFS加密文件

2019-09-16 11:40:49

Linux交換文件

2020-04-21 14:21:31

LinuxPython

2018-12-11 11:00:50

Linux字體命令

2021-10-02 10:10:47

LinuxBusyBox命令

2021-10-12 08:43:19

LinuxSambaWindows

2021-07-10 11:20:44

FreeDOS歸檔文件

2020-09-30 06:00:00

Linux誤刪除恢復(fù)文件
點贊
收藏

51CTO技術(shù)棧公眾號