Script to count number of files in directories


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to count number of files in directories
# 1  
Script to count number of files in directories

Hi All!

I would like to have a script that will count the number of files at the top of the hour of soome directories and mail the results to me.
I was thinking on :

Code:
a=`/directory/subdirectory/ | wc -l`
echo "/directory/subdirectory :$a"
b=`/another_dir/subdir/ | wc -l`
echo "/another_dir/subdir :$b"

I need to have a
Code:
while

, and I would like to have this results every hour of everyday.
# 2  
Instead of a while, can you use cron? It is usually more efficient than having a resident process sleeping.

Code:
#!/bin/ksh
# usage.shl
for dir in /directory1/subdir directory2/subdir
do
echo "$dir $( du $dir |wc -l)" 
done > ./message
mailx -s 'dir usage' me@mycompany.com < ./message

Using crontab

Code:
# five minutes after every hour
5 * * * * *  /path/to/usage.shl 2 > /path/to/usage.log

no crontab
Code:
#!/bin/ksh
# usage.shl

while :
do
 for dir in /directory1/subdir directory2/subdir
 do
   echo "$dir $( du $dir |wc -l)" 
 done > ./message
 mailx -s 'dir usage' me@mycompany.com < ./message
 sleep 3600
done

This execution time will drift over time because sleep is not guranteed to "wake up" precisely because of system load.
# 3  
Script to count number of files in directories

Hi

Thanks for your quick reply, but your script is giving me the following results:

Code:
 /directory1/subdirectory $( du
 /directory2/subdirectory  |wc -l)

# 4  
Did you include
Code:
#!/bin/ksh

as the very first line? - I would guess "no". That will fix the problem.

or you can change
Code:
echo "$dir $( du $dir |wc -l)"

to
Code:
echo "$dir `du $dir |wc -l`"

-- those are backticks
If you cannot do that for some reason then:
You are running what shell on what UNIX os?
# 5  
Script to count number of files in directories

Hi

I have used
Code:
#!/bin/bash

do I have to use korn shell?

---------- Post updated 13-11-12 at 08:30 AM ---------- Previous update was 12-11-12 at 05:28 PM ----------

Hi

I am running bash shell on solaris 10

Code:
 echo $SHELL
/bin/bash

---------- Post updated at 03:16 PM ---------- Previous update was at 08:30 AM ----------

any suggestions please:
# 6  
You can use bash. But those errors are caused by the shell being /bin/sh - a Bourne shell

When crond runs a job in Solaris, by default, it starts the job with /bin/sh. If you do not change that behavior you have to code in Bourne shell. Since you got that error you cannot have copied the original code with the shebang I gave you. The original invoked ksh. use
Code:
#!/bin/bash

if you want.
# 7  
Script to count number of files in directories

Hi

its not working as it should be, because its giving me single digit values, but in reality those directory have more that 2 digit figures
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #528
Difficulty: Medium
The Extended ASCII character set adds support for 128 additional characters (numbered 128 through 255) by adding 1 more bit (16 total).
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count files between multiple directories

Hi All, Everyday we will receive 33 files in our source directory(/prd/pk) with the current date. Once our jobs are completed all the 33 files immediately will be moved to backup folder (/prd/pk/backup). Now, I need to check between source file directory (/prd/pdk) and backup file directory... (3 Replies)
Discussion started by: suresh_target
3 Replies

2. Shell Programming and Scripting

List directories and count files inside

I'm trying to make a script that will list all directories under a selection as well as the number of files in each. I cannot get it to work under a symbolic link. The file structure is: XXX_20131127_001 dir01 (sym link) 2404x912 file.0000.xxx to ... (10 Replies)
Discussion started by: scribling
10 Replies

3. Shell Programming and Scripting

How to count number of files in directory and write to new file with number of files and their name?

Hi! I just want to count number of files in a directory, and write to new text file, with number of files and their name output should look like this,, assume that below one is a new file created by script Number of files in directory = 25 1. a.txt 2. abc.txt 3. asd.dat... (20 Replies)
Discussion started by: Akshay Hegde
20 Replies

4. UNIX for Dummies Questions & Answers

Number of files in all (sub-)directories

Hi, I need a list of the number of files in all (sub-)directories e.g.: /a/b/c 1364 /a/b 125 /a 362 etc. Should be nice to have the list sorted from high to low. Regards, Wim (6 Replies)
Discussion started by: deleriumdog
6 Replies

5. UNIX for Dummies Questions & Answers

Count of files and directories

Hi I have a jfs2 filesystem named /software in my aix 5.3 box. Please note that there are a lot of subdirectories under /software? I need to know the count of how many total files and directories are present under that mount point /software ? For example if by some commands we find that there... (3 Replies)
Discussion started by: samsungsamsung
3 Replies

6. Shell Programming and Scripting

Need script to count specific word and iterate over number of files.

Hi Experts, I want to know the count of specific word in a file. I have almost 600+ files. So I want to loop thru each file and get the count of the specific word. Please help me on achieving this... Many thanks (2 Replies)
Discussion started by: elamurugu
2 Replies

7. Shell Programming and Scripting

perl script on how to count the total number of lines of all the files under a directory

how to count the total number of lines of all the files under a directory using perl script.. I mean if I have 10 files under a directory then I want to count the total number of lines of all the 10 files contain. Please help me in writing a perl script on this. (5 Replies)
Discussion started by: adityam
5 Replies

8. Shell Programming and Scripting

Count of files in directories

Hi, I have a requirement to find out the count of files in directories. I can do this very well by goind to each directory and then ls -lrt | wc -l. But I need to do it for hundreds of directories/sub-directories. I tried with this - for i in `ls -F | grep '/$'`; do `echo "$i"`| ls -lrt... (2 Replies)
Discussion started by: unx100
2 Replies

9. Shell Programming and Scripting

AWK Script - Count Files In Directories

Hey, I'm very new to AWK and am trying to write a script that counts the number of files in all subdirectories. So, basically, my root has many subdirectories, and each subdirectory has many files. How can I get the total count? I haven't been able to figure out how to loop through the... (1 Reply)
Discussion started by: beefeater267
1 Replies

10. Solaris

Get Count of all files in all the directories

Hello, I am looking for a way to get the TOTAL COUNT of the files present in all directories(sub directories) under the root directory..The files can be of any type viz. txt, doc, html, wav, jpeg etc. If it has an extension, it has to be counted.. I want to run the script from the root directory.... (6 Replies)
Discussion started by: oniondosa
6 Replies

Featured Tech Videos