Individual Entry

Una'bash'edly 'more'

If you're like me, your system's drive is rife with thousands of directories replete with thousands of files. When you want to see a directory listing, you type in the command and then, faster than even the best student graduate from the Evelyn Wood Reading Dynamics courses can read, the filenames fly by you. If you're a system manager or you're off traipsing through your system's files, you'll find directories chock full of files that probably dwarf the numbers you have within your own personal directories. If you're using OpenVMS's DECW$TERMINAL, Mac OS X's Terminal or iTerm (a free and far superior terminal emulator) application or xTerm on linux, you have the scrollbar to view back through that which has passed you by.

The OpenVMS DIRECTORY command, fortunately, has the /PAGE qualifier which causes the output to pause every terminal page size lines making it much easier to peruse through a long listing of files. Mac OS X and Linux, of course, have GUI file browsers but if you find yourself at the command line on these systems, what do you do? You use the ls command.

I prefer to see more than just the filenames when I want a directory listing. Without any command switches, ls tends to glob all the names together in a fashion similar to the OpenVMS DIRECTORY command without any command qualifiers. So, I tend to alias dir on Mac OS X and Linux to ls -aFl. This, generally, provides me with the file information that I am seeking. However, there is no switch which provides a paging capability like the OpenVMS DIRECTORY/PAGE command.

Never fear. Mac OS X and Linux are very unix-like and they have a command line facility known as pipe. Piping allows redirection of I/O. One feature allows the output of one command to be passed along to the input of another. On each of these systems, there are the commands more or less, both which can be utilized to paginate output to the terminal.

OK. So the problem is solved. I simply create an alias which will pipe the output from the ls -aFl to more.

macosx% alias dir 'ls -aFl | more'

ubuntu$ alias dir='ls -aFl | more'

You've noticed slightly different syntax between the Mac OS X alias and the Ubuntu alias? That is due to both having different command shells. On Mac OS X, the shell defaults to the tcsh shell whereas the default on Ubuntu is the bash shell. I tend to live in the default environments as much as possible. When working in the field, you are often on machines that are not configured as you might configure your personal system(s). We are also creatures of habit — Pavlovian dogs, if you will — and so easily conditioned that we make the same repeated mistakes even when we know we're not on one system or another. I've often found myself typing pwd on OpenVMS and SHOW DEFAULT on Mac OS X, Ubuntu and other linux systems. These habits are multiplied when using a highly customized environment daily. Besides, there is no tcsh on Ubuntu!

I now have paginated directory listings on Mac OS X just like I do on OpenVMS... almost...

With a $ DIRECTORY/PAGE I can specify a filename parameter or a partial wildcard, and the listing will only contain those files which satisfy the parameter. For example, I could have a directory of files with information about animals. Suppose, I was only interested in animals beginning with the letter A. I would enter the command $ DIRECTORY/PAGE A* and soon I'd have a listing replete with only: AARDVARK, ADDER, AI, ALBATROSS, ALLIGATOR, ALPACA, ANTEATER, ANTELOPE, APE, ARMADILO,... Well, you get the picture. Attempting to do the same with the aliased commands on Mac OS X and Ununtu do not provide the same filtered directory listing.

Why? Because the resultant command looks like this: ls -aFl | more A*. The A* is not being used as a parameter to the ls -aFl but, instead, to the more. Are these command shells so woefully weak? The answer is no. The parameter can be applied to the ls -aFl using parameter substitution.

This can be a daunting topic and I urge you, after reading the brief introduction, to hit the man pages for the tcsh if you're using Max OS X.

Parameter substitution, as I should hope its name implies, will allow command line parameters to the substituted for placeholders used on the command line. There are two I will mention here because they are applicable to this discussion:

!$ Substitute last argument
!* Substitute all arguments

For the aliased directory listing, I prefer to use the substitution parameter which substitues all arguments. That way, I can request a directory listing with a list of wildcarded parameters (eg. ls D* T*). So now, the alias dir command on my Mac OS X system looks like:

alias dir 'ls -aFl \!* | more'

The backslash is necessary to escape the ! in the command.

This is great! Mac OS X now has a simple and powerful paginated directory listing command which will even accept additional command line parameters to filter the desired results. Let's put this on Ubuntu too.

Sadly, what works for Mac OS X and its tcsh will not work for Ubuntu and its bash shell. The bash shell does not have parameter substitution like the tcsh. Does this mean I'm stuck with a paginated directory listing command that will not accept filtering parameters? The answer: Yes and No. Yes if you intent to using command aliasing; No if you know about the bash shell's in-line function capabilities.

The bash shell has a feature that does not exist in the tcsh shall, in-line function definition. Like a function you might write or use in some programming or scripting language, the command line can glob a collections of commands together to perform a more complicated operation. In addition to globbing a number of commands together, bash in-line functions can also accept and use parameters from the command line. So, the result can still be achieved; albeit, by different means. On Ubuntu in the bash shell, the paginated directory listing, allowing command line filter specification, will look like:

function dir() { ls -aFl "$@" | more ;}

Very similar to the aliased paginated directory command under the tcsh and with all of the same features.


To thwart automated comment SPAM, you must answer this question to post.

Comment moderation is enabled. Your comment(s) will not be visisble until approved.
Remember personal info?
Hide email?
All html tags, with the exception of <b> and <i>, will be removed from your comment. You can make links by simply typing the url or email-address.
Powered by…