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)
# 1  
Old 08-30-2014
Script that sums the contents of a folder (help me)

I'm looking for a script that sums the contents of a folder,

When you give a parameter to the script , i want to know the size of the directory, the number of files, number of folders,

These are commands that I have already found
Code:
du -s
find . -type f | wc -l
find . -type d | wc -ly


Can anybody help me , I want to know how to start with this script

Last edited by Scrutinizer; 08-30-2014 at 10:23 AM.. Reason: code tags; spelling
# 2  
Old 08-30-2014
To determine the size of a directory, just simply use

Code:
du -sh /PATH/TO/DIRECTORY

If you want to list the sum of directories only you can use
Code:
ls -l | grep ^d | wc -l


And the same way to the sum of files but use "-" instead of "d", like

Code:
ls -l | grep ^- | wc -l

So you can save it in the top of your script variables.

Last edited by leo_ultra_leo; 08-30-2014 at 12:49 PM.. Reason: Correction
# 3  
Old 08-31-2014
with command find, use this, otherwise you count folder which begin with a dot

Code:
find . ! -name '\.' -type d | wc -l

The same for the files, it avoid to count the .DS_STORE (i work in mac OSX), you not constrain perhaps to use this

Code:
find . ! -name '\.*' -type f | wc -l

# 4  
Old 08-31-2014
I'm sorry but i'm not strong in writing a script.. Can you help me for how to start with this script , that i have something to start

---------- Post updated at 04:33 AM ---------- Previous update was at 04:03 AM ----------

Quote:
Originally Posted by protocomm
with command find, use this, otherwise you count folder which begin with a dot

Code:
find . ! -name '\.' -type d | wc -l

The same for the files, it avoid to count the .DS_STORE (i work in mac OSX), you not constrain perhaps to use this

Code:
find . ! -name '\.*' -type f | wc -l



i'm sorry but can you help me please for how to i get start with this script but i'm not strong in writting a script, i want to know how to start
# 5  
Old 08-31-2014
Hammer & Screwdriver

Here is a small demo which requires a directory name as parameter. It even accepts directory names containing whitespaces. The directory with whitespace needs to be quoted. Using your own commands, I guess you are after something like this:

sumcontent.sh
Code:
#!/bin/bash

if [ $# -ne 1 ]; then
 echo "no parameter/directory or too many provided."
 exit 1
fi

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

Demo:
Code:
$ ./sumcontent.sh 
no parameter/directory or too many provided.
$ ./sumcontent.sh test folder
no parameter/directory or too many provided.
$ ./sumcontent.sh "test folder"
SIZE: 12    test folder
FILES: 2
DIRECTORIES: 3
$ ./sumcontent.sh test
SIZE: 12    test
FILES: 8
DIRECTORIES: 1
$

Now you should be able to play with it and try other commands mentioned in this thread.

You may want to extract the size only (from the du -s output).
You may want to check if the provided directory exists before the commands run.
You may want to add the -maxdepth 1 option to the find commands.

Hope this helps.

Last edited by junior-helper; 08-31-2014 at 08:06 AM.. Reason: removed second unnecessary if statement
This User Gave Thanks to junior-helper For This Post:
# 6  
Old 08-31-2014
Quote:
Originally Posted by junior-helper
Here is a small demo which requires a directory name as parameter. It even accepts directory names containing whitespaces. The directory with whitespace needs to be quoted. Using your own commands, I guess you are after something like this:

sumcontent.sh
Code:
#!/bin/bash

if [ $# -ne 1 ]; then
 echo "no parameter/directory or too many provided."
 exit 1
fi

if [ $# -gt 1 ]; then
 echo "too many parameters/directories provided."
 exit 2
fi

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

Demo:
Code:
$ ./sumcontent.sh 
no parameter/directory or too many provided.
$ ./sumcontent.sh test folder
no parameter/directory or too many provided.
$ ./sumcontent.sh "test folder"
SIZE: 12    test folder
FILES: 2
DIRECTORIES: 3
$ ./sumcontent.sh test
SIZE: 12    test
FILES: 8
DIRECTORIES: 1
$

Now you should be able to play with it and try other commands mentioned in this thread.

You may want to extract the size only (from the du -s output).
You may want to check if the provided directory exists before the commands run.
You may want to add the -maxdepth 1 option to the find commands.

Hope this helps.
Thank to help me Smilie
# 7  
Old 08-31-2014
You're welcome Smilie
Please check my posting again, so you can remove that second IF statement, it is unnecessary.
Enjoy! Smilie
 
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