LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-12-2012, 12:33 AM   #1
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Rep: Reputation: Disabled
help with counting files in subdirectories


how can i count number of files in sub directories?
for file in `ls $dir/*`
do
i=$((i+1))
done

its not working. any help would be appreciated.
Thanks
 
Old 10-12-2012, 12:36 AM   #2
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,672

Rep: Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956
How about just
Code:
ls $dir/* | wc -l
 
Old 10-12-2012, 12:42 AM   #3
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
nope not working . giving wrong answer.
 
Old 10-12-2012, 12:43 AM   #4
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,672

Rep: Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956
Could you provide an example?
 
Old 10-12-2012, 12:45 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,247

Rep: Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025
Code:
ls $dir | wc -l
 
Old 10-12-2012, 12:47 AM   #6
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
like in my A directory it has 1 file, in B - 2 and in C - 3 . Then how can i print total num of files in this directories?
 
Old 10-12-2012, 12:52 AM   #7
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
@chris:

that will just count the directories. not the files in directories.
 
Old 10-12-2012, 12:52 AM   #8
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,672

Rep: Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956
Do you want to include the directories themselves (A, B, C), or just the files? Do these directories contain subdirectories with more files? If so, how deep do you want to go? Do you want the number of files itemized by directory, or just one number for all files in all directories?

---------- Post added 10-11-12 at 11:53 PM ----------

Quote:
Originally Posted by NickPats View Post
@chris:

that will just count the directories. not the files in directories.
Doing an "ls" on a directory will print all of the files contained within the directory, unless you pass the "-d" flag to ls.
 
Old 10-12-2012, 12:56 AM   #9
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
just the files in that directories. total num of files in those directories. and those directories don't have subdirectories. just files. the example that i gave you. in that example ans should be 6.
 
Old 10-12-2012, 12:58 AM   #10
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,672

Rep: Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956
Code:
ls {A,B,C}/* | wc -l
or
Code:
find {A,B,C} -type f | wc -l
If A, B, and C were just example directories, and you simply want to search the contents of all directories in the PWD:

Code:
ls */* | wc -l
Code:
find . -type f | wc -l

Last edited by suicidaleggroll; 10-12-2012 at 01:01 AM.
 
1 members found this post helpful.
Old 10-12-2012, 01:03 AM   #11
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
Yeah thanks. That was easy. Thanks a lot.
 
Old 10-12-2012, 01:38 AM   #12
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
What if i want to print the directory and how many files does that directory contain...
 
Old 10-12-2012, 10:02 AM   #13
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,672

Rep: Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956Reputation: 956
That's when the first few examples would come into play
Code:
for dir in *; do
   echo $dir: $(ls "$dir" | wc -l)
done
This could be combined on one line if you don't want to write a script for it
Code:
for dir in *; do echo $dir: $(ls "$dir" | wc -l); done
 
Old 10-12-2012, 11:07 AM   #14
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
Suppose you want to count all files within a directory /home/smith/, then do as:
% vi count.sh
And enter following lines:

#!/bin/sh

DIR=/home/smith/
CMD=`ls -la $DIR | wc -l`
echo "Files count is: $CMD"


Then save and exit the file. Make it executable i.e. invoke command % chmod +x count.sh
and then run the script as:
./count.sh

Last edited by shivaa; 10-12-2012 at 11:09 AM.
 
Old 10-12-2012, 11:44 AM   #15
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
Thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Renaming files in subdirectories J2O1988 Linux - Newbie 4 02-13-2010 01:39 AM
[Bash] Counting files paul_l Programming 4 09-04-2009 05:04 PM
listing subdirectories files ramesh_manu Red Hat 2 02-18-2007 01:46 PM
counting number of files akin81 Linux - Newbie 6 03-25-2004 01:53 PM
delete files / keep subdirectories? lhorstman Linux - Newbie 2 01-10-2003 03:01 PM


All times are GMT -5. The time now is 08:37 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration