Linux

Ways to Count Files Within Directory in Linux

[ad_1]

Have you ever racked your brain for a way that you can count the number of files within a directory and sub directories in Linux?

In this brief article, we’ll look at how you can do that in a simple yet accurate way.

1) Using wc command

WC command, short for Word Count, is a command line tool in Unix/Linux systems used for printing newlines, counting number lines & characters in a file. The command can also be combined with other piping operations for general counting functions.

To count the number of files in a directory, use the syntax below

Let us break down the Syntax and see what does what

ls : Lists the files
-1  : This is a ONE. It prints one entry per line. To print hidden files, change it to -1a
|    : Pipes output into…
wc  : Wordcount
-l  : Counts the lines

To count the number of files outside the directory, you can specify the directory name as shown

To demonstrate how this commands works, let’s create a new directory test_folder, and navigate into it.

Next, we are going to create a few text files for  demonstration purposes

Let us confirm the existence of the files by listing them  using the ls command

Output

Count Number of Files Within a Directory

Great, now let’s  count the files

Output

Count Number of Files Within a Directory

Similarly, you can exit the directory using cd command

And run

Output

Count Number of Files Within a Directory

2) Using tree command

tree  is a Unix/Linux command line tool that recursively prints directories in a tree-like format. It displays each directory along with any subdirectories within it. In addition, it can also display and print the number of files in a directory.

Navigate into our sample test_folder

Then, run the tree command

Output

As you can see, we the files names alongside the count that appears at the bottom is listed. Clearly, through observation, the count matches exactly the number of files created earlier on.

4) Rsync Command

We can use rsync command to find the number of files of files, directories, symlinks.

--dry-run is used not actually transfer the files. /test should be any empty or non existing folder.

3) Count files recursively through directories and Subdirectories

To recursively count files through directories and subdirectories using the command below

Where:

DIR_NAME is the directory name

– type f specifies files only

wc (Word Count) counts newlines, words, and bytes on its input

-l  Counts new lines

If you are counting files in the current directory replace the DIR_NAME with a period as shown

To include other subfolders and files within subfolders, leave out the - type f flag.

I hope that this article has been helpful and that now you can comfortably count the number of files within directories and subdirectories. Also, try ncdu tool which is a GUI tool which also helps to find the file count.

[ad_2]

Facebook Comments

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button