CLI magic: directory assistance, how may I help you?

37

Author: Joe Barr

In almost every column in this series, I’ve suggested that you use the man command to learn more about the commands being discussed. This week I’ll show you why that’s a good idea. Remember, there’s a lot more to even the smallest commands, like ls, which lists the contents of a directory, than meets the eye. Let’s drill down a bit and find out what else “ls” can do for us other than simply telling us the file names in a directory.

The Basics

The man page on “ls” tells us that the basic format of the command is:

ls [OPTION]… [FILE]…

By default, without any options, the command produces a list of file names in alphabetical order for either the current directory or the directories (the [FILE]s in the basic format shown above) specified.

Of course, there may be “hidden” files in that directory. To find out, we need to add an option to show all. You do that like this:

ls -a

Notice the dot and the double-dot entries? We can safely ignore the “.” and the “..” entries which are shown when “-a” is used. In fact, we can do more than ignore them, we can use a slightly different option and make them disappear. Try this:

ls -A

If you look at the output when using the upper case A, you’ll see that the dot and dot dot entries are gone. No problem, the real hidden files we were looking for are shown, and that’s what counts.

Let’s get wide

If we add another option, the “-l” for long form, the look of directory listing changes considerably. Try “ls -Al” from the command line. You should get something that looks like this:


-rw-rw-r-- warthawg warthawg 15048 Nov 3 08:00 cpl-v10.html
-rwxr-xr-x warthawg warthawg 25087 Nov 3 08:00 eclipse
-rw-rw-r-- warthawg warthawg 59 Nov 3 08:00 .eclipseproduct
drwxrwxr-x warthawg warthawg 4096 Nov 3 08:00 features
-rw-rw-r-- warthawg warthawg 10489 Nov 3 08:00 icon.xpm
-rw-rw-r-- warthawg warthawg 619 Nov 3 08:00 install.ini
-rw-rw-r-- warthawg warthawg 4743 Nov 3 08:00 notice.html
drwxrwxr-x warthawg warthawg 4096 Nov 3 08:00 plugins
drwxrwxr-x warthawg warthawg 4096 Nov 3 08:00 readme
-rw-rw-r-- warthawg warthawg 16549 Nov 3 08:00 startup.jar
drwxr-xr-x warthawg warthawg 4096 Jan 31 07:26 workspace

If you read last week’s column on permissions, you know all about that first column of r’s and w’s and x’s. Depending on your distribution, you may or may not see a column of numbers between the file permissions and the owner’s name. If you do, it’s the file size in blocks. If you don’t, you can make it appear by adding an “-s” to the command options. Some distributions do this for you.

Next come the file owner and the group associated with the file. Then comes the file size in bytes, the date and time it was created, and the name.

Is that all there is?

Not by a long shot. We’re just getting started. The man page for “ls” lists over fifty options. But don’t worry, we’re not going over all of them. Just a few more that might be handy for noobies. Of course, they are always there to use if you need them. That’s a comforting thing to know.

Let’s make the directory “human readable.” You do that by using the “-h” option. Here’s what the startup.jar file entry looks like when the the human readable option set.


-rw-rw-r-- warthawg warthawg 17K Nov 3 08:00 startup.jar

The theory being that 17K will make more sense to you than 16549. Whether or not that’s true, I can’t say.

R you still with us?

Often the same letter is used for two different options: once as a lower case option and again as a capital letter. Sometimes the options are related, like -a and -A. Sometimes not. Let’s look at -r and R, for example.

Using the lower case “-r” option produces the directory list in reverse alphabetical order, like this:


-rwxr-xr-x warthawg warthawg 4096 Jan 31 07:26 workspace
-rw-rw-r-- warthawg warthawg 16549 Nov 3 08:00 startup.jar
drwxrwxr-x warthawg warthawg 4096 Jan 31 19:15 readme
drwxrwxr-x warthawg warthawg 4096 Nov 3 08:00 plugins
-rw-rw-r-- warthawg warthawg 4743 Nov 3 08:00 notice.html
-rw-rw-r-- warthawg warthawg 619 Nov 3 08:00 install.ini
-rw-rw-r-- warthawg warthawg 10489 Nov 3 08:00 icon.xpm
drwxrwxr-x warthawg warthawg 4096 Nov 3 08:00 features
-rw-rw-r-- warthawg warthawg 59 Nov 3 08:00 .eclipseproduct
-rwxr-xr-x warthawg warthawg 25087 Nov 3 08:00 eclipse
-rw-rw-r-- warthawg warthawg 15048 Nov 3 08:00 cpl-v10.html

Using the upper case “-R” option adds the contents of any sub-directories to the output listing. You can spot the subdirectories in the directory listing above because the leading character of the permissions block is a “d” instead of an “-“. Part of the directory listing produced with the -R (such a listing can get to be very long) looks like this:


-rw-rw-r-- warthawg warthawg 15048 Nov 3 08:00 cpl-v10.html
-rwxr-xr-x warthawg warthawg 25087 Nov 3 08:00 eclipse
-rw-rw-r-- warthawg warthawg 59 Nov 3 08:00 .eclipseproduct
drwxrwxr-x warthawg warthawg 4096 Nov 3 08:00 features
-rw-rw-r-- warthawg warthawg 10489 Nov 3 08:00 icon.xpm
-rw-rw-r-- warthawg warthawg 619 Nov 3 08:00 install.ini
-rw-rw-r-- warthawg warthawg 4743 Nov 3 08:00 notice.html
drwxrwxr-x warthawg warthawg 4096 Nov 3 08:00 plugins
drwxrwxr-x warthawg warthawg 4096 Jan 31 19:15 readme
-rw-rw-r-- warthawg warthawg 16549 Nov 3 08:00 startup.jar
drwxr-xr-x warthawg warthawg 4096 Jan 31 07:26 workspace

./features:
total 40
drwxrwxr-x warthawg warthawg 4096 Nov 3 07:48 org.eclipse.jdt_2.1.2
drwxrwxr-x warthawg warthawg 4096 Nov 3 07:49 org.eclipse.jdt.source_2.1.2
drwxrwxr-x warthawg warthawg 4096 Nov 3 07:50 org.eclipse.pde_2.1.2
drwxrwxr-x warthawg warthawg 4096 Nov 3 07:42 org.eclipse.platform_2.1.2

Notice that the contents of the top directory are shown first, followed by the subdirectories.

Enough, already!

I’m not smart enough to remember all the options available for the “ls” command. So I create an alias of the options that provide what I generally need to know and save it in my .bashrc file. My suggestion for newbies is to do the same thing. My alias for ls looks like this:


alias ls='ls -Al'

Refer back to the first article in the series if you’ve forgotten how to create an alias.

When you’re not finding what you need to find in a directory listing, it’s time for you to dive back into “man ls” and learn how to get it.>