Skip to content

Commit

Permalink
Add human readable size option
Browse files Browse the repository at this point in the history
With size option, fdupes shows size of duplicate files in bytes which is not
always easy to read for a human. This new human-size option reports sizes in a
more readable format (B, KiB, MiB,...).
  • Loading branch information
Jérémy Lefaure authored and Jérémy Lefaure committed Nov 12, 2019
1 parent 3a9b2b6 commit 6bb71c5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Usage: fdupes [options] DIRECTORY...
-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
-T --human-size show size of duplicate files in human readable format\n");
-t --time show modification time of duplicate files
-m --summarize summarize dupe information
-q --quiet hide progress indicator
Expand Down
3 changes: 3 additions & 0 deletions fdupes.1
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ list each set of matches on a single line
.B -S --size
show size of duplicate files
.TP
.B -T --human-size
show size of duplicate files in human readable format
.TP
.B -t --time
show modification time of duplicate files
.TP
Expand Down
25 changes: 24 additions & 1 deletion fdupes.c
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,17 @@ void summarizematches(file_t *files)
}
}

static void printhumansize(off_t size)
{
unsigned int i = 0;
const char *const symbols[] = {"", "Ki", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"};
while (size > 1024 && i < (sizeof(symbols) / sizeof(*symbols) - 1)) {
size /= 1024;
i++;
}
printf("%lld%sB each:\n", (long long int)size, symbols[i]);
}

void printmatches(file_t *files)
{
file_t *tmpfile;
Expand All @@ -794,6 +805,7 @@ void printmatches(file_t *files)
if (!ISFLAG(flags, F_OMITFIRST)) {
if (ISFLAG(flags, F_SHOWSIZE)) printf("%lld byte%seach:\n", (long long int)files->size,
(files->size != 1) ? "s " : " ");
else if (ISFLAG(flags, F_SHOWSIZE_HUMAN)) printhumansize(files->size);
if (ISFLAG(flags, F_SHOWTIME))
printf("%s ", fmtmtime(files->d_name));
if (ISFLAG(flags, F_DSAMELINE)) escapefilename("\\ ", &files->d_name);
Expand Down Expand Up @@ -971,6 +983,7 @@ void deletefiles(file_t *files, int prompt, FILE *tty, char *logfile)
curgroup, groups, counter);
if (ISFLAG(flags, F_SHOWSIZE)) printf(" (%lld byte%seach)", (long long int)files->size,
(files->size != 1) ? "s " : " ");
else if (ISFLAG(flags, F_SHOWSIZE_HUMAN)) printhumansize(files->size);
printf(": ");
fflush(stdout);

Expand Down Expand Up @@ -1223,6 +1236,7 @@ void help_text()
printf(" -f --omitfirst \tomit the first file in each set of matches\n");
printf(" -1 --sameline \tlist each set of matches on a single line\n");
printf(" -S --size \tshow size of duplicate files\n");
printf(" -T --human-size \tshow size of duplicate files in human readable format\n");
printf(" -t --time \tshow modification time of duplicate files\n");
printf(" -m --summarize \tsummarize dupe information\n");
printf(" -q --quiet \thide progress indicator\n");
Expand Down Expand Up @@ -1283,6 +1297,7 @@ int main(int argc, char **argv) {
{ "quiet", 0, 0, 'q' },
{ "sameline", 0, 0, '1' },
{ "size", 0, 0, 'S' },
{ "human-size", 0, 0, 'T' },
{ "time", 0, 0, 't' },
{ "symlinks", 0, 0, 's' },
{ "hardlinks", 0, 0, 'H' },
Expand Down Expand Up @@ -1315,7 +1330,7 @@ int main(int argc, char **argv) {

oldargv = cloneargs(argc, argv);

while ((opt = GETOPT(argc, argv, "frRq1StsHG:L:nAdPvhNImpo:il:"
while ((opt = GETOPT(argc, argv, "frRq1STtsHG:L:nAdPvhNImpo:il:"
#ifdef HAVE_GETOPT_H
, long_options, NULL
#endif
Expand All @@ -1339,6 +1354,9 @@ int main(int argc, char **argv) {
case 'S':
SETFLAG(flags, F_SHOWSIZE);
break;
case 'T':
SETFLAG(flags, F_SHOWSIZE_HUMAN);
break;
case 't':
SETFLAG(flags, F_SHOWTIME);
break;
Expand Down Expand Up @@ -1450,6 +1468,11 @@ int main(int argc, char **argv) {
exit(1);
}

if (ISFLAG(flags, F_SHOWSIZE) && ISFLAG(flags, F_SHOWSIZE_HUMAN)) {
errormsg("options --size and --human-size are not compatible\n");
exit(1);
}

if (ISFLAG(flags, F_RECURSEAFTER)) {
firstrecurse = nonoptafter("--recurse:", argc, oldargv, argv, optind);

Expand Down
3 changes: 2 additions & 1 deletion flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
#define F_IMMEDIATE 0x8000
#define F_PLAINPROMPT 0x10000
#define F_SHOWTIME 0x20000
#define F_SHOWSIZE_HUMAN 0x40000

extern unsigned long flags;

#endif
#endif

0 comments on commit 6bb71c5

Please sign in to comment.