Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Universal configuration monitoring and system of record for IT.
Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!
Hide

Top Tags

Hide

Functions

Hide

Credits

Find Duplicate Files (based on size first, then MD5 hash)

Terminal - Find Duplicate Files (based on size first, then MD5 hash)
find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate
2009-09-21 00:24:14
User: syssyphus
Functions: find md5sum sort uniq xargs
59
Find Duplicate Files (based on size first, then MD5 hash)

This dup finder saves time by comparing size first, then md5sum, it doesn't delete anything, just lists them.

Alternatives

There are 9 alternatives - vote for the best!

Terminal - Alternatives
find -type f -exec md5sum '{}' ';' | sort | uniq --all-repeated=separate -w 33 | cut -c 35-
2009-08-04 07:05:12
User: infinull
Functions: cut find md5sum sort uniq
18

Calculates md5 sum of files. sort (required for uniq to work). uniq based on only the hash. use cut ro remove the hash from the result.

fdupes -r .
2011-02-19 17:02:30
User: Vilemirth
Tags: xargs parallel
16

If you have the fdupes command, you'll save a lot of typing. It can do recursive searches (-r,-R) and it allows you to interactively select which of the duplicate files found you wish to keep or delete.

find -type d -name ".svn" -prune -o -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type d -name ".svn" -prune -o -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate
2010-01-28 09:45:29
User: 2chg
Functions: find md5sum sort uniq xargs
2

Improvement of the command "Find Duplicate Files (based on size first, then MD5 hash)" when searching for duplicate files in a directory containing a subversion working copy. This way the (multiple dupicates) in the meta-information directories are ignored.

Can easily be adopted for other VCS as well. For CVS i.e. change ".svn" into ".csv":

find -type d -name ".csv" -prune -o -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type d -name ".csv" -prune -o -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate
find -not -empty -type f -printf "%-30s'\t\"%h/%f\"\n" | sort -rn -t$'\t' | uniq -w30 -D | cut -f 2 -d $'\t' | xargs md5sum | sort | uniq -w32 --all-repeated=separate
2014-10-19 02:00:55
User: fobos3
Functions: cut find md5sum sort uniq xargs
1

Finds duplicates based on MD5 sum. Compares only files with the same size. Performance improvements on:

find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate

The new version takes around 3 seconds where the old version took around 17 minutes. The bottle neck in the old command was the second find. It searches for the files with the specified file size. The new version keeps the file path and size from the beginning.

find . -type f -exec md5 '{}' ';' | sort | uniq -f 3 -d | sed -e "s/.*(\(.*\)).*/\1/"
2012-01-14 08:54:12
User: noahspurrier
Functions: find sed sort uniq
0

This works on Mac OS X using the `md5` command instead of `md5sum`, which works similarly, but has a different output format. Note that this only prints the name of the duplicates, not the original file. This is handy because you can add `| xargs rm` to the end of the command to delete all the duplicates while leaving the original.

Know a better way?

If you can do better, submit your command here.

What others think

As an alternative, check out http://www.pixelbeat.org/fslint/ in case you don't mind using a GUI for this. It gives you the option of hard linking the duplicate files and doing other lint-y tasks. Available as package 'fslint' at least in debian/ubuntu.

Comment by bwoodacre 296 weeks and 1 day ago

Thanks for the FSlint reference. Note fslint uses much the same mechanism underneath and has a CLI mode.

http://fslint.googlecode.com/svn/trunk/fslint/findup

Comment by pixelbeat 296 weeks and 1 day ago

awsome, much faster then fdupes.

Comment by dakunesu 296 weeks ago

Isn't the -D redundant?

Comment by dennisw 294 weeks and 4 days ago

yes it is... thanks for noticing, I fixed it.

Comment by syssyphus 294 weeks and 4 days ago

How can you mass delete these files once they're found? (I'd like to keep one of them)

