Skip to content

Commit

Permalink
Allow to specify minimum and maximum file size (fixes #83)
Browse files Browse the repository at this point in the history
  • Loading branch information
DEVoytas committed Nov 29, 2018
1 parent fdf2f5e commit ae6ae78
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Usage: fdupes [options] DIRECTORY...
modification time (BY='time'; default), status
change time (BY='ctime'), or filename (BY='name')
-i --reverse reverse order while sorting
-G --minsize=SIZE consider only files greater then or equal to SIZE
-L --maxsize=SIZE consider only files of size less or equal to SIZE
-v --version display fdupes version
-h --help display this help message

Expand Down
6 changes: 6 additions & 0 deletions fdupes.1
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ time - sort by modification time, ctime - sort by status change time, name - sor
.B -i --reverse
reverse order while sorting
.TP
.B -G --minsize\fR=\fISIZE\fR
consider only files greater then or equal to SIZE in bytes
.TP
.B -L --maxsize\fR=\fISIZE\fR
consider only files of size less or equal to SIZE in bytes
.TP
.B -v --version
display fdupes version
.TP
Expand Down
27 changes: 25 additions & 2 deletions fdupes.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#ifndef OMIT_GETOPT_LONG
#include <getopt.h>
#endif
Expand Down Expand Up @@ -66,6 +67,8 @@ char *program_name;
unsigned long flags = 0;

ordertype_t ordertype = ORDER_MTIME;
size_t min_size = 0;
size_t max_size = SIZE_MAX;

#define CHUNK_SIZE 8192

Expand Down Expand Up @@ -262,6 +265,7 @@ int grokdir(char *dir, file_t **filelistp)
static int progress = 0;
static char indicator[] = "-\\|/";
char *fullname, *name;
off_t fsize;

cd = opendir(dir);

Expand Down Expand Up @@ -318,7 +322,9 @@ int grokdir(char *dir, file_t **filelistp)
free(fullname);
}

if (filesize(newfile->d_name) == 0 && ISFLAG(flags, F_EXCLUDEEMPTY)) {
fsize = filesize(newfile->d_name);

if (fsize == 0 && ISFLAG(flags, F_EXCLUDEEMPTY)) {
free(newfile->d_name);
free(newfile);
continue;
Expand All @@ -336,6 +342,13 @@ int grokdir(char *dir, file_t **filelistp)
continue;
}

if (S_ISREG(linfo.st_mode) &&
((fsize < min_size ) || (fsize > max_size ))) {
free(newfile->d_name);
free(newfile);
continue;
}

if (S_ISDIR(info.st_mode)) {
if (ISFLAG(flags, F_RECURSE) && (ISFLAG(flags, F_FOLLOWLINKS) || !S_ISLNK(linfo.st_mode)))
filecount += grokdir(newfile->d_name, filelistp);
Expand Down Expand Up @@ -1063,6 +1076,8 @@ void help_text()
printf(" \tmodification time (BY='time'; default), status\n");
printf(" \tchange time (BY='ctime'), or filename (BY='name')\n");
printf(" -i --reverse \treverse order while sorting\n");
printf(" -G --minsize \tconsider only files greater then or equal to SIZE\n");
printf(" -L --maxsize \tconsider only files of size less or equal to SIZE\n");
printf(" -v --version \tdisplay fdupes version\n");
printf(" -h --help \tdisplay this help message\n\n");
#ifdef OMIT_GETOPT_LONG
Expand Down Expand Up @@ -1110,6 +1125,8 @@ int main(int argc, char **argv) {
{ "permissions", 0, 0, 'p' },
{ "order", 1, 0, 'o' },
{ "reverse", 0, 0, 'i' },
{ "minsize", 1, 0, 'G' },
{ "maxsize", 1, 0, 'L' },
{ 0, 0, 0, 0 }
};
#define GETOPT getopt_long
Expand All @@ -1121,7 +1138,7 @@ int main(int argc, char **argv) {

oldargv = cloneargs(argc, argv);

while ((opt = GETOPT(argc, argv, "frRq1SsHlnAdvhNImpo:i"
while ((opt = GETOPT(argc, argv, "frRq1SsHlnAdvhNImpo:iG:L:"
#ifndef OMIT_GETOPT_LONG
, long_options, NULL
#endif
Expand Down Expand Up @@ -1193,6 +1210,12 @@ int main(int argc, char **argv) {
case 'i':
SETFLAG(flags, F_REVERSE);
break;
case 'G':
min_size = atol(optarg);
break;
case 'L':
max_size = atol(optarg);
break;

default:
fprintf(stderr, "Try `fdupes --help' for more information.\n");
Expand Down

0 comments on commit ae6ae78

Please sign in to comment.