The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
List files that are not directories from current directory beni22sof UNIX for Dummies Questions & Answers 2 01-06-2009 03:44 PM
Viewing Directory Content as You Navigate Directories in UNIX shelata UNIX for Dummies Questions & Answers 2 07-28-2008 01:39 AM
how to grep some text in a directory and as well as in its sub-directories itsjoy2u Shell Programming and Scripting 4 04-29-2008 09:25 AM
How one can list only the name of directories present in the directory by using ls co amolpatil54321 UNIX for Dummies Questions & Answers 2 02-01-2004 12:38 PM
list file's by size order in sepecfied directory and sub directories ferretman UNIX for Dummies Questions & Answers 2 01-03-2002 07:55 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-22-2009
smongam smongam is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 2
how many directories and files are there in a directory

I want to know how many directories and files are there in a directory and if the sub directory have any files i also need that also .
I have done this far ....
Quote:
df=`ls -1 | wc -l`
f=`ls -l | grep -v "^d" | wc -l | bc`
tf=`expr $f - 1`
td=`expr $df - $tf`
  #2 (permalink)  
Old 01-22-2009
jaduks's Avatar
jaduks jaduks is offline
Registered User
  
 

Join Date: Aug 2007
Location: Assam,India
Posts: 166
Use find with -type f or d and then use wc ...
  #3 (permalink)  
Old 01-22-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by smongam View Post
I want to know how many directories and files are there in a directory and if the sub directory have any files i also need that also .
I have done this far ....
Code:
df=`ls -1 | wc -l`
f=`ls -l | grep -v "^d" | wc -l | bc`
tf=`expr $f - 1`
td=`expr $df - $tf`

No external commands are necessary:

Code:
set -- *
numfiles=$#
set -- */
numdirs=$#

printf "%13s: %d\n" Files "$(( $numfiles - $numdirs ))" Directories "$numdirs"
EDIT: I missed the requirement for counting subdiretories.

Code:
numfiles=$( find "$DIR" -type f | wc -l )
numdirs=$( find "$DIR" -type d | wc -l )
printf "%13s: %d\n" Files "$numfiles" Directories "$numdirs"

Last edited by cfajohnson; 01-22-2009 at 02:18 PM..
  #4 (permalink)  
Old 01-22-2009
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,163
Code:
One way is to walk the tree.  First pass finds all the directories,
then we count all the files and directories under each directory.
Grossly inefficient on a deep tree ... but short to code.

#!/bin/ksh
(
find . -type d -print|while read SUBDIR
do
        COUNTER=$(find "${SUBDIR}" -print|wc -l)
        echo "${SUBDIR} : ${COUNTER}"
done
) 2>&1 | pg

Last edited by methyl; 01-22-2009 at 02:15 PM.. Reason: too wide!
  #5 (permalink)  
Old 01-22-2009
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,163
cfajohnson and myself are placing different interpretation on the phrase "directories and files". Which is right?
On this forum an example of input and expected output is useful.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0