Goal: Count Number of Files in a Directory

Difficulty: Easy

Prerequisites: Root Login Access

Often times you want to get an accurate count of the number of files in a specific linux directory/folder. So here's come code to do just that

Note: This will give you the count of files and folders of the directory/folder you are already in.

This will not include hidden files and will be 1 more than the number of files you actually have. So if you have 3 files the output will be 4. Here it goes:

ls -l | wc -l

 

Now there are times when you really want to see all hidden files and folders in addition to your normal ones.

ls -la | wc -l

 

We can even take this one step further and do searches on our file information. For example if you wanted to count all files that are read/write for owner only you could use this: (Keeping in mind to subtract 1 from the returned numbers also don't forget to escape your dashes.)

ls -la | grep "\-rw\-\-\-\-\-\-\-"| wc -l

 

Also, if you want to find all files and folders recursively in a directory you can use the following command:

find DIR_NAME -type f -print| wc -l

Comments

Leave a Reply



(Your email will not be publicly displayed.)



Search