Việc tìm và thay thế file trùng lặp là nhu cầu phổ biến của hầu hết người dùng máy tính. Tìm và xóa file trùng lặp là công việc mệt mỏi đòi hỏi thời gian và kiên nhẫn. Việc tìm file trùng lặp có thể rất dễ dàng nếu máy của bạn chạy GNU/Linux, nhờ tiện ích fdupes.

fdupes là gì?
Fdupes là một tiện ích Linux được viết bởi Adrian Lopez bằng ngôn ngữ lập trình C, phát hành dưới giấy phép MIT. Ứng dụng có thể tìm các file trùng lặp trong tập thư mục và thư mục con cho trước. Fdupes nhận diện bản sao bằng cách so sánh chữ ký MD5 của file, tiếp theo là so sánh byte-per-byte. Nhiều tùy chọn có thể truyền cho Fdupes để liệt kê, xóa và thay thế file bằng hardlink trỏ đến bản sao.
Quá trình so sánh diễn ra theo thứ tự:
so sánh kích thước > So sánh chữ ký MD5 một phần > So sánh chữ ký MD5 đầy đủ > So sánh Byte-per-Byte.
Cài đặt fdupes trên Linux
Cài đặt phiên bản mới nhất của fdupes (phiên bản 1.51) dễ dàng như chạy lệnh sau trên các hệ thống dựa trên Debian như Ubuntu và Linux Mint.
$ sudo apt-get install fdupes
Trên các hệ thống dựa trên CentOS/RHEL và Fedora, bạn cần bật kho epel để cài đặt gói fdupes.
# yum install fdupes # dnf install fdupes [Trên Fedora 22 trở đi]
Lưu ý: Trình quản lý gói mặc định yum được thay thế bằng dnf từ Fedora 22 trở đi…
Cách sử dụng lệnh fdupes?
1. Với mục đích minh họa, hãy tạo một vài file trùng lặp trong một thư mục (giả sử tecmint) đơn giản như sau:
$ 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
Sau khi chạy lệnh trên, hãy kiểm tra xem file trùng lặp đã được tạo chưa bằng lệnh ls.
$ ls -l total 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 ... (các file tecmint1.txt đến tecmint15.txt)
Script trên tạo ra 15 file tên tecmint1.txt, tecmint2.txt…tecmint15.txt và mọi file đều chứa cùng dữ liệu:
"I Love Tecmint. Tecmint is a very nice community of Linux Users."
2. Bây giờ tìm kiếm file trùng lặp trong thư mục tecmint.
$ fdupes /home/$USER/Desktop/tecmint /home/tecmint/Desktop/tecmint/tecmint13.txt /home/tecmint/Desktop/tecmint/tecmint8.txt ... (danh sách đầy đủ 15 file)
3. Tìm kiếm trùng lặp đệ quy trong mọi thư mục bao gồm thư mục con bằng tùy chọn -r.
$ fdupes -r /home Progress [37780/54747] 69%
4. Xem kích thước các bản sao tìm thấy trong thư mục bằng tùy chọn -S.
$ fdupes -S /home/$USER/Desktop/tecmint 65 bytes each: /home/tecmint/Desktop/tecmint/tecmint13.txt ... (danh sách file)
5. Bạn có thể xem kích thước file trùng lặp cho mọi thư mục và thư mục con gặp phải bằng cách dùng -S và -r cùng lúc:
$ fdupes -Sr /home/avi/Desktop/ 65 bytes each: ... 107 bytes each: ...
6. Ngoài tìm kiếm trong một thư mục hoặc tất cả đệ quy, bạn có thể chọn tìm trong hai hoặc ba thư mục theo yêu cầu. Cũng có thể dùng tùy chọn -S và/hoặc -r nếu cần.
$ fdupes /home/avi/Desktop/ /home/avi/Templates/
7. Để xóa file trùng lặp trong khi giữ lại một bản sao, bạn có thể dùng tùy chọn '-d'. Cần cẩn thận khi dùng tùy chọn này, nếu không bạn có thể mất file/dữ liệu cần thiết và quá trình này không thể khôi phục.
$ fdupes -d /home/$USER/Desktop/tecmint [1] /home/tecmint/Desktop/tecmint/tecmint13.txt [2] /home/tecmint/Desktop/tecmint/tecmint8.txt ... Set 1 of 1, preserve files [1 - 15, all]:
Bạn có thể chọn một phạm vi để xóa file trong phạm vi cụ thể, ví dụ 2-15.
8. Từ góc độ an toàn, bạn có thể muốn in đầu ra của fdupes ra file và sau đó kiểm tra file văn bản để quyết định file nào cần xóa. Điều này giảm nguy cơ xóa nhầm file. Bạn có thể làm:
$ fdupes -Sr /home > /home/fdupes.txt
Lưu ý: Bạn có thể thay thế '/home' bằng thư mục mong muốn. Cũng dùng tùy chọn '-r' và '-S' nếu muốn tìm đệ quy và in kích thước.
9. Bạn có thể bỏ qua file đầu tiên trong mỗi tập khớp bằng tùy chọn '-f'.
$ ls -l /home/$USER/Desktop/tecmint total 20 -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint9 (3rd copy).txt ... (các file copy)
$ 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. Kiểm tra phiên bản fdupes đã cài đặt.
$ fdupes --version fdupes 1.51
11. Nếu bạn cần trợ giúp về fdupes, hãy dùng công tắc '-h'.
$ fdupes -h Usage: fdupes [options] DIRECTORY... -r --recurse for every directory given follow subdirectories encountered within -R --recurse: for each directory given after this option follow subdirectories encountered within (note the ':' at the end of the option, manpage for more details) -s --symlinks follow symlinks -H --hardlinks normally, when two or more files point to the same disk area they are treated as non-duplicates; this option 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 all others; important: under particular circumstances, data may be lost when using this option together with -s or --symlinks, or when specifying a particular directory more than once; refer to the fdupes documentation for additional information -N --noprompt together with --delete, preserve the first file in each set of duplicates and delete the rest without prompting the user -v --version display fdupes version -h --help display this help message
Đó là tất cả cho đến nay. Hãy cho tôi biết bạn tìm và xóa file trùng lặp như thế nào cho đến nay trong Linux? và cũng cho tôi ý kiến của bạn về tiện ích này. Đặt phản hồi quý báu của bạn trong phần bình luận bên dưới và đừng quên like/share để giúp chúng tôi lan tỏa.
Tôi đang làm việc trên một tiện ích khác gọi là fslint để xóa file trùng lặp, sẽ đăng sớm và các bạn sẽ thích đọc.