Counting files in Linux is fairly easy by simply listing the files using ls and then using the wc application. Both the ls application and the wc application are core Linux applications and should be installed by default on your server. One thing that I had forgotten how to do was how to count all files in a specific directory as well as all files in each sub directories. The second method I will display below is not 100% accurate because it will count sub directories twice however it is likely you just need a round about method to get an idea of how many files and sub directories you are working with. Below I describe first how to count all files and directories within a single directory and second how to count all the files, sub directories, and files within the sub directories via a Linux shell or command prompt.
Count All Files & Directories In A Specific Directory On Linux:
To do this simply use the ls command piped to the wc command with the -l switch as shown in the below example. In this example we are counting all of the files in the top level of the WP Super Cache WordPress plugin directory.
- [root@dev plugins]# ls wp-super-cache/ | wc -l
- 13
- [root@dev plugins]#
As you can see there are 13 files and directories in the wp-super-cache directory which I show in the below output.
- [root@dev plugins]# ls wp-super-cache/
- advanced-cache.php ossdl-cdn.php uninstall.php wp-cache-phase1.php wp-super-cache.pot
- Changelog.txt plugins wp-cache-base.php wp-cache-phase2.php
- languages readme.txt wp-cache-config-sample.php wp-cache.php
- [root@dev plugins]#
Count All Files, Sub Directories, & Files In Sub Directories In A Specific Directory On Linux:
Now we want to count how many files, sub directories, and files in sub directories exist in the wp-super-cache directory. As noted in the beginning of the article this way is not 100% accurate because it actually counts sub directories twice as you will see in the below examples.
- [root@dev plugins]# ls -R wp-super-cache/ | wc -l
- 38
- [root@dev plugins]#
As you can see above the only difference between the first example and the one directly above is the fact that we are now using the -R ls switch which specifies ls to be recursive. Now below you can see that while it is not 100% accurate it is close enough to get an idea of how many files you are dealing with.
- [root@dev plugins]# ls -R wp-super-cache/
- wp-super-cache/:
- advanced-cache.php ossdl-cdn.php uninstall.php wp-cache-phase1.php wp-super-cache.pot
- Changelog.txt plugins wp-cache-base.php wp-cache-phase2.php
- languages readme.txt wp-cache-config-sample.php wp-cache.php
- wp-super-cache/languages:
- wp-super-cache-de_DE.mo wp-super-cache-fr_FR.mo wp-super-cache-ja.mo wp-super-cache-zh_CN.mo
- wp-super-cache-de_DE.po wp-super-cache-fr_FR.po wp-super-cache-ja.po wp-super-cache-zh_CN.po
- wp-super-cache-es_ES.mo wp-super-cache-it_IT.mo wp-super-cache-uk.mo wp-super-cache-zh_TW.mo
- wp-super-cache-es_ES.po wp-super-cache-it_IT.po wp-super-cache-uk.po wp-super-cache-zh_TW.po
- wp-super-cache/plugins:
- awaitingmoderation.php badbehaviour.php domain-mapping.php searchengine.php
- [root@dev plugins]#
Anyhow if you needed a quick and dirty way to figure out how many files you are dealing with this should do it. All of the above examples were performed on a CentOS 5.5 Linux server.
ls -1
Hola markie_mark,
Not sure I understand how the -1 helps out here. :) Care to let me in on your Linux foo… :P Hope all is well man… Any job openings for any US companies you have worked with? I am looking…
Thanks.
alex
-1 will list entries only once so will remove duplicates, as well as the ls header message – so will be a little more accurate with wc.
My old place is probably begging for resources, but I’m not sure that’s a good sign either.
Hello markie_mark,
Gotcha… thanks for that feedback man!
Yeah not sure that is a good sign either. :P I think I got something lined up today however if it doesn’t pan out I will be in touch and see about any connections you could provide. I appreciate the response and regardless of the J-O look forward to talking to you soon.
Did you check out DerbyCon.com? Pretty insane InfoSec Conference being started in Louisville. Really awesome speaker list already and the CFP just opened. If you want to come state side for it then you are welcome to stay with me as long as you are in town.
Thanks.
alex