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 |
How about just
Code:
ls $dir/* | wc -l |
nope not working . giving wrong answer.
|
Could you provide an example?
|
Code:
ls $dir | wc -l |
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?
|
@chris:
that will just count the directories. not the files in directories. |
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:
|
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.
|
Code:
ls {A,B,C}/* | wc -l Code:
find {A,B,C} -type f | wc -l Code:
ls */* | wc -l Code:
find . -type f | wc -l |
Yeah thanks. That was easy. Thanks a lot.
|
What if i want to print the directory and how many files does that directory contain...
|
That's when the first few examples would come into play
Code:
for dir in *; do Code:
for dir in *; do echo $dir: $(ls "$dir" | wc -l); done |
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 |
Thanks
|
All times are GMT -5. The time now is 05:43 AM. |