Count The Files In A Directory

I think I’ve covered this before with the ls command but this time we’ll count the files in a directory with the find command. This being Linux, you can usually count on there being multiple ways to accomplish something.

There are lots of tools out there that let you visualize disk usage. I’ve covered a bunch of them over the years. You can also count the files in a directory to see if there’s something abnormal going on. You may also have some sort of file limitations that mean you’re limited to a certain number of inodes and thus you need to react accordingly.

This is just one way to count the files in a directory. There are surely a variety of ways and we’re only going to cover counting the files with the ‘find’ command. 

In this case, you won’t need to install anything. The find command is a part of the GNU ‘findutils’ and will be included by default in any desktop (or server) you’ll interact with. You’ll also be using the wc command, again something we’ve used in the past, and that too will be installed by default because it is a part of the coreutils.

The find Command:

As I said, you won’t need to install anything to use the find command. You won’t need to install anything at all for this article. You can verify that you have the find command available with this command:

We’ll search for files (with the -type f flag) and then count them with the wc command. If you check the man page for find (with man find) you’ll see that find is a good tool for the job.

find – search for files in a directory hierarchy

We will pipe the output from the find command to the wc command to count files in a directory. You’ll see…

The wc Command:

The output from the find command would be a long list of files. Rather than view that list or anything like that, we’ll simply pipe that command to the wc command. You won’t need to install anything and you can verify that the wc command exists with this command:

You can tell right away that this is a fine tool for counting things. Just check the man page (with man wc) and you’ll see this:

wc – print newline, word, and byte counts for each file

So, you can tell right away that this is a great tool for counting things. In our case, we’ll be counting new lines because the output from the find command would be one file per line. See? It just makes sense!

Let’s get to it!

Count The Files In A Directory:

If the above information wasn’t much of a clue, this is something done in the terminal. Regular readers should be expecting this because we do a lot in the terminal. Open up a terminal (you can usually just CTRL + ALT + T) and we’ll get started.

The syntax is pretty simple…

You’re using the “-type f” flag. That outputs a new file on a new line. Try it with this command, if you’d like to see:

Then, pipe that to the wc command and use the -l flag. The -l flag counts lines, of course. So, each file is on a new line and the wc command counts those new lines instead of displaying them in the terminal.

I do have this in my notes…

If you were to run this against the root “ /” drive, you’d get permission errors. Try this command for example:

Well, you can redirect the output to /dev/null using 2>, like so:

I am not sure why that’s in my notes but it is. It strips out the permission-denied responses and that’s about it. You might want to see those so that you know what wasn’t necessarily counted. Of course, you can also run this command with elevated permission via sudo. So, don’t forget that.

Closure:

There you have it… If you want to count the files in a directory you can do that with the find and wc commands. That makes it relatively simple because you won’t need to install anything. This should just work for any Linux system you’re using. That makes it pretty universal and worth remembering.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

How To: Count Letters, Words, and Lines

Today’s article isn’t going to be all that interesting unless you happen to want to know how to count letters, words, and lines in a file. If you’re worried about publication length, a student, or perhaps a journalist, this might be the article for you.

For everyone else? Well, you can read this as a curiosity. It’s one of the neat things you can do with your Linux terminal, but probably not the most interesting of things. Still, you can probably find a way to have fun counting letters (characters, really), words, and lines in the Linux terminal. Who doesn’t want to have fun in the terminal?!?

The tool we’ll be using is ‘wc’ and it will almost certainly be installed by default. The wc application will be installed by default on some of the smallest Linux distros. It’s a tool that does this:

wc – print newline, word, and byte counts for each file

That happily sums up why we’ll be using wc in this article’s exercise. It is the most appropriate tool for the job, installed by default, and remarkably easy to use. If you want to count a file’s letters, words, and lines, this is the tool for you. You can learn a lot about this very tool by checking the man page. To do that, just type:

If that doesn’t spit out some information, you don’t have wc installed and should install it. Assuming it does, and it should let’s get into the meat of the article…

Count Letters Words And Lines:

Oh yes… I mentioned the terminal in the intro. That means you’ll need an open terminal for this. It isn’t easy to word this differently in every article, but you can just press CTRL + ALT + T and your default terminal should open.

With your terminal now open,  you can first count the letters (characters) in a file. That’s nice and easy. It’s just this command:

For example, let’s use a file most of you will have:

See? Nice and simple…

Now, if you want to count the words in a file, that’s just this command:

Finally, if you want to count the number of lines in a file, you can do just that. The command is fairly obvious and looks like this:

Now, of course, there are other options with the wc command, but I’ve covered the few that you’re most likely to use. The intro had a command that will show you the man page and you should use that if you have any questions about what more the command can do.

Closure:

Yeah, this is a nice and simple article. It’s only useful for those folks who want to count letters, words, and lines. There’s a subset of users who will want to know this. For instance, I ran this on a computer I don’t use that often and discovered that I’d never set up an infinite bash history file on this computer.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Count Word Frequency In A Text File

Have you ever wanted to count the word frequency in a text file? Have you ever wanted to know how many times you used a word in a document? Well, with just two simple commands (grep and wc) we’re going to learn how to do just that.

As a writer, I try to avoid using the same words over and over again. It’s not just bad form, using the same words repeatedly actually makes the document more difficult to comprehend. The brain needs variety (and key concept repetition) or else it just kinda checks out and doesn’t pay much attention.

There are other times when you may want to count the number of words used in a document, perhaps as a reference. “Your honor, the document in question contained my client’s name 245 times – and none of the uses were factual!” I’m sure you can concoct a scenario where you may want this information. You can probably come up with one that doesn’t even involve a judge!

You can also use it a little more broadly, and we’ll cover that. For now, let’s make sure we’re all working on the same page. Press CTRL + ALT + T and open up your terminal, and then enter the following commands:

There. Now that we’re on the same page (you have the text file in your downloads folder) we can all work with the same list of random numbers – instead of random words. (All we care about is that there are characters.)

Count Word Frequency:

Well, you already have the terminal open and you’ve already downloaded my random numbers file. We’ll substitute numbers for words – as words are just a string of characters. So, seeing as you’re prepared…

Trust me, it won’t make a difference that we’re using numbers. By the time I’m done explaining this, you’ll understand and apply it to words (or other characters) all on your own. It’s pretty straightforward and easy to understand.

Let’s say we wanted to count the instances of 62829. The command would look like this:

If you run that command, you’ll see that that string of characters occurs just once. That’s expected and correct.

You can also do things like finding all the instances of 1 (or any single character) in the list with this command:

You can be even more complicated and find all the times a 7 immediately follows a 2. That command would look like this:

(There are three instances of 27.)

So, what’s going on? Well, you are using grep (to search) the contents of the file. You are then piping the output to wc where the number of lines (instances) are being counted.

You can probably be pretty fancy with this, but I just wanted to give a quick overview. Mostly, I figured it’s a good excuse to dig out grep and wc – and who doesn’t like panning for nuggets in text?

Closure:

Yup… This one isn’t a very long (or complicated) article. That’s okay. I like articles of all shapes and sizes. This article will help you count word frequency, something we all may need at one point or another. Sure enough, Linux makes this a pretty simple task.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Subscribe To Our Newsletter
Get notified when new articles are published! It's free and I won't send you any spam.
Linux Tips
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.