You are not logged in.

#1 2005-06-23 18:49:23

GuyonAsm
Member
Registered: 2004-05-25
Posts: 49

Console File Size Program

Yes of course, I could just do an ls -hl, but I just want to see the filesizes only (and the names next to it to distinguish). I wonder if anyone else finds it useful tongue

(You can name it whatever you want, but my binary name is "fsize")

#include <stdio.h>

int main(int argc, char *argv[])
{
    FILE *f;
    int g, m, k, b;
    int i, total;

    for(i = 1; i < argc; i++)
    {
        f = fopen(argv[i], "rb");
        if(f == 0)
        {
            printf("the file %s does not existn", argv[i]);
            continue;
        }

        fseek(f, 0, SEEK_END);
        total = ftell(f);
        fclose(f);

        g = total / 1073741824;
        total = total % 1073741824;
        m = total / 1048576;
        total = total % 1048576;
        k = total / 1024;
        total = total % 1024;
        b = total;

        printf( "%uGt%uMt%uKt%ut- %sn", g, m, k, b, argv[i]);
    }
    return 0;
}

Output looks like:

[danielm@localhost fsize]$ fsize *
0G      0M      0K      0       - README
0G      0M      5K      194     - fsize
0G      0M      0K      543     - fsize.c
0G      0M      0K      0       - fsize.sh

Offline

#2 2005-06-24 03:37:26

celeron2002
Member
From: Santiago, Chile.
Registered: 2004-02-18
Posts: 150

Re: Console File Size Program

Also exist the "du" command. tongue

du -mahc *
4,0K    CVS/Root
4,0K    CVS/Repository
4,0K    CVS/Entries
12K     CVS
12K     total

irc.bsd.cl #linux
irc.freenode.org #archlinux-es

Offline

#3 2005-06-24 04:59:58

codergeek42
Member
From: Anaheim, CA (USA)
Registered: 2005-06-03
Posts: 90
Website

Re: Console File Size Program

$ stat /path/to/file  | grep Size | awk -F " " '{print $2;}'

wink


~Peter~

Offline

#4 2005-06-24 11:33:45

GuyonAsm
Member
Registered: 2004-05-25
Posts: 49

Re: Console File Size Program

codergeek42 wrote:
$ stat /path/to/file  | grep Size | awk -F " " '{print $2;}'

wink

Awww so you're telling me I could have just created an alias to that? Either way it was only 5 mins of coding smile.

Offline

Board footer

Powered by FluxBB