Comment by matthewbauer 290 weeks and 3 days ago

you might want to look at fdupes or fslint in order to help with hardlinking / deleting, etc... my command is really just a quick hack to list them.

Comment by syssyphus 290 weeks and 3 days ago

There is also perfect match:

http://pmatch.rubyforge.org/

That's especially if you are commandline fan.

Comment by zabuch 277 weeks and 5 days ago

Fantastic, man. this is truly great.

Comment by oernii3 219 weeks and 1 day ago

There is also rmlint:

https://github.com/sahib/rmlint

Example:

rmlint [path] -GYX -v5

+ Gives you similiar results

+ you can pipe it directly to 'sh'

+ it's lots faster as additionally fingerprints are done and a few other tricks.

+ it has also other options ;-)

Comment by sahib 217 weeks and 1 day ago

"find -type" doesn?t work on Mac OS X.

Comment by ELV1S 158 weeks ago

can filename comparison be added as a first step to the first solution given?

find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate

seems to me checking filename first could speed things up. if two files lack the same filename then in many cases i would not consider them a dupe.

thanks

Comment by johnywhy 150 weeks ago

The code for findup by Pádraig Brady (http://fslint.googlecodesh.com/svn/trunk/fslint/findup) is very OS (or user defined system) sensitive and is without comments that tell you what it is pointing to:

---------

./FindDups: line 62: /Programming/FSlint/supprt/fslver: No such file or directory

./FindDups: line 135: shell_quote: command not found

./FindDups: line 147: /Programming/FSlint/supprt/getfpf: No such file or directory

./FindDups: line 149: check_uniq: command not found

./FindDups: line 164: /Programming/FSlint/supprt/rmlint/merge_hardlinks: No such file or directory

---------

/Programming is my partition for assorted programming I am doing. I use openSUSE 12.1. I would assume (with all that connotes) that uniq could be used rather than check_uniq, and that the . /supprt directory is unique to another distro (why is there so much illogical difference - that eliminates a lot of people who would like to switch from Windows). Either that or it is one of Pádraig Brady's personnal directories and that does not fly unless they are included.

Considering this came from Google code, you have to first assume it is incomplete. And this is no exception to that!

Comment by JohnLB 147 weeks and 5 days ago

Hi, I would like if it is any way of find duplicates of a given file (not all duplicates on the fs) maybe searching directly by md5.

It would be great for me.

Comment by tia 111 weeks ago

Try this free tool to find similar files:

http://www.mindgems.com/products/Fast-Duplicate-File-Finder/Fast-Duplicate-File-Finder-About.htm

Comment by jackdorsey 100 weeks and 1 day ago

Does anyone know a way to find duplicate files between 2 volumes based only on size and name?

Comment by d0g 68 weeks and 2 days ago

kos omkon 3a hal exemple

Comment by mahmoud 50 weeks and 5 days ago

Here is the tool to find and delete duplicate files "DuplicateFilesDeleter"

Comment by ivanden 44 weeks and 5 days ago

Should be noted that md5sum is not a collision free algorithm, so there's a probability (OK, a very small probability) that the above commands will reports files as dupes even when they're not. Should use a sha1sum if you're paranoid.

Comment by befyber 21 weeks ago

Just a thot:

Sequence of detection

1. size

2. MAX=say 100Mb or 10 Mb

(a) Full MD5 for files

(b) MD5 only the first MAX bytes for files > MAX

e.g. MD5 (dd if=file of=/tmp/file count=200000)

3. Full MD5 or SHA1 for files found in 2(b)

---

Wud b nice for media collection.

Any takers??

Comment by Atanu 15 weeks and 6 days ago

Great one liner! I broke it up in order to understand it better:

https://gist.github.com/saltlakeryan/32ed0e82679f8615f38a

Also, added a file size filter using awk

Comment by saltlakeryan 3 weeks and 6 days ago

Your point of view

You must be signed in to comment.