How to get a file size from the shell



  • There are many ways to get the file size from the shell in Unix. I like LEN=cat file | wc -c.   Others do it this way:

     

    IINFO=`ls -l filename`
    I=0
    for FI in ${IINFO}
    do
        case "$I" in
        4)
            LEN=$FI
            ;;
        esac
    
    I=$((I+1))
    

    done

     



  •  TRWTF is `cat file | wc -c`.  At least the above method doesn't have to pipe the entire contents of a file just to get the size.  Your hard drive would cry out in pain every time you wanted to just get the size of a few video files.



  • @corey said:

    TRWTF is `cat file | wc -c
    Yeah. What's wrong with <font face="courier new,courier">ls -l | cut -d" " -f5</font>?



  • @corey said:

    TRWTF is `cat file | wc -c`. 

    I have to agree there... stat dwh | grep "Size:" |  awk '{ printf("%s\n",$2); }'



  • @snoofle said:

    stat dwh | grep "Size:" |  awk '{ printf("%s\n",$2); }'
     

    Don't want to be pedantic, and most likely this is COMPLETELY dependant on what system you are running, but:

    stat dwh -c %s

    Saving two extra external command invocations is always nice.

    I'm sure bash has a hidden built-in somewhere too.



  •  @snoofle said:

    @corey said:

    TRWTF is `cat file | wc -c`. 

    I have to agree there... stat dwh | grep "Size:" |  awk '{ printf("%s\n",$2); }'

    But do they return the same value? `wc` returns number of bytes, `stat` returs file size - with sparse files, those numbers may differ. There's of course also `du` and `ls` - just to keep people entertained ;)



  • All these are still better than having a user take a screenshot from the file properties in windows and then using some OCR program to parse the file size out.



  • @corey said:

    TRWTF is `cat file | wc -c`.  At least the above method doesn't have to pipe the entire contents of a file just to get the size.

    The compiler will just optimize that out. 



  • @Zecc said:

    @corey said:

    TRWTF is `cat file | wc -c
    Yeah. What's wrong with <FONT face="courier new,courier">ls -l | cut -d" " -f5</FONT>?

    I tried all of the following:

    cat file | wc -c
    ls -l file | cut -d" " -f6 (yes, it's the sixth field)
    stat file  | grep "Size:" | awk '{ printf("%s\n",$2);}'
    

    and they all give the same results on tiny and large files that I tried (Linux). 'ls | cut' is the least expensive...



  • Let me join in!

    SIZE=$(du -b filename)

    SIZE=${SIZE/[[:space:]]*}



  • du -k file (most Unices)

    du file (Linux uses 1024-blocks by default)

    Of course, these return 1024-block counts, so it is kind of an approximate.



  • It's too inefficient to let the shell copy STDOUT into a variable:

    [code]perl -e "exit -s shift" FILE[/code]

    No need for clunky backtick commands, that sticks it right into your $? variable (which has the nice memonic "What's the file's size? " unlike LEN which is just a nonsense word).

    You just need to watch out for those disk-wasting persons.  256-bytes should be good enough for anything.


Log in to reply