天天看點

fdupes:Linux 中查找并删除重複檔案的指令行工具

對于大多數計算機使用者而言,查找并替換重複的檔案是一個常見的需求。查找并移除重複檔案真是一項令人不勝其煩的工作,它耗時又耗力。但如果你的機器上跑着gnu/linux,那麼查找重複檔案會變得十分簡單,這多虧了<code>fdupes</code>工具。

fdupes:Linux 中查找并删除重複檔案的指令行工具

fdupes——在linux中查找并删除重複檔案

<a target="_blank"></a>

fdupes是linux下的一個工具,它由adrian lopez用c程式設計語言編寫并基于mit許可證發行,該應用程式可以在指定的目錄及子目錄中查找重複的檔案。fdupes通過對比檔案的md5簽名,以及逐位元組比較檔案來識别重複内容,fdupes有各種選項,可以實作對檔案的列出、删除、替換為檔案副本的硬連結等操作。

檔案對比以下列順序開始:

大小對比 &gt; 部分 md5 簽名對比 &gt; 完整 md5 簽名對比 &gt; 逐位元組對比

在基于debian的系統上,如ubuntu和linux mint,安裝最新版fdupes,用下面的指令手到擒來。

<code>$ sudo apt-get install fdupes</code>

<code># yum install fdupes</code>

<code># dnf install fdupes [在 fedora 22 及其以後]</code>

注意:自fedora 22之後,預設的包管理器yum被dnf取代了。

1、 作為示範的目的,讓我們來在某個目錄(比如 tecmint)下建立一些重複檔案,指令如下:

<code>$ mkdir /home/"$user"/desktop/tecmint &amp;&amp; cd /home/"$user"/desktop/tecmint &amp;&amp; for i in {1..15}; do echo "i love tecmint. tecmint is a very nice community of linux users." &gt; tecmint${i}.txt ; done</code>

<code>$ ls -l</code>

<code></code>

<code>total 60</code>

<code>-rw-r--r-- 1 tecmint tecmint 65 aug 8 11:22 tecmint10.txt</code>

<code>-rw-r--r-- 1 tecmint tecmint 65 aug 8 11:22 tecmint11.txt</code>

<code>-rw-r--r-- 1 tecmint tecmint 65 aug 8 11:22 tecmint12.txt</code>

<code>-rw-r--r-- 1 tecmint tecmint 65 aug 8 11:22 tecmint13.txt</code>

<code>-rw-r--r-- 1 tecmint tecmint 65 aug 8 11:22 tecmint14.txt</code>

<code>-rw-r--r-- 1 tecmint tecmint 65 aug 8 11:22 tecmint15.txt</code>

<code>-rw-r--r-- 1 tecmint tecmint 65 aug 8 11:22 tecmint1.txt</code>

<code>-rw-r--r-- 1 tecmint tecmint 65 aug 8 11:22 tecmint2.txt</code>

<code>-rw-r--r-- 1 tecmint tecmint 65 aug 8 11:22 tecmint3.txt</code>

<code>-rw-r--r-- 1 tecmint tecmint 65 aug 8 11:22 tecmint4.txt</code>

<code>-rw-r--r-- 1 tecmint tecmint 65 aug 8 11:22 tecmint5.txt</code>

<code>-rw-r--r-- 1 tecmint tecmint 65 aug 8 11:22 tecmint6.txt</code>

<code>-rw-r--r-- 1 tecmint tecmint 65 aug 8 11:22 tecmint7.txt</code>

<code>-rw-r--r-- 1 tecmint tecmint 65 aug 8 11:22 tecmint8.txt</code>

<code>-rw-r--r-- 1 tecmint tecmint 65 aug 8 11:22 tecmint9.txt</code>

上面的腳本建立了15個檔案,名稱分别為tecmint1.txt,tecmint2.txt……tecmint15.txt,并且每個檔案的資料相同,如

<code>"i love tecmint. tecmint is a very nice community of linux users."</code>

2、 現在在tecmint檔案夾内搜尋重複的檔案。

<code>$ fdupes /home/$user/desktop/tecmint</code>

<code>/home/tecmint/desktop/tecmint/tecmint13.txt</code>

<code>/home/tecmint/desktop/tecmint/tecmint8.txt</code>

<code>/home/tecmint/desktop/tecmint/tecmint11.txt</code>

<code>/home/tecmint/desktop/tecmint/tecmint3.txt</code>

<code>/home/tecmint/desktop/tecmint/tecmint4.txt</code>

<code>/home/tecmint/desktop/tecmint/tecmint6.txt</code>

