如何找到系統(tǒng)里的重復(fù)文件,快速釋放磁盤空間?
不管是 Windows 電腦還是 Linux 電腦,在使用的過程中,或多或少都會留下很多重復(fù)的文件。這些文件不僅會占用我們的磁盤,還會拖累我們的系統(tǒng),所以,很有必要干掉這些重復(fù)的文件。
本文將介紹 6 種方法找到系統(tǒng)里的重復(fù)文件,讓你快速釋放硬盤空間!
1. 使用 diff 命令比較文件
在我們平常操作當(dāng)中,比較兩個文件的差異最簡單的方法可能就是使用 diff 命令。diff 命令的輸出將使用 < 和 > 符號顯示兩個文件之間的差異,利用這個特性我們可以找到相同的文件。
當(dāng)兩個文件有差異時,diff 命令將輸出差異點(diǎn):
- $ diff index.html backup.html
- 2438a2439,2441
- > <pre>
- > That's all there is to report.
- > </pre>
如果你的 diff 命令沒有輸出,則表示兩個文件相同:
- $ diff home.html index.html
- $
但是, diff 命令的缺點(diǎn)是它一次只能比較兩個文件,如果我們要比較多個文件,這樣兩個兩個比較效率肯定非常低下。
2. 使用校驗(yàn)和
校驗(yàn)和命令 cksum 會根據(jù)一定的算法將文件的內(nèi)容計(jì)算成一個很長的數(shù)字(如2819078353 228029)。雖然算出的結(jié)果不是絕對唯一,但是內(nèi)容不相同的文件導(dǎo)致校驗(yàn)和相同的可能性跟中國男足進(jìn)世界杯差不多。
- $ cksum *.html
- 2819078353 228029 backup.html
- 4073570409 227985 home.html
- 4073570409 227985 index.html
在我們上面的操作中,我們可以看到第二個和第三個文件校驗(yàn)和是相同的,所以我們可以認(rèn)為這兩個文件是一樣的。
3. 使用 find 命令
雖然 find 命令沒有查找重復(fù)文件的選項(xiàng),但是它卻可用于按名稱或類型搜索文件并運(yùn)行cksum 命令。具體操作如下。
- $ find . -name "*.html" -exec cksum {} \;
- 4073570409 227985 ./home.html
- 2819078353 228029 ./backup.html
- 4073570409 227985 ./index.html
4. 使用 fslint 命令
fslint 命令可以用來專門查找重復(fù)文件。但是這里有個注意事項(xiàng),就是我們需要給它一個起始位置。如果我們需要運(yùn)行大量文件,該命令可能需要相當(dāng)長的時間才能完成查找。
- $ fslint .
- -----------------------------------file name lint
- -------------------------------Invalid utf8 names
- -----------------------------------file case lint
- ----------------------------------DUPlicate files <==
- home.html
- index.html
- -----------------------------------Dangling links
- --------------------redundant characters in links
- ------------------------------------suspect links
- --------------------------------Empty Directories
- ./.gnupg
- ----------------------------------Temporary Files
- ----------------------duplicate/conflicting Names
- ------------------------------------------Bad ids
- -------------------------Non Stripped executables
Tips:我們必須在系統(tǒng)上安裝 fslint ,還需要將它添加到搜索路徑中:
- $ export PATH=$PATH:/usr/share/fslint/fslint
5. 使用 rdfind 命令
rdfind 命令還將尋找重復(fù)的(相同內(nèi)容的)文件。被稱為“冗余數(shù)據(jù)查找”,該命令可以根據(jù)文件日期確定哪些文件是原始文件,這對我們選擇刪除重復(fù)項(xiàng)很有幫助,因?yàn)樗鼤h除較新的文件。
- $ rdfind ~
- Now scanning "/home/alvin", found 12 files.
- Now have 12 files in total.
- Removed 1 files due to nonunique device and inode.
- Total size is 699498 bytes or 683 KiB
- Removed 9 files due to unique sizes from list.2 files left.
- Now eliminating candidates based on first bytes:removed 0 files from list.2 files left.
- Now eliminating candidates based on last bytes:removed 0 files from list.2 files left.
- Now eliminating candidates based on sha1 checksum:removed 0 files from list.2 files left.
- It seems like you have 2 files that are not unique
- Totally, 223 KiB can be reduced.
- Now making results file results.txt
我們還可以在 dryrun 中運(yùn)行。
- $ rdfind -dryrun true ~
- (DRYRUN MODE) Now scanning "/home/alvin", found 12 files.
- (DRYRUN MODE) Now have 12 files in total.
- (DRYRUN MODE) Removed 1 files due to nonunique device and inode.
- (DRYRUN MODE) Total size is 699352 bytes or 683 KiB
- Removed 9 files due to unique sizes from list.2 files left.
- (DRYRUN MODE) Now eliminating candidates based on first bytes:removed 0 files from list.2 files left.
- (DRYRUN MODE) Now eliminating candidates based on last bytes:removed 0 files from list.2 files left.
- (DRYRUN MODE) Now eliminating candidates based on sha1 checksum:removed 0 files from list.2 files left.
- (DRYRUN MODE) It seems like you have 2 files that are not unique
- (DRYRUN MODE) Totally, 223 KiB can be reduced.
- (DRYRUN MODE) Now making results file results.txt
rdfind 命令還提供一些忽略空文件(-ignoreempty)和跟隨符號鏈接(-followsymlinks)之類的選項(xiàng)。下面詳細(xì)解釋它的常用選項(xiàng)。
這里需要我們注意一下,rdfind命令提供了使用 -deleteduplicates true 設(shè)置刪除重復(fù)文件的選項(xiàng)。顧名思義,使用這個選項(xiàng)它將自動刪重復(fù)的文件。
- $ rdfind -deleteduplicates true .
- ...
- Deleted 1 files. <==
當(dāng)然,前提是我們也必須在系統(tǒng)上安裝 rdfind 命令。
6. 使用 fdupes 命令
fdupes 命令也可以很容易地識別重復(fù)文件,并提供了大量有用的選項(xiàng)。在最簡單的操作中,它會把重復(fù)文件放在一起,如下所示:
- $ fdupes ~
- /home/alvin/UPGRADE
- /home/alvin/mytwin
- /home/alvin/lp.txt
- /home/alvin/lp.man
- /home/alvin/penguin.png
- /home/alvin/penguin0.png
- /home/alvin/hideme.png
-r 選項(xiàng)代表遞歸,表示它將在各個目錄下面使用遞歸的方式來查找重復(fù)文件。但是,Linux 下有許多重復(fù)文件是很重要的(比如用戶的 .bashrc 和 .profile 文件),如果被刪除將導(dǎo)致系統(tǒng)異常。
- # fdupes -r /home
- /home/shark/home.html
- /home/shark/index.html
- /home/dory/.bashrc
- /home/eel/.bashrc
- /home/nemo/.profile
- /home/dory/.profile
- /home/shark/.profile
- /home/nemo/tryme
- /home/shs/tryme
- /home/shs/arrow.png
- /home/shs/PNGs/arrow.png
- /home/shs/11/files_11.zip
- /home/shs/ERIC/file_11.zip
- /home/shs/penguin0.jpg
- /home/shs/PNGs/penguin.jpg
- /home/shs/PNGs/penguin0.jpg
- /home/shs/Sandra_rotated.png
- /home/shs/PNGs/Sandra_rotated.png
fdupes 命令的常用選項(xiàng)如下表所示:
小結(jié)
Linux 系統(tǒng)為我們提供了很多用于定位和刪除重復(fù)文件的工具,使用這些工具將快速找到磁盤里的重復(fù)文件并刪除它們。希望本次分享能給大家?guī)韼椭鷡