硬盤女神沒地方放?一起來清一清電腦吧

不管是 Windows 電腦還是 Linux 電腦,在使用的過程中,或多或少都會留下很多重複的文件。這些文件不僅會佔用我們的磁盤,還會拖累我們的系統,所以,很有必要幹掉這些重複的文件。

本文將介紹 6 種方法找到系統裡的重複文件,讓你快速釋放硬盤空間!

1. 使用 diff 命令比較文件

在我們平常操作當中,比較兩個文件的差異最簡單的方法可能就是使用 diff 命令。diff 命令的輸出將使用 < 和 > 符號顯示兩個文件之間的差異,利用這個特性我們可以找到相同的文件。

當兩個文件有差異時,diff 命令將輸出差異點:

<code>$ diff index.html backup.html
2438a2439,2441
>

> That's all there is to report.
>
/<code>

如果你的 diff 命令沒有輸出,則表示兩個文件相同:

<code>$ diff home.html index.html
$/<code>

但是, diff 命令的缺點是它一次只能比較兩個文件,如果我們要比較多個文件,這樣兩個兩個比較效率肯定非常低下。

2. 使用校驗和

校驗和命令 cksum 會根據一定的算法將文件的內容計算成一個很長的數字(如2819078353 228029)。雖然算出的結果不是絕對唯一,但是內容不相同的文件導致校驗和相同的可能性跟中國男足進世界盃差不多。

<code>$ cksum *.html
2819078353 228029 backup.html
4073570409 227985 home.html
4073570409 227985 index.html/<code>

在我們上面的操作中,我們可以看到第二個和第三個文件校驗和是相同的,所以我們可以認為這兩個文件是一樣的。

3. 使用 find 命令

雖然 find 命令沒有查找重複文件的選項,但是它卻可用於按名稱或類型搜索文件並運行cksum 命令。具體操作如下。

<code>$ find . -name "*.html" -exec cksum {} \\;
4073570409 227985 ./home.html
2819078353 228029 ./backup.html
4073570409 227985 ./index.html/<code>

4. 使用 fslint 命令

fslint 命令可以用來專門查找重複文件。但是這裡有個注意事項,就是我們需要給它一個起始位置。如果我們需要運行大量文件,該命令可能需要相當長的時間才能完成查找。

<code>$ 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/<code>

Tips:我們必須在系統上安裝 fslint ,還需要將它添加到搜索路徑中:

<code>$ export PATH=$PATH:/usr/share/fslint/fslint/<code>

5. 使用 rdfind 命令

rdfind 命令還將尋找重複的(相同內容的)文件。被稱為“冗餘數據查找”,該命令可以根據文件日期確定哪些文件是原始文件,這對我們選擇刪除重複項很有幫助,因為它會刪除較新的文件。

<code>$ 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/<code>

我們還可以在 dryrun 中運行。

<code>$ 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/<code>

rdfind 命令還提供一些忽略空文件(-ignoreempty)和跟隨符號鏈接(-followsymlinks)之類的選項。下面詳細解釋它的常用選項。

選項 意義 -ignoreempty 忽略空文件 -minsize 忽略小於特定大小的文件 -followsymlink 遵循符號鏈接 -removeidentinode 刪除引用相同inode的文件 -checksum 標識要使用的校驗和類型 -deterministic 決定如何排序文件 -makesymlinks 將重複文件轉換為符號鏈接 -makehardlinks 用硬鏈接替換重複文件 -makeresultsfile 在當前目錄中創建結果文件 -outputname 提供結果文件的名稱 -deleteduplicates 刪除/取消鏈接重複文件 -sleep 設置讀取文件之間的休眠時間(毫秒) -n,-dryrun 顯示本應執行的操作,但不要執行

這裡需要我們注意一下,rdfind命令提供了使用 -deleteduplicates true 設置刪除重複文件的選項。顧名思義,使用這個選項它將自動刪重複的文件。

<code>$ rdfind -deleteduplicates true .
...
Deleted 1 files. <==/<code>

當然,前提是我們也必須在系統上安裝 rdfind 命令。

6. 使用 fdupes 命令

fdupes 命令也可以很容易地識別重複文件,並提供了大量有用的選項。在最簡單的操作中,它會把重複文件放在一起,如下所示:

<code>$ 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/<code>

-r 選項代表遞歸,表示它將在各個目錄下面使用遞歸的方式來查找重複文件。但是,Linux 下有許多重複文件是很重要的(比如用戶的 .bashrc 和 .profile 文件),如果被刪除將導致系統異常。

<code># 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/<code>

fdupes 命令的常用選項如下表所示:

選項 意義 -r --recurse 遞歸 -R --recurse 遞歸指定的目錄 -s --symlinks-H --hardlinks 遵循符號鏈接目錄 -H --hardlinks 將硬鏈接視為重複鏈接 -n --noempty 忽略空文件 -f --omitfirst 省略每組匹配中的第一個文件 -A --nohidden 忽略隱藏文件 -1 --sameline 相同列表匹配單行 -S --size 顯示重複文件的大小 -m --summarize 彙總重複文件信息 -q --quiet 進度指示器 -d --delete 提示用戶保存文件 -N --noprompt 與--delete一起使用時無效,保留集合中的第一個文件 -I --immediate 在遇到它們時刪除重複項 -p --permissions 權限不會將具有不同所有者/組或權限位的SONCIDER文件作為重複項 -o --order=WORD 根據規範的WORD訂單文件 -i --reverse 排序時反向逆序 -v --version 顯示fdupes版本 -h --help 顯示幫助

小結

Linux 系統為我們提供了很多用於定位和刪除重複文件的工具,使用這些工具將快速找到磁盤裡的重複文件並刪除它們。希望本次分享能給大家帶來幫助~


分享到:


相關文章: