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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-12-2012, 12:33 AM
|
#1
|
|
Member
Registered: Oct 2012
Posts: 43
Rep: 
|
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
|
|
|
|
10-12-2012, 12:36 AM
|
#2
|
|
Senior Member
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 1,648
|
|
|
|
|
10-12-2012, 12:42 AM
|
#3
|
|
Member
Registered: Oct 2012
Posts: 43
Original Poster
Rep: 
|
nope not working . giving wrong answer.
|
|
|
|
10-12-2012, 12:43 AM
|
#4
|
|
Senior Member
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 1,648
|
Could you provide an example?
|
|
|
|
10-12-2012, 12:45 AM
|
#5
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 14,938
|
|
|
|
|
10-12-2012, 12:47 AM
|
#6
|
|
Member
Registered: Oct 2012
Posts: 43
Original Poster
Rep: 
|
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?
|
|
|
|
10-12-2012, 12:52 AM
|
#7
|
|
Member
Registered: Oct 2012
Posts: 43
Original Poster
Rep: 
|
@chris:
that will just count the directories. not the files in directories.
|
|
|
|
10-12-2012, 12:52 AM
|
#8
|
|
Senior Member
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 1,648
|
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
@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.
|
|
|
|
10-12-2012, 12:56 AM
|
#9
|
|
Member
Registered: Oct 2012
Posts: 43
Original Poster
Rep: 
|
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.
|
|
|
|
10-12-2012, 12:58 AM
|
#10
|
|
Senior Member
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 1,648
|
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:
find . -type f | wc -l
Last edited by suicidaleggroll; 10-12-2012 at 01:01 AM.
|
|
|
1 members found this post helpful.
|
10-12-2012, 01:03 AM
|
#11
|
|
Member
Registered: Oct 2012
Posts: 43
Original Poster
Rep: 
|
Yeah thanks. That was easy. Thanks a lot.
|
|
|
|
10-12-2012, 01:38 AM
|
#12
|
|
Member
Registered: Oct 2012
Posts: 43
Original Poster
Rep: 
|
What if i want to print the directory and how many files does that directory contain...
|
|
|
|
10-12-2012, 10:02 AM
|
#13
|
|
Senior Member
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 1,648
|
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
|
|
|
|
10-12-2012, 11:07 AM
|
#14
|
|
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,612
|
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.
|
|
|
|
10-12-2012, 11:44 AM
|
#15
|
|
Member
Registered: Oct 2012
Posts: 43
Original Poster
Rep: 
|
Thanks
|
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:15 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|