Tux

...making Linux just a little more fun!

Two-cent Tip: How big is that directory?

Dr. Parthasarathy S [drpartha at gmail.com]


Tue, 2 Feb 2010 09:57:02 +0530

At times, you may need to know exactly how big is a certain directory (say top directory) along with all its contents and subdirectories(and their contents). You may need this if you are copying a large diectory along with its contents and structure. And you may like to know if what you got after the copy, is what you sent. Or you may need this when trying to copy stuff on to a device where the space is limited. So you want to make sure that you can accomodate the material you are planning to send.

Here is a cute little script. Calling sequence::

howmuch <top directory name>

You get a summary, which gives the total size, the number of subdirectories, and the number of files (counted from the top directory). Good for book-keeping.

###########start-howmuch-script
# Tells you how many files, subdirectories and content bytes in a
# directory
# Usage :: how much <directory-path-and-name>
 
# check if there is no command line argument
if [ $# -eq 0 ]
then
echo "You forgot the directory to be accounted for !"
echo "Usage :: howmuch <directoryname with path>"
exit
fi
 
echo "***start-howmuch***"
pwd > ~/howmuch.rep
pwd
echo -n "Disk usage of directory ::" > ~/howmuch.rep
echo $1 >> ~/howmuch.rep
echo -n "made on ::" >> ~/howmuch.rep
du -s $1 > ~/howmuch1
tree $1 > ~/howmuch2
date >> ~/howmuch.rep
tail ~/howmuch1 >> ~/howmuch.rep
tail --lines=1 ~/howmuch2 >> ~/howmuch.rep
cat ~/howmuch.rep
# cleanup
rm ~/howmuch1
rm ~/howmuch2
#Optional -- you can delete howmuch.rep if you want
#rm ~/howmuch.rep
 
echo "***end-howmuch***"
#   
 
 
########end-howmuch-script
-- 
---------------------------------------------------------------------------------------------
Dr. S. Parthasarathy                    |   mailto:[email protected]
Algologic Research & Solutions    |
78 Sancharpuri Colony                 |
Bowenpally  P.O                          |   Phone: + 91 - 40 - 2775 1650
Secunderabad 500 011 - INDIA     |
WWW-URL: http://algolog.tripod.com/nupartha.htm
---------------------------------------------------------------------------------------------


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Wed, 3 Feb 2010 22:04:25 -0500

Hi, Partha -

On Tue, Feb 02, 2010 at 09:57:02AM +0530, Dr. Parthasarathy S wrote:

> 
> I hope you will find the enclosed submission worthwhile for LG. Please let me
> know as soon as it gets published, or if it is not worth publishing in LG.
> Thank you.

Pretty much anything sent to TAG, other than flames and spam, is fodder for discussion and publication; that's what we're all about. Now, as to the script itself -

###########start-howmuch-script
# Tells you how many files, subdirectories and content bytes in a
# directory
# Usage :: how much <directory-path-and-name>
 
# check if there is no command line argument
if [ $# -eq 0 ]
then
echo "You forgot the directory to be accounted for !"
echo "Usage :: howmuch <directoryname with path>"
exit
fi
 
echo "***start-howmuch***"
pwd > ~/howmuch.rep
pwd
echo -n "Disk usage of directory ::" > ~/howmuch.rep
echo $1 >> ~/howmuch.rep
echo -n "made on ::" >> ~/howmuch.rep
du -s $1 > ~/howmuch1
tree $1 > ~/howmuch2
date >> ~/howmuch.rep
tail ~/howmuch1 >> ~/howmuch.rep
tail --lines=1 ~/howmuch2 >> ~/howmuch.rep
cat ~/howmuch.rep
# cleanup
rm ~/howmuch1
rm ~/howmuch2
#Optional -- you can delete howmuch.rep if you want
#rm ~/howmuch.rep
 
echo "***end-howmuch***"
#   
 
########end-howmuch-script

One of the standard practices in shell scripting is to stay away from temporary files unless they're necessary (e.g., if you need to use a program that only takes files as input.) What would happen, for example, if you already had a file called 'howmuch.rep' in that directory? For example, if you had run this script for, say, the 'foo' directory yesterday, forgot about it, and wanted to get the results for the 'bar' directory today? The first file would be gone - and you wouldn't know anything about it until you wanted the data.

This is why the standard practice is to construct every program as a filter - that is, arrange it so that it takes data in, transforms it, and outputs it to STDOUT. What this mostly means with regard to coding is using pipes instead of temporary files. For example (I'm going to make an explicit effort to replicate your script's output here):

#!/bin/bash
# Created by Ben Okopnik on Wed Feb  3 21:57:05 EST 2010
 
[ -d "$1" ] || { printf "Usage: ${0##*/} <directory>\n"; exit; }
 
pwd
echo -e "Disk usage of directory ::$1\nmade on ::`date`"
du -sk "$1"
# You could use 'tree "$1"|sed -n "$p"' - or stick with the standard toolkit
ls -lR "$1"|awk '/^\//{d++};/^-/{f++}END{print d-1" directories, "f" files"}'

This will do essentially the same thing as your script, but without any temp files. The output can be saved to a specified file simply by redirecting it, or it can be further filtered/modified by piping it to another program.

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Dr. Parthasarathy S [drpartha at gmail.com]


Thu, 4 Feb 2010 10:47:22 +0530

YES I agree. Your script is better than mine. If you authorise me, I will resubmit my stuff using your script (after I add a comment line acknowledging your contribution). Or I can just use the script for my internal usage and not submit for publication.

I thank you for your frank opinion and CONSTRUCTIVE criticism. That is how we all learn.

partha

-- 
---------------------------------------------------------------------------------------------
Dr. S. Parthasarathy                    |   mailto:[email protected]
Algologic Research & Solutions    |
78 Sancharpuri Colony                 |
Bowenpally  P.O                          |   Phone: + 91 - 40 - 2775 1650
Secunderabad 500 011 - INDIA     |
WWW-URL: http://algolog.tripod.com/nupartha.htm
---------------------------------------------------------------------------------------------


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Thu, 4 Feb 2010 00:56:09 -0500

On Thu, Feb 04, 2010 at 10:47:22AM +0530, Dr. Parthasarathy S wrote:

> YES I agree. Your script is better than mine.

Oh-oh. Context error warning! :)

Partha, this wasn't any sort of competition; as an example of what happens here, I've just sent in a Two-cent tip about using 'wget' to download files based on directory listings - and I fully expect that someone here will tell me that I could have done all that by using the "--download-all-the-files-in-the-directory-listing" option or something like that. :) The thing is, though, that it wouldn't be a question of "better" or "worse": both methods are useful. E.g., I'd learn about the option from the follow-up post; someone else might learn about Perl regular expressions, which I used in my tip, from my post.

