fdupes:Linux中查找并刪除重復文件的命令行工具
對于大多數計算機用戶而言,查找并替換重復的文件是一個常見的需求。查找并移除重復文件真是一項令人不勝其煩的工作,它耗時又耗力。但如果你的機器上跑著GNU/Linux,那么查找重復文件會變得十分簡單,這多虧了fdupes工具。
fdupes——在Linux中查找并刪除重復文件
fdupes是啥東東?
fdupes是Linux下的一個工具,它由Adrian Lopez用C編程語言編寫并基于MIT許可證發行,該應用程序可以在指定的目錄及子目錄中查找重復的文件。fdupes通過對比文件的MD5簽名,以及逐字節比較文件來識別重復內容,fdupes有各種選項,可以實現對文件的列出、刪除、替換為文件副本的硬鏈接等操作。
文件對比以下列順序開始:
大小對比 > 部分 MD5 簽名對比 > 完整 MD5 簽名對比 > 逐字節對比
安裝 fdupes 到 Linux
在基于Debian的系統上,如Ubuntu和Linux Mint,安裝***版fdupes,用下面的命令手到擒來。
$ sudo apt-get install fdupes
在基于CentOS/RHEL和Fedora的系統上,你需要開啟epel倉庫來安裝fdupes包。
# yum install fdupes# dnf install fdupes [在 Fedora 22 及其以后]
注意:自Fedora 22之后,默認的包管理器yum被dnf取代了。
fdupes命令如何使用
1、作為演示的目的,讓我們來在某個目錄(比如 tecmint)下創建一些重復文件,命令如下:
$ mkdir /home/"$USER"/Desktop/tecmint && cd /home/"$USER"/Desktop/tecmint && for i in {1..15}; do echo "I Love Tecmint. Tecmint is a very nice community of Linux Users." > tecmint${i}.txt ; done
在執行以上命令后,讓我們使用ls命令驗證重復文件是否創建。
$ ls -ltotal 60-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint10.txt-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint11.txt-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint12.txt-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint13.txt-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint14.txt-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint15.txt-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint1.txt-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint2.txt-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint3.txt-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint4.txt-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint5.txt-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint6.txt-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint7.txt-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint8.txt-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint9.txt
上面的腳本創建了15個文件,名稱分別為tecmint1.txt,tecmint2.txt……tecmint15.txt,并且每個文件的數據相同,如:
"I Love Tecmint. Tecmint is a very nice community of Linux Users."
2、現在在tecmint文件夾內搜索重復的文件。
$ fdupes /home/$USER/Desktop/tecmint/home/tecmint/Desktop/tecmint/tecmint13.txt/home/tecmint/Desktop/tecmint/tecmint8.txt/home/tecmint/Desktop/tecmint/tecmint11.txt/home/tecmint/Desktop/tecmint/tecmint3.txt/home/tecmint/Desktop/tecmint/tecmint4.txt/home/tecmint/Desktop/tecmint/tecmint6.txt/home/tecmint/Desktop/tecmint/tecmint7.txt/home/tecmint/Desktop/tecmint/tecmint9.txt/home/tecmint/Desktop/tecmint/tecmint10.txt/home/tecmint/Desktop/tecmint/tecmint2.txt/home/tecmint/Desktop/tecmint/tecmint5.txt/home/tecmint/Desktop/tecmint/tecmint14.txt/home/tecmint/Desktop/tecmint/tecmint1.txt/home/tecmint/Desktop/tecmint/tecmint15.txt/home/tecmint/Desktop/tecmint/tecmint12.txt
3、使用-r選項在每個目錄包括其子目錄中遞歸搜索重復文件。
它會遞歸搜索所有文件和文件夾,花一點時間來掃描重復文件,時間的長短取決于文件和文件夾的數量。在此其間,終端中會顯示全部過程,像下面這樣。
$ fdupes -r /homeProgress [37780/54747] 69%
4、使用-S選項來查看某個文件夾內找到的重復文件的大小。
$ fdupes -S /home/$USER/Desktop/tecmint65 bytes each:/home/tecmint/Desktop/tecmint/tecmint13.txt/home/tecmint/Desktop/tecmint/tecmint8.txt/home/tecmint/Desktop/tecmint/tecmint11.txt/home/tecmint/Desktop/tecmint/tecmint3.txt/home/tecmint/Desktop/tecmint/tecmint4.txt/home/tecmint/Desktop/tecmint/tecmint6.txt/home/tecmint/Desktop/tecmint/tecmint7.txt/home/tecmint/Desktop/tecmint/tecmint9.txt/home/tecmint/Desktop/tecmint/tecmint10.txt/home/tecmint/Desktop/tecmint/tecmint2.txt/home/tecmint/Desktop/tecmint/tecmint5.txt/home/tecmint/Desktop/tecmint/tecmint14.txt/home/tecmint/Desktop/tecmint/tecmint1.txt/home/tecmint/Desktop/tecmint/tecmint15.txt/home/tecmint/Desktop/tecmint/tecmint12.txt
5、你可以同時使用-S和-r選項來查看所有涉及到的目錄和子目錄中的重復文件的大小,如下:
$ fdupes -Sr /home/avi/Desktop/65 bytes each:/home/tecmint/Desktop/tecmint/tecmint13.txt/home/tecmint/Desktop/tecmint/tecmint8.txt/home/tecmint/Desktop/tecmint/tecmint11.txt/home/tecmint/Desktop/tecmint/tecmint3.txt/home/tecmint/Desktop/tecmint/tecmint4.txt/home/tecmint/Desktop/tecmint/tecmint6.txt/home/tecmint/Desktop/tecmint/tecmint7.txt/home/tecmint/Desktop/tecmint/tecmint9.txt/home/tecmint/Desktop/tecmint/tecmint10.txt/home/tecmint/Desktop/tecmint/tecmint2.txt/home/tecmint/Desktop/tecmint/tecmint5.txt/home/tecmint/Desktop/tecmint/tecmint14.txt/home/tecmint/Desktop/tecmint/tecmint1.txt/home/tecmint/Desktop/tecmint/tecmint15.txt/home/tecmint/Desktop/tecmint/tecmint12.txt107 bytes each:/home/tecmint/Desktop/resume_files/r-csc.html/home/tecmint/Desktop/resume_files/fc.html
#p#
6、不同于在一個或所有文件夾內遞歸搜索,你可以選擇按要求有選擇性地在兩個或三個文件夾內進行搜索。不必再提醒你了吧,如有需要,你可以使用-S和/或-r選項。
$ fdupes /home/avi/Desktop/ /home/avi/Templates/
7、要刪除重復文件,同時保留一個副本,你可以使用-d選項。使用該選項,你必須額外小心,否則最終結果可能會是文件/數據的丟失。鄭重提醒,此操作不可恢復。
$ fdupes -d /home/$USER/Desktop/tecmint[1] /home/tecmint/Desktop/tecmint/tecmint13.txt[2] /home/tecmint/Desktop/tecmint/tecmint8.txt[3] /home/tecmint/Desktop/tecmint/tecmint11.txt[4] /home/tecmint/Desktop/tecmint/tecmint3.txt[5] /home/tecmint/Desktop/tecmint/tecmint4.txt[6] /home/tecmint/Desktop/tecmint/tecmint6.txt[7] /home/tecmint/Desktop/tecmint/tecmint7.txt[8] /home/tecmint/Desktop/tecmint/tecmint9.txt[9] /home/tecmint/Desktop/tecmint/tecmint10.txt[10] /home/tecmint/Desktop/tecmint/tecmint2.txt[11] /home/tecmint/Desktop/tecmint/tecmint5.txt[12] /home/tecmint/Desktop/tecmint/tecmint14.txt[13] /home/tecmint/Desktop/tecmint/tecmint1.txt[14] /home/tecmint/Desktop/tecmint/tecmint15.txt[15] /home/tecmint/Desktop/tecmint/tecmint12.txtSet 1 of 1, preserve files [1 - 15, all]:
你可能注意到了,所有重復的文件被列了出來,并給出刪除提示,一個一個來,或者指定范圍,或者一次性全部刪除。你可以選擇一個范圍,就像下面這樣,來刪除指定范圍內的文件。
Set 1 of 1, preserve files [1 - 15, all]: 2-15[-] /home/tecmint/Desktop/tecmint/tecmint13.txt[+] /home/tecmint/Desktop/tecmint/tecmint8.txt[-] /home/tecmint/Desktop/tecmint/tecmint11.txt[-] /home/tecmint/Desktop/tecmint/tecmint3.txt[-] /home/tecmint/Desktop/tecmint/tecmint4.txt[-] /home/tecmint/Desktop/tecmint/tecmint6.txt[-] /home/tecmint/Desktop/tecmint/tecmint7.txt[-] /home/tecmint/Desktop/tecmint/tecmint9.txt[-] /home/tecmint/Desktop/tecmint/tecmint10.txt[-] /home/tecmint/Desktop/tecmint/tecmint2.txt[-] /home/tecmint/Desktop/tecmint/tecmint5.txt[-] /home/tecmint/Desktop/tecmint/tecmint14.txt[-] /home/tecmint/Desktop/tecmint/tecmint1.txt[-] /home/tecmint/Desktop/tecmint/tecmint15.txt[-] /home/tecmint/Desktop/tecmint/tecmint12.txt
8、從安全角度出發,你可能想要打印fdupes的輸出結果到文件中,然后檢查文本文件來決定要刪除什么文件。這可以降低意外刪除文件的風險。你可以這么做:
$ fdupes -Sr /home > /home/fdupes.txt
注意:你應該替換/home為你想要的文件夾。同時,如果你想要遞歸搜索并打印大小,可以使用-r和-S選項。
9、你可以使用-f選項來忽略每個匹配集中的***文件。
首先列出該目錄中的文件。
$ ls -l /home/$USER/Desktop/tecminttotal 20-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint9 (3rd copy).txt-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint9 (4th copy).txt-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint9 (another copy).txt-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint9 (copy).txt-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint9.txt
然后,忽略掉每個匹配集中的***文件。
$ fdupes -f /home/$USER/Desktop/tecmint/home/tecmint/Desktop/tecmint9 (copy).txt/home/tecmint/Desktop/tecmint9 (3rd copy).txt/home/tecmint/Desktop/tecmint9 (another copy).txt/home/tecmint/Desktop/tecmint9 (4th copy).txt
10、檢查已安裝的fdupes版本。
$ fdupes --versionfdupes 1.51
11、如果你需要關于fdupes的幫助,可以使用-h開關。
$ fdupes -hUsage: fdupes [options] DIRECTORY...-r --recurse for every directory given follow subdirectoriesencountered within-R --recurse: for each directory given after this option followsubdirectories encountered within (note the ':' atthe end of the option, manpage for more details)-s --symlinks follow symlinks-H --hardlinks normally, when two or more files point to the samedisk area they are treated as non-duplicates; thisoption will change this behavior-n --noempty exclude zero-length files from consideration-A --nohidden exclude hidden files from consideration-f --omitfirst omit the first file in each set of matches-1 --sameline list each set of matches on a single line-S --size show size of duplicate files-m --summarize summarize dupe information-q --quiet hide progress indicator-d --delete prompt user for files to preserve and delete allothers; important: under particular circumstances,data may be lost when using this option togetherwith -s or --symlinks, or when specifying aparticular directory more than once; refer to thefdupes documentation for additional information-N --noprompt together with --delete, preserve the first file ineach set of duplicates and delete the rest withoutprompting the user-v --version display fdupes version-h --help display this help message
到此為止了。讓我知道你以前怎么在Linux中查找并刪除重復文件的吧?同時,也讓我知道你關于這個工具的看法。在下面的評論部分中提供你有價值的反饋吧!別忘了為我們點贊并分享,幫助我們擴散哦!
我正在使用另外一個移除重復文件的工具,它叫fslint。很快就會把使用心得分享給大家哦,你們一定會喜歡看的。
































