Let’s Count The Number Of Files In A Directory

Today’s article probably isn’t all that useful unless, of course, you want to count the number of files in a directory. This can be useful if you want to quickly see if all the files were copied over or the like. Maybe you’ve made a backup and want to ensure the number of files matches. Who knows? It’s your terminal, you do what you want!

This article comes from my notes. These notes were right next to the notes used for the last article, where we learned to count letters, words, and lines in a file. I did get some feedback about why some folks want to know that information, which is always good feedback as far as I’m concerned. It’s great to learn the ways you put these commands to work.

Seeing as I’m lazy and just using the next notes in line, we’ll be using the ‘wc’ command again. If you didn’t read yesterday’s article, or got here by way of a search engine result, then the wc command describes itself like this:

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

You can expect the wc command to be included in every distro. It is a fundamental application and small enough to be a default application. Even the tiniest desktop distros will likely have this application and you won’t need to install anything. You will need an open terminal.

Count The Number Of Files In A Directory:

As I said in the intro, you’re going to need to use the terminal. Regular readers will notice a trend. We use the terminal a great deal. Just press CTRL + ALT + T and you should be good to go. (Some distros don’t do that as a standard. I’m looking at you, Elementary OS.)

With your terminal now open, navigate to your favorite directory with:

For example:

Then, when you’re there, try the following command:

You can also specify the path, just like you would with the regular ls command. That’d be something like:

If you want to count all the files, including the hidden files, the command would be quite similar:

In case any of this is unclear, I’ve made an image showing how I can count the number of files in a directory with a few similar commands. It looks like this:

using the ls and wc commands together to count the number of files in a directory
See? If you want to count the number of files in a directory, you can do that!

As always, be sure to check the man page for both of these commands:

And:

So, have fun counting the number of files in a directory – while learning a bit about ls, wc, and the almighty pipe (which I’ve not yet written about in any great detail).

Closure:

Of course, I hope you have fun with this one. I’m not sure how many creative ways you can use this set of commands, but it’s nice to use the pipe in an article, even if it’s just one where we count the number of files in a directory. We did use it in a fun article about cowsay and fortune. It has also been used in a few other articles, but we should have an article all about it.

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.

Let’s Count The Running Processes In Linux

This article will show you how to count the running processes on your Linux system. Like many articles, it will show you how to use some tools that you may not yet know about.

The tools in question will be ‘ps‘ and ‘wc‘. The ‘ps’ command reports the current processes and the ‘wc’ stands for word count and has uses beyond just counting individual words, as this article will show you.

If you’re curious, the man page describes them as:

ps – report a snapshot of the current processes.

And:

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

We will be using both of these tools, not just to just count the running processes, but also to expose you to these tools. After all, it’s really not all that important to count the running processes. I suppose if you have resource constraints, but it’s not so important the number of running processes – but how computationally expensive those processes are.

So, this should actually be a pretty short article and it’s definitely in the beginner territory. If you’re an advanced user, it’s likely that you’ll have at least some familiarity with these tools. Even if you’re an expert, have some fun and read on!

Count The Running Processes:

Like many other articles on this site, we start with an open terminal. You can open your terminal with your keyboard – just press CTRL + ALT + T and your default terminal should open.

With the terminal now open, we’ll start with the ‘ps’ command. Running just ‘ps’ is pretty useless, so be sure to check man ps for more information. Instead, we’ll run it with the -e flag to show every running process.

Tada! That shows you every running process. Now, let’s see the same command with the -U flag. You can’t just use the -U flag by itself, it requires a username. So, try this to show all the processes being run by a specific user:

Where you see <user>, put your username. In my case, the command would look like ps -U kgiii – and don’t forget that it is case sensitive and lowercase letters are all you can use in your username. Anyhow, that command will show the running processes owned by the user ‘kgiii’.

So, all that is well and good. How about we now count them? To do that, you just need to add a pipe and wc -l. We’ve used a pipe before, and the pipe is one of the greatest concepts in Linux – as it allows you to take the output from one command and pipe it into a new command.

Using the information above, we would then try:

And to count the processes from a specific user, you’d just use:

An example output of the latter would look something like this:

A count of processes owned by user 'kgiii'.
Counting the processes owned by user ‘kgiii’. A nice even 200, strangely enough.

That’s about it, really. You can check the man pages for both ‘ps’ and ‘wc’ to learn more about them, but this would be how I’d count the running processes on my Linux boxes. 

Closure:

There you have it, another article – this one telling how to count the running processes. Hopefully, we all learned a little something today and hopefully we’ll all learn a little something new in two days – which is when the next article should come out.

We’re approaching the halfway point in this year-long project. It has been good fun and I’ve learned a great deal. If I had to speculate, I would guess that this project will keep going beyond the one year mark, though maybe with a more lenient publishing schedule!

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.