<code>/home/tecmint/desktop/tecmint/tecmint7.txt</code>

<code>/home/tecmint/desktop/tecmint/tecmint9.txt</code>

<code>/home/tecmint/desktop/tecmint/tecmint10.txt</code>

<code>/home/tecmint/desktop/tecmint/tecmint2.txt</code>

<code>/home/tecmint/desktop/tecmint/tecmint5.txt</code>

<code>/home/tecmint/desktop/tecmint/tecmint14.txt</code>

<code>/home/tecmint/desktop/tecmint/tecmint1.txt</code>

<code>/home/tecmint/desktop/tecmint/tecmint15.txt</code>

<code>/home/tecmint/desktop/tecmint/tecmint12.txt</code>

3、 使用-r選項在每個目錄包括其子目錄中遞歸搜尋重複檔案。

它會遞歸搜尋所有檔案和檔案夾,花一點時間來掃描重複檔案,時間的長短取決于檔案和檔案夾的數量。在此其間,終端中會顯示全部過程,像下面這樣。

<code>$ fdupes -r /home</code>

<code>progress [37780/54747] 69%</code>

4、 使用-s選項來檢視某個檔案夾内找到的重複檔案的大小。

<code>$ fdupes -s /home/$user/desktop/tecmint</code>

<code>65 bytes each:</code>

5、 你可以同時使用-s和-r選項來檢視所有涉及到的目錄和子目錄中的重複檔案的大小,如下:

<code>$ fdupes -sr /home/avi/desktop/</code>

<code>107 bytes each:</code>

<code>/home/tecmint/desktop/resume_files/r-csc.html</code>

<code>/home/tecmint/desktop/resume_files/fc.html</code>

6、 不同于在一個或所有檔案夾内遞歸搜尋,你可以選擇按要求有選擇性地在兩個或三個檔案夾内進行搜尋。不必再提醒你了吧,如有需要,你可以使用-s和/或-r選項。

<code>$ fdupes /home/avi/desktop/ /home/avi/templates/</code>

7、 要删除重複檔案,同時保留一個副本,你可以使用<code>-d</code>選項。使用該選項,你必須額外小心,否則最終結果可能會是檔案/資料的丢失。鄭重提醒,此操作不可恢複。

<code>$ fdupes -d /home/$user/desktop/tecmint</code>

<code>[1] /home/tecmint/desktop/tecmint/tecmint13.txt</code>

<code>[2] /home/tecmint/desktop/tecmint/tecmint8.txt</code>

<code>[3] /home/tecmint/desktop/tecmint/tecmint11.txt</code>

<code>[4] /home/tecmint/desktop/tecmint/tecmint3.txt</code>

<code>[5] /home/tecmint/desktop/tecmint/tecmint4.txt</code>

<code>[6] /home/tecmint/desktop/tecmint/tecmint6.txt</code>

<code>[7] /home/tecmint/desktop/tecmint/tecmint7.txt</code>

<code>[8] /home/tecmint/desktop/tecmint/tecmint9.txt</code>

<code>[9] /home/tecmint/desktop/tecmint/tecmint10.txt</code>

<code>[10] /home/tecmint/desktop/tecmint/tecmint2.txt</code>

<code>[11] /home/tecmint/desktop/tecmint/tecmint5.txt</code>

<code>[12] /home/tecmint/desktop/tecmint/tecmint14.txt</code>

<code>[13] /home/tecmint/desktop/tecmint/tecmint1.txt</code>

<code>[14] /home/tecmint/desktop/tecmint/tecmint15.txt</code>

<code>[15] /home/tecmint/desktop/tecmint/tecmint12.txt</code>

<code>set 1 of 1, preserve files [1 - 15, all]:</code>

你可能注意到了,所有重複的檔案被列了出來,并給出删除提示,一個一個來,或者指定範圍,或者一次性全部删除。你可以選擇一個範圍,就像下面這樣,來删除指定範圍内的檔案。

<code>set 1 of 1, preserve files [1 - 15, all]: 2-15</code>

<code>[-] /home/tecmint/desktop/tecmint/tecmint13.txt</code>

<code>[+] /home/tecmint/desktop/tecmint/tecmint8.txt</code>

<code>[-] /home/tecmint/desktop/tecmint/tecmint11.txt</code>

<code>[-] /home/tecmint/desktop/tecmint/tecmint3.txt</code>

<code>[-] /home/tecmint/desktop/tecmint/tecmint4.txt</code>

<code>[-] /home/tecmint/desktop/tecmint/tecmint6.txt</code>

