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.

How To: Have Infinite Bash History

Today we’re going to have a good time because we’re learning how you can have infinite Bash history. There’s a bit more to it and not all of you will want an infinite Bash history. But, for those that do, we’re going to accomplish that right here in this article!

First, I’m assuming you’re using Bash as your shell. The odds are really good that you’d know if you’re not using Bash, as Bash is pretty much the default. Bash is the interactive shell you’re using, known as Bourne Again Shell. If you don’t know what shell you’re using, I covered that in an article about determining the shell you’re using.

Side Note: I sometimes write articles that are meant to preface another article, but then I forget and don’t write the additional article. I suppose that helps keep things interesting…

So, as you know, when you type a command into the terminal that command is stored in a file typically called ~/.bash_history – a hidden file in your home directory. Well, there’s a limit to how much history that file will retain and this is Linux – meaning that, of course, you can change that value. Today, we’ll be changing that value – making it ‘infinite’.

No, of course, it’s not really infinite. There’s a finite amount of disk space you have. This being a plain text file, it takes up very little space. While it’s not technically infinite you can store a whole lot of entries in your Bash history. We’ll set it to infinite, but I’ll show you how to set it to any value you want.

Ready?

Infinite Bash History:

As you can see, this is a Bash thing. That’s a good sign that you’ll need an open terminal. You can press CTRL + ALT + T and your default terminal should open. See? We’re already making progress!

With your terminal open, we’re going to edit another hidden file. The name of this file is .bashrc and it’s a file that stores things like terminal settings.

The tool we’ll be using is ‘nano‘. Click that link to learn about Nano, including how to install Nano should Nano not come preinstalled in your chosen distro. As you may know, Nano is one of my favorite text editors. We use Nano a lot around here.

Start with opening the ~/.bashrc file with nano, like so:

Scroll to the bottom and add the following lines:

Any negative value, in this case, -1, will result in an infinite history. However, you can make that value any number you want. When you’re done adding those lines, you have to save the file. Remember, to save a file in Nano, you just press CTRL + X, then Y, and then ENTER.

You could make the value 5000, for example. That’d simply look like this:

The sky is the limit. You can make that value any number you want, but all negative values will be treated the same. If the value is negative, it means you’ve got an infinite Bash history. Don’t forget to save any changes with Nano, as instructed above.

Now, you could reboot or log out to make the new settings take effect, or you can simply tell your system to reload the file to make the changes take immediate effect. That’s an easy command, simply use:

As you can see, infinite isn’t infinite (nothing is in the real world, according to current observations) and you don’t have to choose the infinite setting. You can pick whatever numbers you fancy and the process is the same.

Closure:

There you go, you now know how to set up your system to have an infinite Bash history. Well, an infinite ~/.bash_history if you prefer. With that setting engaged, you’re limited only by your creativity, fanaticism, and disk space. Go nuts with it! They make bigger disks! (Also, it takes up very little space.)

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 Tell If You’re Using Wayland Or X11

Today’s article will be an interesting one, for at least a subset of readers, as we learn how to tell if you’re using Wayland or X11. The command isn’t all that difficult and is just a single command that should work with everything, but the topic might confuse some of you.

Frankly, if you ask this question to the majority of users, you’re probably going to find out that they’re still using X11. The move to Wayland has been a process, that started back in 2008. The goal is to replace X and provide a much better set of protocols.

That’s what Wayland is… It’s a set of protocols dealing with how things are displayed on your monitor. X is also a set of protocols for the display server – and it dates back to 1984. Without X (or something else), we’d have been staring at nothing but terminal outputs this entire time, so it is kind of a big deal.

That doesn’t mean the current X implementation is that old on your device, it just means that it has been the default for a long time. The upstart, that is Wayland, is meant to take care of a variety of flaws – including potential security flaws. That’d be a subject way too deep to get into today.

Currently, some distros are confident with Wayland and release distros that default to Wayland. Some offer it as an option, without it being the default. The GNOME and KDE desktop environments are currently the closest to being ‘Wayland-ready’. It’s a slow process!

So, you might not know if you’re using Wayland or X11 and this article tells you how to check just that.

Are You Using Wayland Or X11:

This article requires an open terminal, like many other articles on this site. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

With your terminal open, just run the following command: (It should work in any distro, regardless of which display server you’re running.)

The output should look a lot like this:

Unless you’re using Wayland. In that case, it’d look something like this:

If the output says x11,  you’re not using Wayland. If it says Wayland, you’re using Wayland. I probably didn’t need to specify this, but I did. This way, you can be completely sure if you’re using Wayland or x11.

Closure:

See? There’s a fun command you can play around with. If you’re not sure if you’re using Wayland or X11, this command will get you sorted quickly and easily. There are all sorts of great things you can do with an open terminal and a little bit of knowledge.

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 Make A Directory Tree

Today we’re going to install a nifty application and then we’re going to make ourselves a directory tree. We’ll learn a couple of other things along the way, as we often do. Linux is great like that, and so we’ll have some fun along the way – including generating an entirely pointless text file that takes up too much space and takes too much time! That sounds fun to me!

So, what is a directory tree? Well, you start in one directory and then make a “tree” of all the directories below it. The directory you start in will be the top of the tree, and the directories below will be the branches. In its simplest form, it might look a little like this:

using the tree command to make a directory tree
As you can see, that’s part of the tree – with various directories and sub-directories.

You can’t see the parent directory, but there is one. That’s just a piece of the tree, which should be enough to explain this tree concept.

The tool we’ll be using is known as ‘tree’, of course. The man page describes it as being:

tree – list contents of directories in a tree-like format.

You may not have ‘tree’ installed and you will need to install it. It should probably be a default tool, but it is not. Use your package manager, search for ‘tree’, and install it. In Ubuntu/Debian/Derivatives, you can try:

That should get you started and, with tree now installed, we can…

Make A Directory Tree:

If you had your terminal open to install ‘tree’, you might as well leave it open. You’ll need it for the rest of this article. If your terminal is not open,  you’ll need to open it. Just press CTRL + ALT + T. Tada!

Now, the first thing we’re going to do is right there in your home directory, simply make a directory tree with the following command:

That will make a lovely tree. The -d flag means that it will only show directories in the output. That’s what we wanted, a directory tree. However…

You can make a tree with the files included. Just drop the -d flag and try this:

Now, let’s send that output to a file. If you want a tree (with or without files listed in it) as a saved text file, you can try this command:

Now, for some fun, you can try this (it will take a long time):

You need ‘sudo’ for that command so that it can traverse and list the various directories for which you have no permissions. It will take a long time. I want to say that it took about 30 minutes, but I currently have a bunch of external stuff hooked up to this device.

It will also generate a giant text file. Mine was over 500 MB in size. Opening it is a slow and painful process, but it’s not too bad once it is open. There are other options for the tree command, just type man tree to learn more about the tree command.

Closure:

So, now you know how to create a directory tree. You also know how to make your directory tree include files and how to make a directory tree of your entire computer’s file system. I don’t know why you’d need to know this, but now you do. They do make good text documents for small sections of the tree, especially if you need to quickly see the directory structure for some other 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 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.