My overall point is that we share this information, these ideas, with the Linux community - and all of us get to learn from all the information in the exchange. So, my answer isn't necessarily "better" than yours; yours may well be more useful for someone else's purposes.

> If you authorise me, I will
> resubmit my stuff using your script (after I add a comment line acknowledging
> your contribution). Or I can just use the script for my internal usage and not
> submit for publication.

There's nothing to resubmit, or authorize: TAG is open to our readers for the exact purpose of hosting this kind of discussion. As I mentioned the last time, all TAG content, minus flames and spam, gets published in the Mailbag section of LG - so readers get to see the whole exchange.

> I thank you for your frank opinion and CONSTRUCTIVE criticism. That is how we
> all learn.

You're welcome! And you're right - this is indeed one of the best ways to learn. TAG was one of the keystones of my own learning process about Linux, especially in the early days - and it still remains a useful tool that keeps up my skills, among other things.

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Dr. Parthasarathy S [drpartha at gmail.com]


Thu, 4 Feb 2010 17:53:04 +0530

Although I do not show up too often on TAG, believe me I read every issue of LG and of course Two-cent tips.

I do use these ideas often both for myself and for teaching.

Thanks for all the great job you are doing.

partha

-- 
---------------------------------------------------------------------------------------------
Dr. S. Parthasarathy                    |   mailto:[email protected]
Algologic Research & Solutions    |
78 Sancharpuri Colony                 |
Bowenpally  P.O                          |   Phone: + 91 - 40 - 2775 1650
Secunderabad 500 011 - INDIA     |
WWW-URL: http://algolog.tripod.com/nupartha.htm
---------------------------------------------------------------------------------------------


Top    Back