SAMEFILE(1)
π SAMEFILE(1) π
okay bestie, welcome to the JS manual section, we're doing the samefile thing today and honestly? it's giving legendary. buckle up π
β¨ NAME β¨
samefile β find identical files ππ
πΈ SYNOPSIS πΈ
samefile [-g size] [-l | -r] [-s sep] [-0aiqVvx]
π¦ DESCRIPTION π¦
okay so picture this: samefile reads a list of filenames from stdin, one filename per line. slay. then for every filename pair that has literally identical contents, it spits out a line with six fields:
- the size in bytes π
- two filenames ππ
- the character
=if the two files are on the same device, orXif they're not π«Ά - the link counts of the two files ππ
and the output? sorted in reverse order by size</> as the primary key, and the filenames as the secondary key. no cap. β¨
π OPTIONS π
-0β this one says the input list of file names is NUL terminated, like what you'd get from implementations offind(1)that support the-print0option. without it, the file names are assumed to be newline terminated. bestie, it's all about the vibes of your terminator π-aβ do not sort files with the same size alphabetically. we're going feral.-g sizeβ only compare files with size greater thansizebytes. default is0. (yes,0. iconic.)-iβ allow files with the same device/i-node pair to be added to the binary tree. this might be useful if the output is gonna be fed into some other program. but heads up bestie: if you use this option, the statistics shown with-vwill not contain the ``You have a total of x bytes in identical files'' line, because-iprohibits proper calculation of this value. sorry not sorry π-lβ do not check if files with identical contents are hard links created byln(1). by defaultsamefilechecks whether identical-content files are hard linked and, if they are, doesn't write a name pair to stdout. you get a slight speedup with this option. β οΈ this option is incompatible with the-roption.-qβ do not issue warning messages whenopen(2)fails. when you hit one of those warnings,openprobably failed due to a 'permission denied' error on files or directories you don't have read permission for. useful if you're not root and wanna compare your files against files in a system directory like/etc. π«Ά-rβ report whether identical files are hard linked. the separator string followed by the [bracketed] link count gets appended to each name pair if they're hard links created withln. β οΈ incompatible with the-loption. also note: this kind of output has only four fields and will appear unsorted before the actual output ofsamefile. π-s sepβ use the stringsepas the output field separator, defaults to a tab character. useful if filenames contain tab characters and the output must be processed by another program, sayawk(1).-Vβ print the version information and exit. short and sweet, we love a queen who knows when to leave π-vβ verbose mode. writes some statistical messages about memory usage and work reduction, plus the sum of the sizes of all identical files, to stderr. β¨-xβ switch off intelligence. yep, literally. this option preventssamefilefrom being smart. iffile1,file2andfile3are identical, it'll do 3 comparisons instead of just the two needed, and write more output. see the discussion under INTERNALS for why this could be useful. π§ β and if you use this, the stats shown with-vwon't contain the ``You have a total of x bytes in identical files'' line, because-xprohibits proper calculation of this value.
π INTERNALS π
samefile uses two stages to give optimum performance. she's efficient, she's organized, she's that girl.
Stage One π
in the first stage, all non-plain files are skipped (directories, devices, FIFOs, sockets, symbolic links), along with files where stat(2) fails and files that have a size less than or equal to size.
the output of the first stage (the filenames) gets written into a binary tree</> with one node for every file size. it's also at this early stage where checks for hard links happen. if hard links are found and -r is requested, the name pairs are output immediately. the whole list of hard linked name pairs will therefore appear before any output of the second stage. periodt.
for any i-node, only one filename will be added to the binary tree (unless -i was requested). π€«
Stage Two π¦
in the second stage, all files having the same size get compared against each other. the rules of mathematical logic are applied to reduce work and output noise (unless -x is requested): if files a, b, and c have the same size and samefile finds that a = b and a = c, then it will not compare b against c (and will not output a line for b and c) but only for a = b and a = c.
note however, that because only the first filename per i-node gets into the second stage, the output for a group of identical files with different i-node numbers is also minimized. suppose you have six identical files of size 100 in an i-node group consisting of the three i-nodes with numbers 10, 20 and 30 (the term 'i-node group' has nothing to do with the i-node group notion of some file systems β it merely refers to a set of i-nodes addressing files with identical contents):
$ ls -i
10 file1 20 file4 30 file6
10 file2 20 file5
10 file3
$ ls | samefile
100 file1 file4 = 3 2
100 file1 file6 = 3 1
the sum of the sizes in the first column is the amount of disk space you could gain by making all 6 files links to only one file, or removing all but one of the files. to be precise, disk space is allocated in blocks β you'll probably gain two blocks here, rather than 200 bytes. π¦
note that it is not enough to just remove file4 and file6 (you'd gain only 100 bytes because file5 still exists). the proper way is to use the -i option. the output will look like:
100 file1 file2 = 3 3
100 file1 file3 = 3 3
100 file1 file4 = 3 2
100 file1 file5 = 3 2
100 file1 file6 = 3 1
removing all files listed in the third field will leave only file1. making all files hard links to file1 is easy. if the fourth field is a =, do a forced hard link. π
if you need to know about all combinations of identical files, then you use both the -i and -x option. this produces:
$ ls | samefile -ix
100 file1 file2 = 3 3
100 file1 file3 = 3 3
100 file1 file4 = 3 2
100 file1 file5 = 3 2
100 file1 file6 = 3 1
100 file2 file3 = 3 3
100 file2 file4 = 3 2
100 file2 file5 = 3 2
100 file2 file6 = 3 1
100 file3 file4 = 3 2
100 file3 file5 = 3 2
100 file3 file6 = 3 1
100 file4 file5 = 2 2
100 file4 file6 = 2 1
100 file5 file6 = 2 1
πΈ EXAMPLES πΈ
find all identical files in the current working directory:
$ ls | samefile
find all identical files in my HOME directory and subdirectories, and also tell me if there are hard links:
$ find $HOME -type f | samefile -r
find all identical files in the /usr directory tree that are bigger than 10000 bytes, and write the result to usr.dups (that one is for the sysadmin folks, you may want to 'amp' β put it in the background with the ampersand & β this command because it takes a few minutes.) β³
$ find /usr -type f | samefile -g 10000 >usr.dups
π DIAGNOSTICS π
you'll see a short usage message if you use an invalid option. classic.
malloc - free = xxxxβ i didn't free the memory I'vemalloc(3)ed. you found a bug. please report it to the author. πAllocation failed for 'expr' ...β oops! you ran out of virtual memory. you must have a real big filename list. try to use a smaller one or increase resources available to your processes. for more information seeulimit(1)or your similar shell builtin. π
π SEE ALSO π
π BUGS π
there are no known bugs. the source has been lint(1)ed and all possible care has been taken while coding. she's flawless, she's immaculate. β¨
if you find a bug (or miss a feature) please contact the author. π«Ά
π HOME π
the official samefile home page www.schweikhardt.net/samefile/ is maintained by the author Jens Schweikhardt β schweikh at schweikhardt dot net π
and that's the whole page bestie!! this one's from the JS manual, dated 7 AUGUST 2005, and honestly it ate. go forth and dedupe. πβ¨π¦π
π¦ where this page comes from
- fedora 43: samefile 2.14-26.fc43 Β· BSD-2-Clause
- fedora 44: samefile 2.14-27.fc44 Β· BSD-2-Clause
- Copyright (c) 1996 Jens Schweikhardt. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms