<HTML ><HEAD ><TITLE >Total Bytes in the Current Directory</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Bash Prompt HOWTO" HREF="index.html"><LINK REL="UP" TITLE="Prompt Code Snippets" HREF="c679.html"><LINK REL="PREVIOUS" TITLE="Counting Files in the Current Directory" HREF="x700.html"><LINK REL="NEXT" TITLE="Checking the Current TTY" HREF="x721.html"></HEAD ><BODY CLASS="SECT1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Bash Prompt HOWTO: </TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="x700.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 11. Prompt Code Snippets</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="x721.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="AEN711" ></A >11.4. Total Bytes in the Current Directory</H1 ><P >If you want to know how much space the contents of the current directory take up, you can use something like the following:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="PROGRAMLISTING" >let TotalBytes=0 for Bytes in $(ls -l | grep "^-" | awk '{ print $5 }') do let TotalBytes=$TotalBytes+$Bytes done # The if...fi's give a more specific output in byte, kilobyte, megabyte, # and gigabyte if [ $TotalBytes -lt 1024 ]; then TotalSize=$(echo -e "scale=3 \n$TotalBytes \nquit" | bc) suffix="b" elif [ $TotalBytes -lt 1048576 ]; then TotalSize=$(echo -e "scale=3 \n$TotalBytes/1024 \nquit" | bc) suffix="kb" elif [ $TotalBytes -lt 1073741824 ]; then TotalSize=$(echo -e "scale=3 \n$TotalBytes/1048576 \nquit" | bc) suffix="Mb" else TotalSize=$(echo -e "scale=3 \n$TotalBytes/1073741824 \nquit" | bc) suffix="Gb" fi echo -n "${TotalSize}${suffix}" </PRE ></FONT ></TD ></TR ></TABLE ><P >Code courtesy of me, Sam Schmit (<TT CLASS="EMAIL" ><<A HREF="mailto:id at pt dot lu" >id at pt dot lu</A >></TT >), and Sam's uncle Jean-Paul, who ironed out a fairly major bug in my original code, and just generally cleaned it up. </P ><P >Note that you could also just use <TT CLASS="USERINPUT" ><B >ls -l | grep ^total | awk '{ print $2 }'</B ></TT > because <TT CLASS="USERINPUT" ><B >ls -l</B ></TT > prints out a line at the beginning that is the approximate size of the directory in kilobytes - although for reasons unknown to me, it seems to be less accurate (but obviously faster) than the above script. </P ><P >Relative speed: this process takes between 3.2 and 5.8 seconds in /usr/bin/ (14.7 meg in the directory) on an unloaded 486SX25, depending on how much of the information is cached (if you use this in a prompt, more or less of it will be cached depending how long you work in the directory). </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="x700.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="x721.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Counting Files in the Current Directory</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="c679.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Checking the Current TTY</TD ></TR ></TABLE ></DIV ></BODY ></HTML >