Linux ls command - How to show the permissions and size of a directory

How to see the permissions and size of a directory

Here's a short story :) on how to look at files and directories on a Unix/Linux computer system. I've seen so many people do this the hard way that I thought I should finally write something about it.

When you're in a directory, like /home/al, and you have other subdirectories in /home/al, like bin, lib, stuff, and etc, how do you see the size of these subdirectories? You basically have three choices, and I'll start with the worst of the three ways, and conclude with the two most rational ways.

In each case, imagine that you want to see the permissions or size of the directory named bin, and bin exists in your home directory.

Option 1: cd to the desired directory

In the first option you cd to the directory you want to see the size of, then run the ls command from that directory. This is not a smart way to do this at all, but I thought I better show it, because I've certainly seen people do it.

Here is the step-by-step approach:

Command Description
cd takes you to your home directory
cd bin takes you to the bin subdirectory
ls -al | more lists all of the files in the bin subdirectory, then uses the more command to page the directory listing. The file at the top of the listing, represented as the file ., is the current directory, so this line will show you the permissions of this subdirectory. Of course this is way too much work.

Option 2: ls -al | more, or ls -al | grep 'bin'

In the second option you simply move to your home directory, then run the ls -al command from that directory. As in the first example you pipe the output from this command into the more command, so you can easily page through your results. In this case you are looking for the line in the output that represents your bin subdirectory.

Here is the step-by-step approach:

Command Description
cd takes you to your home directory
ls -al | more lists all of the files in your home directory, then use the more command to page the directory listing. Then look for the bin subdirectory.

I've also seen a similar approach where people use the grep command instead of the more command:

Command Description
cd takes you to your home directory
ls -al | grep 'bin' lists all of the files in your home directory, then uses the grep command to search for the bin directory in the ls -al listing.

Option 3: ls -ld bin

In the final option you use the -d option of the ls command so that ls won't look inside the bin directory, it just gives you a listing of the parameters for that directory. Normally, if you type ls -al bin you will get a listing of the bin directory parameters, but you will also get a listing of every file in that directory, which usually scrolls the line you want to see off the screen. The -d option tells ls not to list the contents of the bin directory; just show us the listing for the directory itself.

Here is the step-by-step approach:

Command Description
cd takes you to your home directory
ls -ld bin provide a long listing of the bin directory, without showing the contents of the bin directory.

Of course this also works great when you are somewhere else in the filesystem, because then you can just issue one command as shown below to see the parameters of the bin subdirectory of your home directory.

Command Description
ls -ld ~/bin provides a listing of the bin directory that is in your home directory, regardless of where you are in the filesystem at the time the command is run.

If you haven't seen it before, the "~" character is a shortcut way of referring to your home directory.

Summary

I know this is a somewhat trivial example for some people, but hopefully you saw a few things that you haven't seen before, including:

  • How to use the -d option of the ls command.
  • How to refer to your home directory using the ~ shortcut.
  • Examples of using more, grep, and simple pipe commands (ls -al|more, and ls -al|grep 'bin') in Unix.