<code>[-] /home/tecmint/desktop/tecmint/tecmint7.txt</code>

<code>[-] /home/tecmint/desktop/tecmint/tecmint9.txt</code>

<code>[-] /home/tecmint/desktop/tecmint/tecmint10.txt</code>

<code>[-] /home/tecmint/desktop/tecmint/tecmint2.txt</code>

<code>[-] /home/tecmint/desktop/tecmint/tecmint5.txt</code>

<code>[-] /home/tecmint/desktop/tecmint/tecmint14.txt</code>

<code>[-] /home/tecmint/desktop/tecmint/tecmint1.txt</code>

<code>[-] /home/tecmint/desktop/tecmint/tecmint15.txt</code>

<code>[-] /home/tecmint/desktop/tecmint/tecmint12.txt</code>

8、 從安全角度出發,你可能想要列印<code>fdupes</code>的輸出結果到檔案中,然後檢查文本檔案來決定要删除什麼檔案。這可以降低意外删除檔案的風險。你可以這麼做:

<code>$ fdupes -sr /home &gt; /home/fdupes.txt</code>

注意:你應該替換<code>/home</code>為你想要的檔案夾。同時,如果你想要遞歸搜尋并列印大小,可以使用<code>-r</code>和<code>-s</code>選項。

9、 你可以使用<code>-f</code>選項來忽略每個比對集中的首個檔案。

首先列出該目錄中的檔案。

<code>$ ls -l /home/$user/desktop/tecmint</code>

<code>total 20</code>

<code>-rw-r--r-- 1 tecmint tecmint 65 aug 8 11:22 tecmint9 (3rd copy).txt</code>

<code>-rw-r--r-- 1 tecmint tecmint 65 aug 8 11:22 tecmint9 (4th copy).txt</code>

<code>-rw-r--r-- 1 tecmint tecmint 65 aug 8 11:22 tecmint9 (another copy).txt</code>

<code>-rw-r--r-- 1 tecmint tecmint 65 aug 8 11:22 tecmint9 (copy).txt</code>

然後,忽略掉每個比對集中的首個檔案。

<code>$ fdupes -f /home/$user/desktop/tecmint</code>

<code>/home/tecmint/desktop/tecmint9 (copy).txt</code>

<code>/home/tecmint/desktop/tecmint9 (3rd copy).txt</code>

<code>/home/tecmint/desktop/tecmint9 (another copy).txt</code>

<code>/home/tecmint/desktop/tecmint9 (4th copy).txt</code>

10、 檢查已安裝的fdupes版本。

<code>$ fdupes --version</code>

<code>fdupes 1.51</code>

11、 如果你需要關于fdupes的幫助,可以使用<code>-h</code>開關。

<code>$ fdupes -h</code>

<code>usage: fdupes [options] directory...</code>

<code>-r --recurse for every directory given follow subdirectories</code>

<code>encountered within</code>

<code>-r --recurse: for each directory given after this option follow</code>

<code>subdirectories encountered within (note the ':' at</code>

<code>the end of the option, manpage for more details)</code>

<code>-s --symlinks follow symlinks</code>

<code>-h --hardlinks normally, when two or more files point to the same</code>

<code>disk area they are treated as non-duplicates; this</code>

<code>option will change this behavior</code>

<code>-n --noempty exclude zero-length files from consideration</code>

<code>-a --nohidden exclude hidden files from consideration</code>

<code>-f --omitfirst omit the first file in each set of matches</code>

<code>-1 --sameline list each set of matches on a single line</code>

<code>-s --size show size of duplicate files</code>

<code>-m --summarize summarize dupe information</code>

<code>-q --quiet hide progress indicator</code>

<code>-d --delete prompt user for files to preserve and delete all</code>

<code>others; important: under particular circumstances,</code>

<code>data may be lost when using this option together</code>

<code>with -s or --symlinks, or when specifying a</code>

<code>particular directory more than once; refer to the</code>

<code>fdupes documentation for additional information</code>

<code>-n --noprompt together with --delete, preserve the first file in</code>

<code>each set of duplicates and delete the rest without</code>

<code>prompting the user</code>

<code>-v --version display fdupes version</code>

<code>-h --help display this help message</code>

到此為止了。讓我知道你以前怎麼在linux中查找并删除重複檔案的吧?同時,也讓我知道你關于這個工具的看法。在下面的評論部分中提供你有價值的回報吧,别忘了為我們點贊并分享,幫助我們擴散哦。

本文來自雲栖社群合作夥伴“linux中國”,原文釋出日期:2015-09-01

繼續閱讀