Script that sums the contents of a folder (help me)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script that sums the contents of a folder (help me)
# 15  
Old 09-02-2014
if [ -z "$1" ] ; then and if [ $# -lt 1 ]; then are basically the same.

I added the required extensions for you, hope you will like it. It's rather a quick & dirty hack than how it should be done in a professional manner. I mean it should be done using getopts.
Code:
#!/bin/bash

function usage {
echo
echo " Usage: `basename $0` [-d] directory [directory2] [directory3]"
echo
echo " Given a directory, this script estimates its file space usage,"
echo " separately counts files and directories in it."
echo
echo " -d        show subfolders of specified folder(s)"
echo " -h, --help    show this usage notice"
echo;exit
}

if [ -z "$1" ] ; then 
    usage
fi

case "$1" in
    -h|--help)    usage;;
    -d)        if [ -z "$2" ]; then
            usage
            fi
            shift
            dswitch=1;;
esac

for dirname in "$@"
do
if [ -d "$dirname" ]; then
 echo "CHECKING $dirname..."
 echo -n "SIZE: "
 du -s "$dirname"
 echo -n "FILES: "
 find "$dirname" -type f | wc -l
 echo -n "DIRECTORIES: "
 find "$dirname" -type d | wc -l

 if [[ $dswitch -eq 1 ]]; then
 find "$dirname" -type d
 fi
 echo
else
 echo "Directory \"$dirname\" does not exist!"
 echo
fi
done

# 16  
Old 09-02-2014
Quote:
Originally Posted by junior-helper
if [ -z "$1" ] ; then and if [ $# -lt 1 ]; then are basically the same.

I added the required extensions for you, hope you will like it. It's rather a quick & dirty hack than how it should be done in a professional manner. I mean it should be done using getopts.
Code:
#!/bin/bash

function usage {
echo
echo " Usage: `basename $0` [-d] directory [directory2] [directory3]"
echo
echo " Given a directory, this script estimates its file space usage,"
echo " separately counts files and directories in it."
echo
echo " -d        show subfolders of specified folder(s)"
echo " -h, --help    show this usage notice"
echo;exit
}

if [ -z "$1" ] ; then 
    usage
fi

case "$1" in
    -h|--help)    usage;;
    -d)        if [ -z "$2" ]; then
            usage
            fi
            shift
            dswitch=1;;
esac

for dirname in "$@"
do
if [ -d "$dirname" ]; then
 echo "CHECKING $dirname..."
 echo -n "SIZE: "
 du -s "$dirname"
 echo -n "FILES: "
 find "$dirname" -type f | wc -l
 echo -n "DIRECTORIES: "
 find "$dirname" -type d | wc -l

 if [[ $dswitch -eq 1 ]]; then
 find "$dirname" -type d
 fi
 echo
else
 echo "Directory \"$dirname\" does not exist!"
 echo
fi
done

Thank you !! Now can i try implement other parameters Smilie

---------- Post updated at 11:56 AM ---------- Previous update was at 11:48 AM ----------

Quote:
Originally Posted by Roggy
Thank you !! Now can i try implement other parameters Smilie


i have done it to implement other parameters but i have one question that I not understand in the script what means and what does dswitch , I don't understand it because it is my first time I do bash scripting
# 17  
Old 09-02-2014
dswitch=1 literally means that value 1 is assigned to the variable dswitch, but I used that variable name to "remember" that the script was started with the "-d" option. One could interpret it as "script started with -d option?"="YES".
It's related to the second if statement in the for loop, which decides if all subfolders of a specific directory need to be shown too or not.
# 18  
Old 09-03-2014
Quote:
Originally Posted by junior-helper
dswitch=1 literally means that value 1 is assigned to the variable dswitch, but I used that variable name to "remember" that the script was started with the "-d" option. One could interpret it as "script started with -d option?"="YES".
It's related to the second if statement in the for loop, which decides if all subfolders of a specific directory need to be shown too or not.


Thank you very much, but what must I change for give one parameter ?
I want to give ./test1.sh Desktop only first and then I want to give two directories ./test1.sh Documents Desktop and then the parameters I implement -d -f

