How To: Show File Sizes When Listing Files In A Directory

Today we’re going to have an easy article that shows you how to show file sizes when listing files in a directory. It’s a simple set of commands and easy enough for a newbie to learn. Though the title is unwieldy, read on to learn how to show file sizes when listing files in a directory…

We’ll be using the ‘ls’ command for this exercise. Like normal, I’ll go ahead and suggest you read this page about why you shouldn’t parse the output from the ‘ls’ command. There are better ways and that link will explain it, and what you should consider, far better than I can.

Anyhow, the ‘ls’ command defines itself as:

ls – list directory contents

Of course, regular readers will recognize the ls command as we’ve used it numerous times before. Even though you shouldn’t parse the output from the command doesn’t mean it’s not still useful for a variety of other tasks.

Today, we’ll be exploring one of those tasks. We’re going to show you how to show file sizes when listing files in a directory – specifically with the ls command. Don’t worry, you won’t need to install anything. If you’re using any desktop Linux distro, the ls command is certainly available to you by default.

For the record, you should probably view the ls man page (run man ls in the terminal) to learn more about the command. It’s still a useful command – and I do sometimes parse the output from the command, but only when nobody is looking and when the output isn’t all that vital. I use grep with the ls command almost every day! Shh!!!

Show File Sizes When Listing Files In A Directory:

(That’s an unwieldy title!)

Anyhow, it’s the ls command. That’s a terminal-based command. So, of course, you’re going to need an open terminal (like so many of these articles). Press CTRL + ALT + T and your default terminal should open. Tada!

With your terminal now open, we can just use your home folder. To run the basic ls command, it’s just that:

Now, to show file sizes when listing files in a directory, you’d start with the -s flag:

You may find the output from that command is relatively useless without doing a bit of math to go along with it. Fortunately, you can tell the command to show the file size in a ‘human-readable format’. To do that, you’d run this command with the -h flag:

Now, if you wanted to show all the files, including the hidden files in that directory, you’d just add the -a flag. The command would look like this:

And if I was a Linux newbie, that’s the command I’d commit to memory. It doesn’t make a mess. The output is still plenty clear. It’s easy to understand. It’s easy to remember and turn into a habit. This way, when you ran the ls command, you just default to using -ash flags.

If you don’t want columns (and some do not), and you want more information such as file permissions or ownership values, you can just add an -lflag to the above. That’d look like:

So, it’s up to you…

You can remember ‘ash’ or you can remember ‘lash’. You can also not remember either and ignore the flags completely, but I find developing a habit is a good way to remember things. It’s your device, you do what you want!

Closure:

There you have it! You have another article. This one was simple enough and I hope you found it useful. If you’re new and running the ls command is new to you, it’s worth learning some additional flags and gathering more information at a glance. That’s why there’s this relatively simple article that shows you how to show file sizes when listing files in a directory.

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.

Let’s Find The Size Of A Directory

Today’s article is one where we’re going to find the size of a directory. Why? Because that sounds like something fun to do. The site has more than 250 articles, most of which are reasonably productive and authored by me. (I do love me some guest articles – so long as they’re from the community. No more paid stuff! That lesson was learned.) They can’t all be winners!

So, why would you want to know the size of a directory? How should I know? Maybe you need to make space? You could want to upload the directories and want to know if you have space at the destination? Perhaps you are going to transfer the files and you want to estimate how long that will take? You do you. I don’t care why you want to find the size of a directory. That sort of thing is between you and your directories!

Anyhow, we’ll be using ‘du’ in this article. If you want a GUI way to find the size of your directories, you can visualize disk usage easily enough. You can also easily find large files. You shouldn’t need to install ‘du’, it should be installed as a part of the standard toolkit (GNU coreutils). To find out if you have ‘du’ installed, you can just try du --v in the terminal, which will spit out some version information.

So this should be a fairly short article – nice and easy. Let’s get on with it!

Find The Size Of A Directory:

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.

We’re going to us ‘du -h‘ for everything, because that -h stands for ‘human readable’ and it’s just easier than seeing byte sizes. You don’t actually have to use the -h, it just makes good sense to do so.

The format for this is largely ‘du -h /directory‘, like so (using the Documents directory as an example):

You can just add more directories as needed (we’ll add downloads):

That will output the size of both directories. If you are unaware, the ~ (tilde) is a shortcut meaning your home folder. For example, if you’ve navigated away from your desktop, you can cd ~ and get back home. It’s faster than clicking your heels together three times!

By the way, if you’re stringing directories together, you can get the total size of the directories with the following:

That’s about it. That’s all you really need to know for this exercise. I told you that it’d be quick and easy! Most of my articles involve opening the terminal, and yet so many of them are really quite simple to learn and remember. I strongly urge new Linux users learn a bit about the terminal, not because it’s hard but because it’s often easier and faster than using a graphical tool.

Closure:

I wasn’t kidding about there being more than 250 articles. There are 256 of them at this time, and this will make 257. So far, I haven’t run out of ideas and I’ve been able to get one out to you every other day. I hope you find these articles as useful as I’d like them to be. It does take me longer to come up with ideas, so it might have to slow down at some point. I mean, I can’t keep writing these kind of articles forever,  can I? No… No, I don’t think I can.

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.