LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   help with counting files in subdirectories (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-counting-files-in-subdirectories-4175431790/)

NickPats 10-12-2012 12:33 AM

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

suicidaleggroll 10-12-2012 12:36 AM

How about just
Code:

ls $dir/* | wc -l

NickPats 10-12-2012 12:42 AM

nope not working . giving wrong answer.

suicidaleggroll 10-12-2012 12:43 AM

Could you provide an example?

chrism01 10-12-2012 12:45 AM

Code:

ls $dir | wc -l

NickPats 10-12-2012 12:47 AM

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?

NickPats 10-12-2012 12:52 AM

@chris:

that will just count the directories. not the files in directories.

suicidaleggroll 10-12-2012 12:52 AM

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 (Post 4803595)
@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.

NickPats 10-12-2012 12:56 AM

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.

suicidaleggroll 10-12-2012 12:58 AM

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

NickPats 10-12-2012 01:03 AM

Yeah thanks. That was easy. Thanks a lot.

NickPats 10-12-2012 01:38 AM

What if i want to print the directory and how many files does that directory contain...

suicidaleggroll 10-12-2012 10:02 AM

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

shivaa 10-12-2012 11:07 AM

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

NickPats 10-12-2012 11:44 AM

Thanks


All times are GMT -5. The time now is 05:43 AM.