Is this the line I have to changed ? Into if [$# -lt 1 ] ?
Code:
if [ -z "$1" ]; then 
usage
fi

# 19  
Old 09-03-2014
There have been a couple of suggestions saying that you should use getopts to parse options (and I STRONGLY agree). Unfortunately, as far as I know, getopts in bash can't handle long options. Although it isn't often described in the man pages, getopts in many version of the Korn shell works well with both short and long options as long as there is a single letter equivalent for each long option. The following is a rewrite of your script using ksh instead of bash and using getopts instead of discrete option parsing. This doesn't make a lot of difference in your code since you only have one option that can be logically used at a time; but it does give you the ability to use -- as an option terminator in case you have a directory operand that starts with a -, and, if you add more options later, groups of short options that don't take option arguments can be combined into a single argument to your script (the same as allowed by standard utilities).

After parsing options this script will print the number of operands remaining, list them, and then process them as directory operands.

You haven't said what OS you're using, so this might not work on your system, but it is worth a try...
Code:
#!/bin/ksh
IAm=${0##*/}
usage() {
printf '
SYNOPSIS:
    %s [-d] directory...
    %s -h

DESCRIPTIN:
    For each directory operand given, estimate the amount of disk space
    used by the file hierarchy rooted in that directory, and count the
    number of regular files and directories in that hierarchy.

OPTIONS:
    -a, --subdir	Also list subdirectories of directory operands.
    -h, --help		Display this help notice.

OPERANDS:
    directory	The pathname of a direcotry to be processed.

EXIT STATUS:
    0	Successful completion.
    1	Unexpected options found.
    2	No operands found.
    3	One or more directory operands were not found.

' "$IAm" "$IAm"
}

# Parse options...
dswitch=0
err=0
while getopts -a "$IAm" "d(subdir)h(help)" opt
do	case $opt in
	(d)	dswitch=1;;
	(h)	usage
		exit 0;;
	(?)	err=1;;
	esac
done
# Shift away arguments used for options
shift $((OPTIND - 1))

# If no bad options were found, verify that we have at least one operand...
if [ $err -eq 0 ] && [ $# -lt 1 ]
then	printf '%s: No operands provided\n' "$IAm" >&2
	err=2
fi

# If we had bad optoins or do not have any operands, print help message as a
# diagnostic and exit.
if [ $err -gt 0 ]
then	usage >&2
	exit $err
fi

# Print operands remaining after option parsing...
printf '%s: %d operands remain after option parsing:\n' "$IAm" $#
printf '\t"%s"\n' "$@"
echo

# Process directory operands...
for dirname in "$@"
do	# Be sure directory named by an operand is present...
	if [ -d "$dirname" ]
	then	# The directory is present...
		printf '"CHECKING DIRECTORY: "%s"...\n' "$dirname"
		printf 'SIZE: '
		du -s "$dirname"
		printf 'REGULAR FILES: '
		find "$dirname" -type f | wc -l
		printf 'DIRECTORIES: '
		find "$dirname" -type d | wc -l

		# If -d (or --subdir) option was given, list subdirectories...
		if [ $dswitch -eq 1 ]
		then	find "$dirname" -type d
		fi
		echo
	else	# The directory is not present...
		printf 'Directory "%s" does not exist!\n\n' "$dirname" >&2

		# Set non-zero exit code for when we have finished processing
		# any remaining directory operands.
		err=2
	fi
done
exit $err

When invoked as:
Code:
sumcontent.sh --help
      or
sumcontent.sh -h

it produces the output:
Code:
SYNOPSIS:
    sumcontent.sh [-d] directory...
    sumcontent.sh -h

DESCRIPTIN:
    For each directory operand given, estimate the amount of disk space
    used by the file hierarchy rooted in that directory, and count the
    number of regular files and directories in that hierarchy.

OPTIONS:
    -a, --subdir	Also list subdirectories of directory operands.
    -h, --help		Display this help notice.

OPERANDS:
    directory	The pathname of a direcotry to be processed.

EXIT STATUS:
    0	Successful completion.
    1	Unexpected options found.
    2	No operands found.
    3	One or more directory operands were not found.

on standard output and exits with exit code 0.

When invoked as:
Code:
sumcontent.sh -d $HOME/dl .
      or
sumcontent.sh --subdir $HOME/dl .
      or
sumcontent.sh -d -- $HOME/dl .

on my system, it currently produces the output:
Code:
sumcontent.sh: 2 operands remain after option parsing:
	"/Users/dwc/dl"
	"."

"CHECKING DIRECTORY: "/Users/dwc/dl"...
SIZE: 158192	/Users/dwc/dl
REGULAR FILES:      239
DIRECTORIES:        3
/Users/dwc/dl
/Users/dwc/dl/pdf
/Users/dwc/dl/spreadsheets

"CHECKING DIRECTORY: "."...
SIZE: 16	.
REGULAR FILES:        3
DIRECTORIES:        1
.

And, if invoked as:
Code:
sumcontent.sh -subdir --hat $HOME/dl

it produces the output:
Code:
sumcontent.sh: -s: unknown option
sumcontent.sh: -u: unknown option
sumcontent.sh: -b: unknown option
sumcontent.sh: -i: unknown option
sumcontent.sh: -r: unknown option
sumcontent.sh: --hat: unknown option

followed by the help screen (all written to standard error output instead of to standard output) and exits with exit code 1.
This User Gave Thanks to Don Cragun For This Post:
# 20  
Old 09-03-2014
Don if you dont mind I will consider your last post as a conclusion to this thread and close it. (We are way from requirement in the title now...)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Best way to move the contents of a folder to another one

what is the best way to move the contents of a folder to another one without deleting the structure of the first one. the contents could include subfolder too. both folder, the source-folder and the target-folder are on the same host. any idea is appreciated . (7 Replies)
Discussion started by: andy2000
7 Replies

2. Shell Programming and Scripting

Remove folder contents

for dir in BKP/*/ do echo You are in :$dir done O/P -- BKP/201448/ BKP/201449/ BKP/201450/ BKP/201451/ BKP/201452/ BKP/201501/ BKP/201502/ BKP/201503/ BKP/201504/ BKP/201505/ BKP/201506/ BKP/201507/ (3 Replies)
Discussion started by: rocking77
3 Replies

3. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

copy folder and its contents to another folder

Hi experts, I am coming to you with this basic question on copying a folder and its content from one location to another folder using PERL script. This is my requirement. I have a folder AB under /users/myhome I want to copy AB and its contents to /user/workspace. Finally it should... (1 Reply)
Discussion started by: amvarma77
1 Replies

5. Shell Programming and Scripting

Script to create a folder with contents

I am working on HP Unix. Require a script for the below requirement. Requirement are: 1. Need to create a folder with files. 2. The folder should have a naming convention like - LRIC_ARCHIVE_ddmmyyhhmmss_version_nnn, the version number needs to be selected from an oracle table. 3. When the... (4 Replies)
Discussion started by: Roadies99
4 Replies

6. Shell Programming and Scripting

Compare folder contents over network

I use diff -r dir1 dir2 to get comparison of two folders that are on same machine. Now I need the same thing but one of the folders is on a different machine. Currently I ftp the folder to a temp folder compare using above command and delete the temp folder. Is there any other better options?... (5 Replies)
Discussion started by: ke3kelly
5 Replies

7. Shell Programming and Scripting

do a full comparison of folder contents in script

Hello everyone.... I have a small issue here at work and I am trying to script out a way to automate a fix for it. I have a small number of users (I work in a 1:1 with 6,000 macbooks) that aren't really managed in my deployment. They are managed with a few policies, but the policies are broken... (2 Replies)
Discussion started by: tlarkin
2 Replies

8. UNIX for Dummies Questions & Answers

How to display contents of folder when 'cd' is used

Hi, I am a new learner of Unix. I am currently working on a Solaris 8 machine. Earlier, when I use 'cd <folder name>' command, I am not only able to change the folder but also able to see the contents of the folder as if a 'ls -lt' command was executed. However, since a week, suddenly this... (3 Replies)
Discussion started by: mumashankar
3 Replies

9. UNIX for Dummies Questions & Answers

copy folder contents

I need to make a new dir in side the dir lab5 the new dir is called testLab5 without changing directories copy all files from your lab5 directory into your testLab5 directory then i have to without chaning directories and using exactly one command remove all files that start with the... (1 Reply)
Discussion started by: robsk8_99
1 Replies

10. UNIX for Dummies Questions & Answers

Folder Contents

Hi, I'm trying to allow people to access the contents of a folder on a web site, I am automatically placing files in this folder for people to download. I'm using Apache on Mac OS X, if that makes a difference. Can anyone help with this? I've found no documentation on this so far... ... (6 Replies)
Discussion started by: spencer
6 Replies
Login or Register to Ask a Question