YouTube Icon

Code Playground.

How to Count Files in Directory in Linux

CFG

How to Count Files in Directory in Linux

Despite the fact that not regularly, there are times when you have to discover the number of documents are in a given index. For instance, in the event that you run out of inodes on your Linux framework, you'll have to discover which index contains thousands or a large number of records. 

In this article, we will show you a few unique approaches to locate the quantity of documents in a registry in Linux. 

Count Files in Directory

The easiest method to include records in a catalog is to show one document for every line with ls and line the yield to wc to tally the lines: 

ls -1U DIR_NAME | wc -l

The order above will give you a whole, everything being equal, including catalogs and symlinks. The - 1 choice methods show one record for each line and - U advises ls to don't sort the yield which makes the execution of the order quicker. 

ls - 1U order doesn't check shrouded documents (dotfiles). 

In the event that you need to check just documents and exclude the registries utilize the accompanying: 

ls -1Up DIR_NAME | grep -v / | wc -l

The - p choice powers ls to attach slice (/) marker to registries. The yield is channeled to the grep - v order that avoid the indexes. 

To have more power over what records are recorded, utilize the discover order rather than ls: 

find DIR_NAME -maxdepth 1 -type f | wc -l

- type f choice advises find to list just documents (counting dotfiles), and - maxdepth 1 limit search to the principal level catalog. 

Recursively Count Files in Directory

To recursively include records in index run the discover order as follows: 

find DIR_NAME -type f | wc -l

Another order that can be utilized to tally documents is tree that rundowns substance of catalogs in a tree-like configuration: 

tree DIR_NAME

The last line of yield will show the all out number of documents and catalogs recorded: 

15144 directories, 91311 files

Conclusion

We have told you the best way to include documents in index utilizing the ls, find and tree orders. 

On the off chance that you have any inquiries or criticism, don't hesitate to leave a remark.




CFG