Is it possible to find duplicate files on my disk which are bit to bit identical but have different file-names?
12 Answers
fdupes
can do this. From man fdupes
:
Searches the given path for duplicate files. Such files are found by comparing file sizes and MD5 signatures, followed by a byte-by-byte comparison.
In Debian or Ubuntu, you can install it with apt-get install fdupes
. In Fedora/Red Hat/CentOS, you can install it with yum install fdupes
. On Arch Linux you can use pacman -S fdupes
, and on Gentoo, emerge fdupes
.
To run a check descending from your filesystem root, which will likely take a significant amount of time and memory, use something like fdupes -r /
.
As asked in the comments, you can get the largest duplicates by doing the following:
fdupes -r . | {
while IFS= read -r file; do
[[ $file ]] && du "$file"
done
} | sort -n
This will break if your filenames contain newlines.
-
Thanks. How can I filter out the largest dupe? How can I make the sizes human readable?– studentApr 5, 2013 at 9:31
-
@student: use something along the line of (make sure fdupes just outputs the filenames with no extra informatinos, or cut or sed to just keep that) :
fdupes ....... | xargs ls -alhd | egrep 'M |G '
to keep files in Human readable format and only those with size in Megabytes or Gigabytes. Change the command to suit the real outputs. Apr 5, 2013 at 12:27 -
2@OlivierDulac You should never parse ls. Usually it's worse than your use case, but even in your use case, you risk false positives. Apr 5, 2013 at 13:13
-
@student - Once you have the filenames,
du
piped tosort
will tell you. Apr 5, 2013 at 13:14 -
@ChrisDown: it's true it's a bad habit, and can give false positives. But in that case (interactive use, and for display only, no "rm" or anything of the sort directly relying on it) it's fine and quick ^^ . I love those pages you link to, btw (been reading them since a few months, and full of many usefull infos) Apr 5, 2013 at 14:05
Another good tool is fslint
:
fslint is a toolset to find various problems with filesystems, including duplicate files and problematic filenames etc.
Individual command line tools are available in addition to the GUI and to access them, one can change to, or add to $PATH the /usr/share/fslint/fslint directory on a standard install. Each of these commands in that directory have a --help option which further details its parameters.
findup - find DUPlicate files
On debian-based systems, youcan install it with:
sudo apt-get install fslint
You can also do this manually if you don't want to or cannot install third party tools. The way most such programs work is by calculating file checksums. Files with the same md5sum almost certainly contain exactly the same data. So, you could do something like this:
find / -type f -exec md5sum {} \; > md5sums
awk '{print $1}' md5sums | sort | uniq -d > dupes
while read -r d; do echo "---"; grep -- "$d" md5sums | cut -d ' ' -f 2-; done < dupes
Sample output (the file names in this example are the same, but it will also work when they are different):
$ while read -r d; do echo "---"; grep -- "$d" md5sums | cut -d ' ' -f 2-; done < dupes
---
/usr/src/linux-headers-3.2.0-3-common/include/linux/if_bonding.h
/usr/src/linux-headers-3.2.0-4-common/include/linux/if_bonding.h
---
/usr/src/linux-headers-3.2.0-3-common/include/linux/route.h
/usr/src/linux-headers-3.2.0-4-common/include/linux/route.h
---
/usr/src/linux-headers-3.2.0-3-common/include/drm/Kbuild
/usr/src/linux-headers-3.2.0-4-common/include/drm/Kbuild
---
This will be much slower than the dedicated tools already mentioned, but it will work.
-
4It would be much, much faster to find any files with the same size as another file using
st_size
, eliminating any that only have one file of this size, and then calculating md5sums only between files with the samest_size
. Apr 4, 2013 at 16:34 -
@ChrisDown yeah, just wanted to keep it simple. What you suggest will greatly speed things up of course. That's why I have the disclaimer about it being slow at the end of my answer.– terdon ♦Apr 4, 2013 at 16:37
-
It can be run on macOS, but you should replace
md5sum {}
withmd5 -q {}
andgawk '{print $1}'
withcat
– FinesseOct 24, 2019 at 2:26
Short answer: yes.
Longer version: have a look at the wikipedia fdupes entry, it sports quite nice list of ready made solutions. Of course you can write your own, it's not that difficult - hashing programs like diff
, sha*sum
, find
, sort
and uniq
should do the job. You can even put it on one line, and it will still be understandable.
I thought to add a recent enhanced fork of fdupes, jdupes, which promises to be faster and more feature rich than fdupes (e.g. size filter):
jdupes . -rS -X size-:50m > myjdups.txt
This will recursively find duplicated files bigger than 50MB in the current directory and output the resulted list in myjdups.txt.
Note, the output is not sorted by size and since it appears not to be build in, I have adapted @Chris_Down answer above to achieve this:
jdupes -r . -X size-:50m | {
while IFS= read -r file; do
[[ $file ]] && du "$file"
done
} | sort -n > myjdups_sorted.txt
-
1Note: the latest version of jdupes supports matching files with only a partial hash instead of waiting to hash the whole thing. Very useful. (You have to clone the git archive to get it.) Here are the option I'm using right now: jdupes -r -T -T --exclude=size-:50m --nohidden Jul 3, 2019 at 17:48
If you believe a hash function (here MD5) is collision-free on your domain:
find $target -type f -exec md5sum '{}' + | sort | uniq --all-repeated --check-chars=32 \
| cut --characters=35-
Want identical file names grouped? Write a simple script not_uniq.sh
to format output:
#!/bin/bash
last_checksum=0
while read line; do
checksum=${line:0:32}
filename=${line:34}
if [ $checksum == $last_checksum ]; then
if [ ${last_filename:-0} != '0' ]; then
echo $last_filename
unset last_filename
fi
echo $filename
else
if [ ${last_filename:-0} == '0' ]; then
echo "======="
fi
last_filename=$filename
fi
last_checksum=$checksum
done
Then change find
command to use your script:
chmod +x not_uniq.sh
find $target -type f -exec md5sum '{}' + | sort | not_uniq.sh
This is basic idea. Probably you should change find
if your file names containing some characters. (e.g space)
-
You can skip the script and use
--all-repeated=separate
for a similar result.– JacktoseSep 5, 2021 at 18:39
Wikipedia once had an article with a list of available open source software for this task, but it's now been deleted.
I will add that the GUI version of fslint is very interesting, allowing to use mask to select which files to delete - very useful to clean duplicated photos.
On Linux you can use:
- FSLint: http://www.pixelbeat.org/fslint/
- FDupes: https://en.wikipedia.org/wiki/Fdupes
- DupeGuru: https://www.hardcoded.net/dupeguru/
- Czkawka: https://qarmin.github.io/czkawka/
FDupes and DupeGuru work on many systems (Windows, Mac and Linux). I've not checked FSLint or Czkawka.
-
5It is better to provide actual information here and not just a link, the link might change and then the answer has no value left– AnthonJan 29, 2014 at 11:22
-
2
-
-
I had a situation where I was working in an environment where I couldn't install new software, and had to scan >380 GB of JPG and MOV files for duplicates. I developed the following POSIX awk script to process all of the data in 72 seconds (as opposed to the find -exec md5sum
approach, that took over 90 minutes to run):
https://github.com/taltman/scripts/blob/master/unix_utils/find-dupes.awk
You call it as follows:
ls -lTR | awk -f find-dupes.awk
It was developed on a FreeBSD shell environment, so might need some tweaks to work optimally in a GNU/Linux shell environment.
Here's my take on that:
find -type f -size +3M -print0 | while IFS= read -r -d '' i; do
echo -n '.'
if grep -q "$i" md5-partial.txt; then echo -e "\n$i ---- Already counted, skipping."; continue; fi
MD5=`dd bs=1M count=1 if="$i" status=noxfer | md5sum`
MD5=`echo $MD5 | cut -d' ' -f1`
if grep "$MD5" md5-partial.txt; then echo "\n$i ---- Possible duplicate"; fi
echo $MD5 $i >> md5-partial.txt
done
It's different in that it only hashes up to first 1 MB of the file.
This has few issues / features:
- There might be a difference after first 1 MB so the result rather a candidate to check. I might fix that later.
- Checking by file size first could speed this up.
- Only takes files larger than 3 MB.
I use it to compare video clips so this is enough for me.
I realize this is necro but it is highly relevant. I had asked a similar question on Find duplicate files based on first few characters of filename and what was a presented was a solution to use some awk script.
I use it for mod conflict cleanup, useful in Forge packs 1.14.4+ because Forge now disabled mods that are older instead of FATAL crashing and letting you know of the duplicate.
#!/bin/bash
declare -a names
xIFS="${IFS}"
IFS="^M"
while true; do
awk -F'[-_ ]' '
NR==FNR {seen[tolower($1)]++; next}
seen[tolower($1)] > 1
' <(printf "%s\n" *.jar) <(printf "%s\n" *.jar) > tmp.dat
IDX=0
names=()
readarray names < tmp.dat
size=${#names[@]}
clear
printf '\nPossible Dupes\n'
for (( i=0; i<${size}; i++)); do
printf '%s\t%s' ${i} ${names[i]}
done
printf '\nWhich dupe would you like to delete?\nEnter # to delete or q to quit\n'
read n
if [ $n == 'q' ]; then
exit
fi
if [ $n -lt 0 ] || [ $n -gt $size ]; then
read -p "Invalid Option: present [ENTER] to try again" dummyvar
continue
fi
#clean the carriage return \n from the name
IFS='^M'
read -ra TARGET <<< "${names[$n]}"
unset IFS
#now remove the first element from the filesystem
rm "${TARGET[0]}"
echo "removed ${TARGET[0]}" >> rm.log
done
IFS="${xIFS}"
I recommend saving it as "dupes.sh" to your personal bin or /usr/var/bin
You can find duplicate with this command:
time find . ! -empty -type f -print0 | xargs -0 -P"$(nproc)" -I{} md5sum "{}" | sort | uniq -w32 -dD
For free open-source Linux duplicate file finders, there is a new kid on the block and it's even in a trendy language, Rust.
It is called Czkawka (which apparently means hiccup)
So it does have an unpronounceable name unless you speak Polish.
It is based very much on some of the ideas in FSlint (which can now be difficult to make work as it is no longer maintained and uses the now deprecated Python 2.x).
Czkawka has both GUI and CLI versions and is reported to be faster than FSlint and Fdupes.
There is also a Githup repo For those that want to fork it just to change the name.
On Linux, you can use the following tools to find duplicate files.
https://github.com/adrianlopezroche/fdupes
https://github.com/arsenetar/dupeguru
https://github.com/jbruchon/jdupes
https://github.com/pauldreik/rdfind
https://github.com/pixelb/fslint (Python 2.x)
https://github.com/sahib/rmlint
https://github.com/qarmin/czkawka (Rust)
st_size
s, eliminating those with only one of the same, and then only calculating md5sums for matchingst_size
s.