From a0b7fb219b8e5203ae9884871c61b7f064e45797 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Br=C3=BCns?= Date: Tue, 29 Apr 2014 19:12:48 +0200 Subject: [PATCH] add an option to sort duplicate files by name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stefan BrĂ¼ns --- fdupes.1 | 4 ++++ fdupes.c | 30 ++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/fdupes.1 b/fdupes.1 index e0516f1..b5fb0f6 100644 --- a/fdupes.1 +++ b/fdupes.1 @@ -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 diff --git a/fdupes.c b/fdupes.c index b6aeaa7..08f9e2c 100644 --- a/fdupes.c +++ b/fdupes.c @@ -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; @@ -918,6 +923,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)) { @@ -995,6 +1005,9 @@ void help_text() printf(" \teach set of duplicates and delete the rest without\n"); printf(" \twithout prompting 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 @@ -1015,6 +1028,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[] = @@ -1039,6 +1053,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 @@ -1050,7 +1065,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 @@ -1104,6 +1119,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"); @@ -1179,7 +1204,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; -- 1.8.4.5