Linux: find duplicate files in directory using sha1sum
Private research annotation
Sign in to add a private research label or note.
- User
Linux: find duplicate files in directory using sha1sum
- Assistant
To find duplicate files in a directory using
sha1sum, you can follow these steps in a Linux terminal. The process involves generating SHA-1 checksums for files and then identifying files with the same checksum.Here’s a step-by-step method using a command-line approach:
-
Open your terminal.
-
Navigate to the directory where you want to check for duplicate files. Use the
cdcommand:
-
- User
I used this command:
find . ! -empty -type f -exec md5sum {} + | sort | uniq -w32 -dD > ~/result.txt
There are sorted contents of ~/result.txt: 4fe317872a6ddd3fb05bd4bc0d7695fdace04ef7 ./file1 4fe317872a6ddd3fb05bd4bc0d7695fdace04ef7 ./file2 64485f3f53e9393efdd0e00f8b38a629746fe3d0 ./file3 5be824152c662eeb3327c5afc332b776d28bd359 ./file4 5be824152c662eeb3327c5afc332b776d28bd359 ./file5 ...
You can see that file1 and file2, file4 and file5 are duplicates. Write simple Python script to keep first original file and remove all duplicates
- Assistant
You can use the following simple Python script to process the
result.txtfile you created, remove duplicate files, and keep only the first original file for each set of duplicates. Here’s how you can do it: