Skip to content
Advertisement

print the count of files for each sub folder iterativly

I have the following folder structure:

JavaScript

Similarly,

JavaScript

Now, each folder from 00 to 23 has many files inside, which I would like to count.

If I run this simple command:

ls /A/B/C/D/E/00 | wc -l I can get the count of files in each of these sub directories. I want to automate this or get it iteratively. Also, the final output I am looking at is a file that should look like this:

JavaScript

So, the output should look like this finally

JavaScript

Please note, the values after the alphabets are the values of file counts of the 24 folders 00, 01 through 23.

Using the eval approach: I can hardcode and get the exact results. But, I wanted it in a way that would show me the data for the previous day. So this is what I did:

JavaScript

This works perfectly because in the given location there are files inside child directories 20150800,20150801..20150823. However when I try to generalize this like below, it shows no such file or directory:

JavaScript

Is there something I am missing in the above line?

Advertisement

Answer

A very safe way of counting files:

JavaScript

To not count recursively add -maxdepth 1 before -exec.

Some other notes:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement