Skip to content

Commit

Permalink
add an option to sort duplicate files by name
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Brüns <[email protected]>
  • Loading branch information
StefanBruens authored and jobermayr committed May 16, 2014
1 parent c62a533 commit 12cefd5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions fdupes.1
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ set of duplicates and delete the others without prompting the user
.B -p --permissions
don't consider files with different owner/group or permission bits as duplicates
.TP
.B -o --order\fR=\fIWORD\fR
order files according to WORD:
time - sort by mtime, name - sort by filename
.TP
.B -v --version
display fdupes version
.TP
Expand Down
30 changes: 28 additions & 2 deletions fdupes.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
#define F_EXCLUDEHIDDEN 0x1000
#define F_PERMISSIONS 0x2000

typedef enum {
ORDER_TIME = 0,
ORDER_NAME
} ordertype_t;

char *program_name;

unsigned long flags = 0;
Expand Down Expand Up @@ -921,6 +926,11 @@ int sort_pairs_by_mtime(file_t *f1, file_t *f2)
return 0;
}

int sort_pairs_by_filename(file_t *f1, file_t *f2)
{
return strcmp(f1->d_name, f2->d_name);
}

void registerpair(file_t **matchlist, file_t *newmatch,
int (*comparef)(file_t *f1, file_t *f2))
{
Expand Down Expand Up @@ -998,6 +1008,9 @@ void help_text()
printf(" \teach set of duplicates and delete the rest without\n");
printf(" \tprompting the user\n");
printf(" -p --permissions \tdon't consider files with different owner/group or permission bits as duplicates\n");
printf(" -o --order \tselect sort order for output, linking and deleting. One of:\n");
printf(" time \torder by mtime (default)\n");
printf(" name \torder by filename\n");
printf(" -v --version \tdisplay fdupes version\n");
printf(" -h --help \tdisplay this help message\n\n");
#ifdef OMIT_GETOPT_LONG
Expand All @@ -1018,6 +1031,7 @@ int main(int argc, char **argv) {
int progress = 0;
char **oldargv;
int firstrecurse;
ordertype_t ordertype = ORDER_TIME;

#ifndef OMIT_GETOPT_LONG
static struct option long_options[] =
Expand All @@ -1042,6 +1056,7 @@ int main(int argc, char **argv) {
{ "summarize", 0, 0, 'm'},
{ "summary", 0, 0, 'm' },
{ "permissions", 0, 0, 'p' },
{ "order", 1, 0, 'o' },
{ 0, 0, 0, 0 }
};
#define GETOPT getopt_long
Expand All @@ -1053,7 +1068,7 @@ int main(int argc, char **argv) {

oldargv = cloneargs(argc, argv);

while ((opt = GETOPT(argc, argv, "frRq1Ss::HlnAdvhNmp"
while ((opt = GETOPT(argc, argv, "frRq1SsHlndvhNmpo:"
#ifndef OMIT_GETOPT_LONG
, long_options, NULL
#endif
Expand Down Expand Up @@ -1107,6 +1122,16 @@ int main(int argc, char **argv) {
case 'p':
SETFLAG(flags, F_PERMISSIONS);
break;
case 'o':
if (!strcasecmp("name", optarg)) {
ordertype = ORDER_NAME;
} else if (!strcasecmp("time", optarg)) {
ordertype = ORDER_TIME;
} else {
errormsg("invalid value for --order: '%s'\n", optarg);
exit(1);
}
break;

default:
fprintf(stderr, "Try `fdupes --help' for more information.\n");
Expand Down Expand Up @@ -1182,7 +1207,8 @@ int main(int argc, char **argv) {
}

if (confirmmatch(file1, file2)) {
registerpair(match, curfile, sort_pairs_by_mtime);
registerpair(match, curfile,
(ordertype == ORDER_TIME) ? sort_pairs_by_mtime : sort_pairs_by_filename );

/*match->hasdupes = 1;
curfile->duplicates = match->duplicates;
Expand Down

0 comments on commit 12cefd5

Please sign in to comment.