https://github.com/xuejianbest/tools/blob/master/src/com/lwt/tools/FileUtil.java
另外介绍个Linux开源工具:fdupes
https://github.com/adrianlopezroche/fdupes
sudo apt install fdupes
centos 6:
https://centos.pkgs.org/6/epel-x86_64/fdupes-1.6.1-1.el6.x86_64.rpm.html
centos 7:
https://centos.pkgs.org/7/epel-x86_64/fdupes-1.6.1-1.el7.x86_64.rpm.html
从以上网址下载rpm包,用以下命令安装:
rpm -ivh fdupes*.rpm
列出目录列表中目录(不包括子目录)包含的重复文件:
fdupes dir1 dir2
同上,-r
参数表明包含dir2子目录中的文件(子目录和祖先目录中文件相同也算重复):
fdupes dir1 -r dir2
-d
参数,删除重复文件,具体保留哪个会让用户选择:
fdupes -rd dir
-N
参数,删除重复文件并自动保留第一个,不让用户选择:
fdupes -rdN dir
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
-I --immediate delete duplicates as they are encountered, without
grouping into sets; implies --noprompt
-p --permissions don't consider files with different owner/group or
permission bits as duplicates
-o --order=BY select sort order for output, linking and deleting; by
mtime (BY='time'; default) or filename (BY='name')
-i --reverse reverse order while sorting
-v --version display fdupes version
-h --help display this help message